28 #ifndef ROCKETCOREPYTHONNAMEINDEXINTERFACE_H
29 #define ROCKETCOREPYTHONNAMEINDEXINTERFACE_H
37 template <
typename HostType >
41 python::object
operator()(HostType& host,
const char* name)
43 PyErr_SetString(PyExc_KeyError,
"Invalid key.");
44 python::throw_error_already_set();
46 return python::object();
56 template <
typename HostType,
typename LengthAccessor,
typename IndexAccessor,
typename NameAccessor = NameAccessorInval
id< HostType > >
57 class NameIndexInterface :
public python::def_visitor< NameIndexInterface< HostType, LengthAccessor, IndexAccessor, NameAccessor > >
69 template <
typename ClassType >
70 void visit(ClassType& _class)
const
77 static python::object
GetItem(HostType& host, python::object key)
79 static NameAccessor name_accessor;
80 static LengthAccessor length_accessor;
81 static IndexAccessor index_accessor;
83 if (PyString_Check(key.ptr()))
87 else if (PyInt_Check(key.ptr()))
89 int index = PyInt_AsLong(key.ptr());
93 index = length_accessor(host) + index;
96 if (index >= length_accessor(host))
98 PyErr_SetString(PyExc_IndexError,
"Index out of range.");
99 python::throw_error_already_set();
105 PyErr_SetString(PyExc_KeyError,
"Invalid key.");
106 python::throw_error_already_set();
108 return python::object();
112 static int Len(HostType& host)
114 static LengthAccessor length_accessor;
115 return length_accessor(host);
~NameIndexInterface()
Definition: NameIndexInterface.h:64
python::object operator()(HostType &host, const char *name)
Definition: NameIndexInterface.h:41
Definition: Clipboard.h:33
NameIndexInterface()
Definition: NameIndexInterface.h:60
Definition: NameIndexInterface.h:57
static python::object GetItem(HostType &host, python::object key)
Python getitem override.
Definition: NameIndexInterface.h:77
static python::object MakeObject(T *object)
Definition: Utilities.h:69
void visit(ClassType &_class) const
Initialise the interface.
Definition: NameIndexInterface.h:70
Definition: NameIndexInterface.h:38
static int Len(HostType &host)
Python len override.
Definition: NameIndexInterface.h:112