Skip to content

Commit

Permalink
UPBGE: Use member function in EXP_ListWrapper.
Browse files Browse the repository at this point in the history
EXP_ListWrapper is now a template class based upon EXP_BaseListWraper.
The template parameter are the Object and its memeber functions to call.
These memeber fuctions are encapsulated in static function defined in EXP_ListWrapper.
  • Loading branch information
panzergame committed Jun 3, 2018
1 parent 69d9d10 commit 99455b2
Show file tree
Hide file tree
Showing 27 changed files with 394 additions and 380 deletions.
3 changes: 2 additions & 1 deletion source/gameengine/Expressions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(INC_SYS

set(SRC
intern/BaseListValue.cpp
intern/BaseListWrapper.cpp
intern/BoolValue.cpp
intern/ConstExpr.cpp
intern/EmptyValue.cpp
Expand All @@ -54,9 +55,9 @@ set(SRC
intern/PyObjectPlus.cpp
intern/StringValue.cpp
intern/Value.cpp
intern/ListWrapper.cpp

EXP_BaseListValue.h
EXP_BaseListWrapper.h
EXP_BoolValue.h
EXP_ConstExpr.h
EXP_EmptyValue.h
Expand Down
106 changes: 106 additions & 0 deletions source/gameengine/Expressions/EXP_BaseListWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Porteries Tristan.
*
* ***** END GPL LICENSE BLOCK *****
*/

/** \file EXP_BaseListWrapper.h
* \ingroup expressions
*/

#ifdef WITH_PYTHON

#ifndef __EXP_BASELISTWRAPPER_H__
#define __EXP_BASELISTWRAPPER_H__

#include "EXP_Value.h"

class EXP_BaseListWrapper : public EXP_Value
{
Py_Header
public:

using GetItemFunction = PyObject *(*)(EXP_PyObjectPlus *, unsigned int);
using GetItemNameFunction = std::string (*)(EXP_PyObjectPlus *, unsigned int);
using GetSizeFunction = unsigned int (*)(EXP_PyObjectPlus *);
using SetItemFunction = bool (*)(EXP_PyObjectPlus *, unsigned int, PyObject *);
private:
/// The client instance passed as first argument of each callback.
EXP_PyObjectPlus *m_client;
/// Weak reference to the client proxy.
PyObject *m_weakRef;
/// Returns the list size.
GetSizeFunction m_getSize;
/// Returns the list item for the giving index.
GetItemFunction m_getItem;
/// Returns name item for the giving index, used for python operator list["name"].
GetItemNameFunction m_getItemName;
/// Sets the new item to the index place, return false when failed item conversion.
SetItemFunction m_setItem;

/// Flags used to define special behaviours of the list.
int m_flag;

public:
enum Flag {
FLAG_NONE = 0,
/// Allow iterating on all items and compare the value of it with a research key.
FLAG_FIND_VALUE = (1 << 0),
/// Allow no validation using weak ref.
FLAG_NO_WEAK_REF = (1 << 1)
};

EXP_BaseListWrapper(EXP_PyObjectPlus *client,
GetSizeFunction getSize, GetItemFunction getItem,
GetItemNameFunction getItemName, SetItemFunction setItem, Flag flag = FLAG_NONE);
virtual ~EXP_BaseListWrapper();

/// \section Python Interface
static bool CheckValid(EXP_BaseListWrapper *list);
unsigned int GetSize() const;
PyObject *GetItem(int index) const;
std::string GetItemName(int index) const;
bool SetItem(int index, PyObject *item);
bool AllowSetItem() const;
bool AllowGetItemByName() const;
bool AllowFindValue() const;

/// \section EXP_Value Inherited Functions.
virtual std::string GetName();
virtual std::string GetText();
virtual int GetValueType();

// Python list operators.
static PySequenceMethods py_as_sequence;
// Python dictionnary operators.
static PyMappingMethods py_as_mapping;

static Py_ssize_t py_len(PyObject *self);
static PyObject *py_get_item(PyObject *self, Py_ssize_t index);
static int py_set_item(PyObject *self, Py_ssize_t index, PyObject *value);
static PyObject *py_mapping_subscript(PyObject *self, PyObject *key);
static int py_mapping_ass_subscript(PyObject *self, PyObject *key, PyObject *value);
static int py_contains(PyObject *self, PyObject *key);

EXP_PYMETHOD_VARARGS(EXP_BaseListWrapper, Get);
};

#endif // __EXP_BASELISTWRAPPER_H__

#endif // WITH_PYTHON
110 changes: 36 additions & 74 deletions source/gameengine/Expressions/EXP_ListWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,84 +29,46 @@
#ifndef __EXP_LISTWRAPPER_H__
#define __EXP_LISTWRAPPER_H__

#include "EXP_Value.h"

class EXP_ListWrapper : public EXP_Value
#include "EXP_BaseListWrapper.h"

template <class Object,
unsigned int (Object::*GetSizeFunc)(),
PyObject *(Object::*GetItemFunc)(unsigned int),
bool (Object::*SetItemFunc)(unsigned int, PyObject *) = nullptr,
std::string (Object::*GetItemNameFunc)(unsigned int) = nullptr>
class EXP_ListWrapper : public EXP_BaseListWrapper
{
Py_Header
private:
/** The client instance passed as first argument of each callback.
* We use a void * instead of a template to avoid to declare this class
* for each use in KX_PythonInitTypes.
*/
void *m_client;

// The python object which owned this list.
PyObject *m_base;

/// Returns true if the list is still valid, else each call will raise an error.
bool (*m_checkValid)(void *);

/// Returns the list size.
int (*m_getSize)(void *);

/// Returns the list item for the giving index.
PyObject *(*m_getItem)(void *, int);

/// Returns name item for the giving index, used for python operator list["name"].
const std::string (*m_getItemName)(void *, int);

/// Sets the nex item to the index place, return false when failed item conversion.
bool (*m_setItem)(void *, int, PyObject *);

/// Flags used to define special behaviours of the list.
int m_flag;
static PyObject *GetItem(EXP_PyObjectPlus *self, unsigned int index)
{
return (static_cast<Object *>(self)->*GetItemFunc)(index);
}

static std::string GetItemName(EXP_PyObjectPlus *self, unsigned int index)
{
return (static_cast<Object *>(self)->*GetItemNameFunc)(index);
}

static unsigned int GetSize(EXP_PyObjectPlus *self)
{
return (static_cast<Object *>(self)->*GetSizeFunc)();
}

static bool SetItem(EXP_PyObjectPlus *self, unsigned int index, PyObject *item)
{
return (static_cast<Object *>(self)->*SetItemFunc)(index, item);
}

public:
enum {
FLAG_NONE = 0,
/// Allow iterating on all items and compare the value of it with a research key.
FLAG_FIND_VALUE = (1 << 0)
};

EXP_ListWrapper(void *client,
PyObject *base,
bool(*checkValid)(void *),
int(*getSize)(void *),
PyObject *(*getItem)(void *, int),
const std::string(*getItemName)(void *, int),
bool(*setItem)(void *, int, PyObject *),
int flag = FLAG_NONE);
~EXP_ListWrapper();

/// \section Python Interface
bool CheckValid();
int GetSize();
PyObject *GetItem(int index);
const std::string GetItemName(int index);
bool SetItem(int index, PyObject *item);
bool AllowSetItem();
bool AllowGetItemByName();
bool AllowFindValue();

/// \section EXP_Value Inherited Functions.
virtual std::string GetName();
virtual std::string GetText();
virtual int GetValueType();

// Python list operators.
static PySequenceMethods py_as_sequence;
// Python dictionnary operators.
static PyMappingMethods py_as_mapping;

static Py_ssize_t py_len(PyObject *self);
static PyObject *py_get_item(PyObject *self, Py_ssize_t index);
static int py_set_item(PyObject *self, Py_ssize_t index, PyObject *value);
static PyObject *py_mapping_subscript(PyObject *self, PyObject *key);
static int py_mapping_ass_subscript(PyObject *self, PyObject *key, PyObject *value);
static int py_contains(PyObject *self, PyObject *key);

EXP_PYMETHOD_VARARGS(EXP_ListWrapper, Get);
EXP_ListWrapper(EXP_PyObjectPlus *client, Flag flag = FLAG_NONE)
:EXP_BaseListWrapper(client, GetSize, GetItem,
GetItemNameFunc ? GetItemName : nullptr,
SetItemFunc ? SetItem : nullptr, flag)
{
}
virtual ~EXP_ListWrapper()
{
}
};

#endif // __EXP_LISTWRAPPER_H__
Expand Down
Loading

0 comments on commit 99455b2

Please sign in to comment.