1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <Current.h>
#include <structmember.h>
#include <Connection.h>
#include <ObjectAdapter.h>
#include <Util.h>
#include <Ice/ObjectAdapter.h>
using namespace std;
using namespace IcePy;
namespace IcePy
{
struct CurrentObject
{
PyObject_HEAD
Ice::Current* current;
PyObject* adapter;
PyObject* con;
PyObject* id;
PyObject* facet;
PyObject* operation;
PyObject* mode;
PyObject* ctx;
PyObject* requestId;
PyObject* encoding;
};
//
// Member identifiers.
//
const Py_ssize_t CURRENT_ADAPTER = 0;
const Py_ssize_t CURRENT_CONNECTION = 1;
const Py_ssize_t CURRENT_ID = 2;
const Py_ssize_t CURRENT_FACET = 3;
const Py_ssize_t CURRENT_OPERATION = 4;
const Py_ssize_t CURRENT_MODE = 5;
const Py_ssize_t CURRENT_CTX = 6;
const Py_ssize_t CURRENT_REQUEST_ID = 7;
const Py_ssize_t CURRENT_ENCODING = 8;
}
#ifdef WIN32
extern "C"
#endif
static CurrentObject*
currentNew(PyTypeObject* type, PyObject* /*args*/, PyObject* /*kwds*/)
{
CurrentObject* self = reinterpret_cast<CurrentObject*>(type->tp_alloc(type, 0));
if(!self)
{
return 0;
}
self->current = new Ice::Current;
self->adapter = 0;
self->con = 0;
self->id = 0;
self->facet = 0;
self->operation = 0;
self->mode = 0;
self->ctx = 0;
self->requestId = 0;
self->encoding = 0;
return self;
}
#ifdef WIN32
extern "C"
#endif
static void
currentDealloc(CurrentObject* self)
{
Py_XDECREF(self->adapter);
Py_XDECREF(self->con);
Py_XDECREF(self->id);
Py_XDECREF(self->facet);
Py_XDECREF(self->operation);
Py_XDECREF(self->mode);
Py_XDECREF(self->ctx);
Py_XDECREF(self->requestId);
Py_XDECREF(self->encoding);
delete self->current;
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}
#ifdef WIN32
extern "C"
#endif
static PyObject*
currentGetter(CurrentObject* self, void* closure)
{
//
// This function intercepts requests for attributes of a Current object. We use this
// lazy initialization in order to minimize the cost of translating Ice::Current into a
// Python object for every upcall.
//
PyObject* result = 0;
assert(self->current);
Py_ssize_t field = reinterpret_cast<Py_ssize_t>(closure);
switch(field)
{
case CURRENT_ADAPTER:
{
if(!self->adapter)
{
self->adapter = wrapObjectAdapter(self->current->adapter);
if(!self->adapter)
{
return 0;
}
}
Py_INCREF(self->adapter);
result = self->adapter;
break;
}
case CURRENT_CONNECTION:
{
if(!self->con)
{
self->con = createConnection(self->current->con, self->current->adapter->getCommunicator());
if(!self->con)
{
return 0;
}
}
Py_INCREF(self->con);
result = self->con;
break;
}
case CURRENT_ID:
{
if(!self->id)
{
self->id = createIdentity(self->current->id);
}
Py_INCREF(self->id);
result = self->id;
break;
}
case CURRENT_FACET:
{
if(!self->facet)
{
self->facet = createString(self->current->facet);
}
Py_INCREF(self->facet);
result = self->facet;
break;
}
case CURRENT_OPERATION:
{
if(!self->operation)
{
self->operation = createString(self->current->operation);
}
Py_INCREF(self->operation);
result = self->operation;
break;
}
case CURRENT_MODE:
{
if(!self->mode)
{
PyObject* type = lookupType("Ice.OperationMode");
assert(type);
const char* enumerator = 0;
switch(self->current->mode)
{
case Ice::Normal:
enumerator = "Normal";
break;
case Ice::Nonmutating:
enumerator = "Nonmutating";
break;
case Ice::Idempotent:
enumerator = "Idempotent";
break;
}
self->mode = getAttr(type, enumerator, false);
assert(self->mode);
}
Py_INCREF(self->mode);
result = self->mode;
break;
}
case CURRENT_CTX:
{
if(!self->ctx)
{
self->ctx = PyDict_New();
if(self->ctx && !contextToDictionary(self->current->ctx, self->ctx))
{
Py_DECREF(self->ctx);
self->ctx = 0;
break;
}
}
Py_INCREF(self->ctx);
result = self->ctx;
break;
}
case CURRENT_REQUEST_ID:
{
if(!self->requestId)
{
self->requestId = PyLong_FromLong(self->current->requestId);
assert(self->requestId);
}
Py_INCREF(self->requestId);
result = self->requestId;
break;
}
case CURRENT_ENCODING:
{
if(!self->encoding)
{
self->encoding = IcePy::createEncodingVersion(self->current->encoding);
assert(self->encoding);
}
Py_INCREF(self->encoding);
result = self->encoding;
break;
}
}
return result;
}
static PyGetSetDef CurrentGetSetters[] =
{
{ STRCAST("adapter"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("object adapter"),
reinterpret_cast<void*>(CURRENT_ADAPTER) },
{ STRCAST("con"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("connection info"),
reinterpret_cast<void*>(CURRENT_CONNECTION) },
{ STRCAST("id"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("identity"),
reinterpret_cast<void*>(CURRENT_ID) },
{ STRCAST("facet"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("facet name"),
reinterpret_cast<void*>(CURRENT_FACET) },
{ STRCAST("operation"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("operation name"),
reinterpret_cast<void*>(CURRENT_OPERATION) },
{ STRCAST("mode"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("operation mode"),
reinterpret_cast<void*>(CURRENT_MODE) },
{ STRCAST("ctx"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("context"),
reinterpret_cast<void*>(CURRENT_CTX) },
{ STRCAST("requestId"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("requestId"),
reinterpret_cast<void*>(CURRENT_REQUEST_ID) },
{ STRCAST("encoding"), reinterpret_cast<getter>(currentGetter), 0, STRCAST("encoding"),
reinterpret_cast<void*>(CURRENT_ENCODING) },
{ 0 } /* Sentinel */
};
namespace IcePy
{
PyTypeObject CurrentType =
{
/* The ob_type field must be initialized in the module init function
* to be portable to Windows without using C++. */
PyVarObject_HEAD_INIT(0, 0)
STRCAST("IcePy.Current"), /* tp_name */
sizeof(CurrentObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
reinterpret_cast<destructor>(currentDealloc), /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
CurrentGetSetters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
reinterpret_cast<newfunc>(currentNew), /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
};
}
bool
IcePy::initCurrent(PyObject* module)
{
if(PyType_Ready(&CurrentType) < 0)
{
return false;
}
PyTypeObject* type = &CurrentType; // Necessary to prevent GCC's strict-alias warnings.
if(PyModule_AddObject(module, STRCAST("Current"), reinterpret_cast<PyObject*>(type)) < 0)
{
return false;
}
return true;
}
PyObject*
IcePy::createCurrent(const Ice::Current& current)
{
//
// Return an instance of IcePy.Current to hold the current information.
//
CurrentObject* obj = currentNew(&CurrentType, 0, 0);
if(obj)
{
*obj->current = current;
}
return reinterpret_cast<PyObject*>(obj);
}
|