Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CallContext.h
Go to the documentation of this file.
1#ifndef CPYCPPYY_CALLCONTEXT_H
2#define CPYCPPYY_CALLCONTEXT_H
3
4// Standard
5#include <vector>
6
7#include <sys/types.h>
8
9
10namespace CPyCppyy {
11
12// small number that allows use of stack for argument passing
13const int SMALL_ARGS_N = 8;
14
15// convention to pass flag for direct calls (similar to Python's vector calls)
16#define DIRECT_CALL ((size_t)1 << (8 * sizeof(size_t) - 1))
17
18#ifndef CPYCPPYY_PARAMETER
19#define CPYCPPYY_PARAMETER
20// general place holder for function parameters
21struct Parameter {
22 union Value {
23 bool fBool;
24 int8_t fInt8;
25 uint8_t fUInt8;
26 short fShort;
27 unsigned short fUShort;
28 int fInt;
29 unsigned int fUInt;
30 long fLong;
31 intptr_t fIntPtr;
32 unsigned long fULong;
33 long long fLLong;
34 unsigned long long fULLong;
35 int64_t fInt64;
36 uint64_t fUInt64;
37 float fFloat;
38 double fDouble;
39 long double fLDouble;
40 void* fVoidp;
41 } fValue;
42 void* fRef;
43 char fTypeCode;
44};
45#endif // CPYCPPYY_PARAMETER
46
47// extra call information
49 CallContext() : fCurScope(0), fPyContext(nullptr), fFlags(0),
50 fArgsVec(nullptr), fNArgs(0), fTemps(nullptr) {}
51 CallContext(const CallContext&) = delete;
53 ~CallContext() { if (fTemps) Cleanup(); delete fArgsVec; }
54
56 kNone = 0x000000,
57 kIsSorted = 0x000001, // if method overload priority determined
58 kIsCreator = 0x000002, // if method creates python-owned objects
59 kIsConstructor = 0x000004, // if method is a C++ constructor
60 kHaveImplicit = 0x000008, // indicate that implicit converters are available
61 kAllowImplicit = 0x000010, // indicate that implicit conversions are allowed
62 kNoImplicit = 0x000020, // disable implicit to prevent recursion
63 kCallDirect = 0x000040, // call wrapped method directly, no inheritance
64 kFromDescr = 0x000080, // initiated from a descriptor
65 kUseHeuristics = 0x000100, // if method applies heuristics memory policy
66 kUseStrict = 0x000200, // if method applies strict memory policy
67 kReleaseGIL = 0x000400, // if method should release the GIL
68 kSetLifeLine = 0x000800, // if return value is part of 'this'
69 kNeverLifeLine = 0x001000, // if the return value is never part of 'this'
70 kPyException = 0x002000, // Python exception during method execution
71 kCppException = 0x004000, // C++ exception during method execution
72 kProtected = 0x008000, // if method should return on signals
73 kUseFFI = 0x010000, // not implemented
74 kIsPseudoFunc = 0x020000, // internal, used for introspection
75 };
76
77// memory handling
79 static bool SetMemoryPolicy(ECallFlags e);
80
81 void AddTemporary(PyObject* pyobj);
82 void Cleanup();
83
84// signal safety
86 static bool SetGlobalSignalPolicy(bool setProtected);
87
88 Parameter* GetArgs(size_t sz) {
89 if (sz != (size_t)-1) fNArgs = sz;
90 if (fNArgs <= SMALL_ARGS_N) return fArgs;
91 if (!fArgsVec) fArgsVec = new std::vector<Parameter>();
92 fArgsVec->resize(fNArgs);
93 return fArgsVec->data();
94 }
95
97 if (fNArgs <= SMALL_ARGS_N) return fArgs;
98 return fArgsVec->data();
99 }
100
101 size_t GetSize() { return fNArgs; }
102 size_t GetEncodedSize() { return fNArgs | ((fFlags & kCallDirect) ? DIRECT_CALL : 0); }
103
104public:
105// info/status
108 uint32_t fFlags;
109
110private:
112
113// payload
115 std::vector<Parameter>* fArgsVec;
116 size_t fNArgs;
118};
119
120inline bool IsSorted(uint64_t flags) {
121 return flags & CallContext::kIsSorted;
122}
123
124inline bool IsCreator(uint64_t flags) {
125 return flags & CallContext::kIsCreator;
126}
127
128inline bool IsConstructor(uint64_t flags) {
129 return flags & CallContext::kIsConstructor;
130}
131
132inline bool HaveImplicit(CallContext* ctxt) {
133 return ctxt ? (!(ctxt->fFlags & CallContext::kNoImplicit) && (ctxt->fFlags & CallContext::kHaveImplicit)) : false;
134}
135
136inline bool AllowImplicit(CallContext* ctxt) {
137 return ctxt ? (!(ctxt->fFlags & CallContext::kNoImplicit) && (ctxt->fFlags & CallContext::kAllowImplicit)) : false;
138}
139
140inline bool NoImplicit(CallContext* ctxt) {
141 return ctxt ? (ctxt->fFlags & CallContext::kNoImplicit) : false;
142}
143
144inline bool ReleasesGIL(CallContext* ctxt) {
145 return ctxt ? (ctxt->fFlags & CallContext::kReleaseGIL) : false;
146}
147
148inline bool UseStrictOwnership(CallContext* ctxt) {
149 if (ctxt && (ctxt->fFlags & CallContext::kUseStrict))
150 return true;
151 if (ctxt && (ctxt->fFlags & CallContext::kUseHeuristics))
152 return false;
153
155}
156
157template<CallContext::ECallFlags F>
159public:
161 fPrior = fCtxt->fFlags & F;
162 fCtxt->fFlags |= F;
163 }
167 if (!fPrior) fCtxt->fFlags &= ~F;
168 }
169
170private:
172 bool fPrior;
173};
174
175} // namespace CPyCppyy
176
177#endif // !CPYCPPYY_CALLCONTEXT_H
_object PyObject
#define e(i)
Definition RSha256.hxx:103
CallContextRAII(CallContext *ctxt)
CallContextRAII & operator=(const CallContextRAII &)=delete
CallContextRAII(const CallContextRAII &)=delete
#define DIRECT_CALL
#define F(x, y, z)
bool HaveImplicit(CallContext *ctxt)
bool NoImplicit(CallContext *ctxt)
const int SMALL_ARGS_N
Definition CallContext.h:13
bool IsCreator(uint64_t flags)
bool AllowImplicit(CallContext *ctxt)
bool UseStrictOwnership(CallContext *ctxt)
bool IsConstructor(uint64_t flags)
bool IsSorted(uint64_t flags)
bool ReleasesGIL(CallContext *ctxt)
size_t TCppScope_t
Definition cpp_cppyy.h:18
static ECallFlags sMemoryPolicy
Definition CallContext.h:78
static bool SetGlobalSignalPolicy(bool setProtected)
Cppyy::TCppScope_t fCurScope
Parameter * GetArgs(size_t sz)
Definition CallContext.h:88
CallContext & operator=(const CallContext &)=delete
std::vector< Parameter > * fArgsVec
static bool SetMemoryPolicy(ECallFlags e)
Parameter * GetArgs()
Definition CallContext.h:96
Parameter fArgs[SMALL_ARGS_N]
CallContext(const CallContext &)=delete
static ECallFlags sSignalPolicy
Definition CallContext.h:85
void AddTemporary(PyObject *pyobj)
union CPyCppyy::Parameter::Value fValue
unsigned long long fULLong
Definition callcontext.h:26