resonance 1.0.1
Loading...
Searching...
No Matches
mapbase.h
Go to the documentation of this file.
1#pragma once
2
3#include "script/cxx/config.h"
5#include "script/cxx/object.h"
6
7class HxStr;
8
9namespace Py {
10
35template <typename T>
36class MapBase : public Object {
37protected:
46 }
47
53 explicit MapBase(PyObject *pyob) : Object(pyob) {
54 validate();
55 }
56
57public:
66 virtual bool accepts(PyObject *pyob) const {
67 return pyob != nullptr && PyMapping_Check(pyob) != 0;
68 }
69
79 virtual void setItem(const HxStr &key, const Object &value) {
80 char *pszKey = const_cast<char *>(key.mStr != nullptr ? key.mStr : g_szEmptyString);
81 if (PyMapping_SetItemString(mPtr, pszKey, value.mPtr) == -1) {
82 throw Exception();
83 }
84 }
85
94 virtual void setItem(const Object &key, const Object &value) {
95 if (PyObject_SetItem(mPtr, key.mPtr, value.mPtr) == -1) {
96 throw Exception();
97 }
98 }
99};
100
101} // namespace Py
Heap-allocated NUL-terminated string.
Definition hxstr.h:41
char * mStr
Text buffer.
Definition hxstr.h:464
Root of the PyCXX exception hierarchy.
Definition exception.h:19
MapBase()
Produce a handle on None for a derived class that fills it in afterwards.
Definition mapbase.h:45
MapBase(PyObject *pyob)
Take a borrowed reference on behalf of a derived class.
Definition mapbase.h:53
virtual void setItem(const HxStr &key, const Object &value)
Store a value under a name.
Definition mapbase.h:79
virtual bool accepts(PyObject *pyob) const
Accept any reference the mapping protocol supports.
Definition mapbase.h:66
virtual void setItem(const Object &key, const Object &value)
Store a value under a key object.
Definition mapbase.h:94
Object(PyObject *pyob=Py_None)
Take a borrowed reference and add a count of its own.
Definition object.h:52
void validate()
Enforce this type's accepts() test on the stored reference.
Definition object.h:237
PyObject * mPtr
Wrapped reference, null only between release() and the next assignment.
Definition object.h:191
Configuration header of the PyCXX binding layer.
const char * g_szEmptyString
Text an empty HxStr points at, and the substitute for a null buffer.
C++ wrappers for the embedded Python interpreter's objects, types, and extension modules.
Definition attributeerror.h:7