Permalink
Browse files

interrogate: support MAKE_MAP_PROPERTY

  • Loading branch information...
rdb committed Oct 8, 2017
1 parent 0e2b14c commit bf190f73064542fbb21e3ff5d0281d2306bbba82

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -18,37 +18,17 @@
*
*/
CPPMakeProperty::
CPPMakeProperty(CPPIdentifier *ident,
CPPFunctionGroup *getter, CPPFunctionGroup *setter,
CPPMakeProperty(CPPIdentifier *ident, Type type,
CPPScope *current_scope, const CPPFile &file) :
CPPDeclaration(file),
_ident(ident),
_length_function(NULL),
_has_function(NULL),
_get_function(getter),
_set_function(setter),
_clear_function(NULL),
_del_function(NULL)
{
_ident->_native_scope = current_scope;
}
/**
*
*/
CPPMakeProperty::
CPPMakeProperty(CPPIdentifier *ident,
CPPFunctionGroup *hasser, CPPFunctionGroup *getter,
CPPFunctionGroup *setter, CPPFunctionGroup *clearer,
CPPScope *current_scope, const CPPFile &file) :
CPPDeclaration(file),
_ident(ident),
_length_function(NULL),
_has_function(hasser),
_get_function(getter),
_set_function(setter),
_clear_function(clearer),
_del_function(NULL)
_type(type),
_length_function(nullptr),
_has_function(nullptr),
_get_function(nullptr),
_set_function(nullptr),
_clear_function(nullptr),
_del_function(nullptr)
{
_ident->_native_scope = current_scope;
}
@@ -23,16 +23,72 @@
* This is a MAKE_PROPERTY() declaration appearing within a class body. It
* means to generate a property within Python, replacing (for instance)
* get_something()/set_something() with a synthetic 'something' attribute.
*
* This is an example of a simple property (MAKE_PROPERTY is defined as
* the built-in __make_property):
* @@code
* Thing get_thing() const;
* void set_thing(const Thing &);
*
* MAKE_PROPERTY(thing, get_thing, set_thing);
* @@endcode
* The setter may be omitted to make the property read-only.
*
* There is also a secondary macro that allows the property to be set to a
* cleared state using separate clear functions. In the scripting language,
* this would be represented by a "null" value, or an "optional" construct in
* languages that have no notion of a null value.
*
* @@code
* bool has_thing() const;
* Thing get_thing() const;
* void set_thing(const Thing &);
* void clear_thing();
* MAKE_PROPERTY2(thing, has_thing, get_thing, set_thing, clear_thing);
* @@endcode
* As with MAKE_PROPERTY, both the setter and clearer can be omitted to create
* a read-only property.
*
* Thirdly, there is a variant called MAKE_SEQ_PROPERTY. It takes a length
* function as argument and the getter and setter take an index as first
* argument:
* @@code
* size_t get_num_things() const;
* Thing &get_thing(size_t i) const;
* void set_thing(size_t i, Thing value) const;
* void remove_thing(size_t i) const;
*
* MAKE_SEQ_PROPERTY(get_num_things, get_thing, set_thing, remove_thing);
* @@endcode
*
* Lastly, there is the possibility to have properties with key/value
* associations, often called a "map" or "dictionary" in scripting languages:
* @@code
* bool has_thing(string key) const;
* Thing &get_thing(string key) const;
* void set_thing(string key, Thing value) const;
* void clear_thing(string key) const;
*
* MAKE_MAP_PROPERTY(things, has_thing, get_thing, set_thing, clear_thing);
* @@endcode
* You may also replace the "has" function with a "find" function that returns
* an index. If the returned index is negative (or in the case of an unsigned
* integer, the maximum value), the item is assumed not to be present in the
* mapping.
*
* It is also possible to use both MAKE_SEQ_PROPERTY and MAKE_MAP_PROPERTY on
* the same property name. This implies that this property has both a
* sequence and mapping interface.
*/
class CPPMakeProperty : public CPPDeclaration {
public:
CPPMakeProperty(CPPIdentifier *ident,
CPPFunctionGroup *getter, CPPFunctionGroup *setter,
CPPScope *current_scope, const CPPFile &file);
enum Type {
T_normal = 0x0,
T_sequence = 0x1,
T_mapping = 0x2,
};
CPPMakeProperty(CPPIdentifier *ident,
CPPFunctionGroup *hasser, CPPFunctionGroup *getter,
CPPFunctionGroup *setter, CPPFunctionGroup *clearer,
CPPMakeProperty(CPPIdentifier *ident, Type type,
CPPScope *current_scope, const CPPFile &file);
virtual string get_simple_name() const;
@@ -46,8 +102,7 @@ class CPPMakeProperty : public CPPDeclaration {
virtual CPPMakeProperty *as_make_property();
CPPIdentifier *_ident;
// If length_function is not NULL, this is actually a sequence property,
// and the other functions take an additional index argument.
Type _type;
CPPFunctionGroup *_length_function;
CPPFunctionGroup *_has_function;
CPPFunctionGroup *_get_function;
@@ -456,6 +456,7 @@ typedef struct _object PyObject;
#define MAKE_PROPERTY2(property_name, ...) __make_property2(property_name, __VA_ARGS__)
#define MAKE_SEQ(seq_name, num_name, element_name) __make_seq(seq_name, num_name, element_name)
#define MAKE_SEQ_PROPERTY(property_name, ...) __make_seq_property(property_name, __VA_ARGS__)
#define MAKE_MAP_PROPERTY(property_name, ...) __make_map_property(property_name, __VA_ARGS__)
#define EXTENSION(x) __extension x
#define EXTEND __extension
#else
@@ -466,6 +467,7 @@ typedef struct _object PyObject;
#define MAKE_PROPERTY2(property_name, ...)
#define MAKE_SEQ(seq_name, num_name, element_name)
#define MAKE_SEQ_PROPERTY(property_name, ...)
#define MAKE_MAP_PROPERTY(property_name, ...)
#define EXTENSION(x)
#define EXTEND
#endif
@@ -51,6 +51,10 @@ class EXPCL_DTOOL ExecutionEnvironment {
static Filename get_cwd();
PUBLISHED:
MAKE_MAP_PROPERTY(environment_variables, has_environment_variable,
get_environment_variable, set_environment_variable);
MAKE_SEQ_PROPERTY(args, get_num_args, get_arg);
MAKE_PROPERTY(binary_name, get_binary_name, set_binary_name);
MAKE_PROPERTY(dtool_name, get_dtool_name, set_dtool_name);
@@ -75,8 +75,8 @@ InterfaceMaker::MakeSeq::
MakeSeq(const string &name, const InterrogateMakeSeq &imake_seq) :
_name(name),
_imake_seq(imake_seq),
_length_getter(NULL),
_element_getter(NULL)
_length_getter(nullptr),
_element_getter(nullptr)
{
}
@@ -86,12 +86,10 @@ MakeSeq(const string &name, const InterrogateMakeSeq &imake_seq) :
InterfaceMaker::Property::
Property(const InterrogateElement &ielement) :
_ielement(ielement),
_length_function(NULL),
_getter(NULL),
_setter(NULL),
_has_function(NULL),
_clear_function(NULL),
_deleter(NULL),
_length_function(nullptr),
_has_function(nullptr),
_clear_function(nullptr),
_deleter(nullptr),
_has_this(false)
{
}
@@ -126,9 +126,9 @@ class InterfaceMaker {
Property(const InterrogateElement &ielement);
const InterrogateElement &_ielement;
vector<FunctionRemap *> _getter_remaps;
vector<FunctionRemap *> _setter_remaps;
Function *_length_function;
Function *_getter;
Function *_setter;
Function *_has_function;
Function *_clear_function;
Function *_deleter;
Oops, something went wrong.

0 comments on commit bf190f7

Please sign in to comment.