~ubuntu-branches/ubuntu/utopic/python-omniorb/utopic

« back to all changes in this revision

Viewing changes to modules/pyContext.cc

  • Committer: Bazaar Package Importer
  • Author(s): Floris Bruynooghe
  • Date: 2008-03-26 22:17:38 UTC
  • Revision ID: james.westby@ubuntu.com-20080326221738-r20t9hmikbvcg2vh
Tags: upstream-3.2
ImportĀ upstreamĀ versionĀ 3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: C++; -*-
 
2
//                            Package   : omniORBpy
 
3
// pyContext.cc               Created on: 2002/01/17
 
4
//                            Author    : Duncan Grisby (dpg1)
 
5
//
 
6
//    Copyright (C) 2002 AT&T Laboratories Cambridge
 
7
//
 
8
//    This file is part of the omniORBpy library
 
9
//
 
10
//    The omniORBpy library is free software; you can redistribute it
 
11
//    and/or modify it under the terms of the GNU Lesser General
 
12
//    Public License as published by the Free Software Foundation;
 
13
//    either version 2.1 of the License, or (at your option) any later
 
14
//    version.
 
15
//
 
16
//    This library is distributed in the hope that it will be useful,
 
17
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
//    GNU Lesser General Public License for more details.
 
20
//
 
21
//    You should have received a copy of the GNU Lesser General Public
 
22
//    License along with this library; if not, write to the Free
 
23
//    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
24
//    MA 02111-1307, USA
 
25
//
 
26
//
 
27
// Description:
 
28
//    Context support
 
29
 
 
30
// $Id: pyContext.cc,v 1.1.4.3 2005/06/24 17:36:08 dgrisby Exp $
 
31
// $Log: pyContext.cc,v $
 
32
// Revision 1.1.4.3  2005/06/24 17:36:08  dgrisby
 
33
// Support for receiving valuetypes inside Anys; relax requirement for
 
34
// old style classes in a lot of places.
 
35
//
 
36
// Revision 1.1.4.2  2003/05/20 17:10:23  dgrisby
 
37
// Preliminary valuetype support.
 
38
//
 
39
// Revision 1.1.4.1  2003/03/23 21:51:57  dgrisby
 
40
// New omnipy3_develop branch.
 
41
//
 
42
// Revision 1.1.2.2  2003/03/14 15:29:22  dgrisby
 
43
// Remove const char* -> char* warnings.
 
44
//
 
45
// Revision 1.1.2.1  2002/01/18 15:49:44  dpg1
 
46
// Context support. New system exception construction. Fix None call problem.
 
47
//
 
48
 
 
49
#include <omnipy.h>
 
50
 
 
51
OMNI_USING_NAMESPACE(omni)
 
52
 
 
53
 
 
54
void
 
55
omniPy::validateContext(PyObject* c_o, CORBA::CompletionStatus compstatus)
 
56
{
 
57
  if (!isInstance(c_o, pyCORBAContextClass))
 
58
    OMNIORB_THROW(BAD_PARAM, BAD_PARAM_WrongPythonType, compstatus);
 
59
}
 
60
 
 
61
void
 
62
omniPy::marshalContext(cdrStream& stream, PyObject* p_o, PyObject* c_o)
 
63
{
 
64
  PyObject* values = PyObject_CallMethod(c_o, (char*)"_get_values",
 
65
                                         (char*)"O", p_o);
 
66
  if (values) {
 
67
    PyObject* items = PyDict_Items(values);
 
68
    CORBA::ULong count = PyList_GET_SIZE(items);
 
69
    CORBA::ULong mlen  = count * 2;
 
70
    mlen >>= stream;
 
71
 
 
72
    for (CORBA::ULong i=0; i < count; i++) {
 
73
      PyObject* item = PyList_GET_ITEM(items, i);
 
74
      omniPy::marshalRawPyString(stream, PyTuple_GET_ITEM(item, 0));
 
75
      omniPy::marshalRawPyString(stream, PyTuple_GET_ITEM(item, 1));
 
76
    }
 
77
    Py_DECREF(values);
 
78
  }
 
79
  else {
 
80
    if (omniORB::trace(1)) {
 
81
      {
 
82
        omniORB::logger l;
 
83
        l << "Exception trying to get Context values:\n";
 
84
      }
 
85
      PyErr_Print();
 
86
    }
 
87
    else
 
88
      PyErr_Clear();
 
89
    
 
90
    OMNIORB_THROW(TRANSIENT, TRANSIENT_PythonExceptionInORB,
 
91
                  CORBA::COMPLETED_NO);
 
92
  }
 
93
}
 
94
 
 
95
PyObject*
 
96
omniPy::unmarshalContext(cdrStream& stream)
 
97
{
 
98
  PyObject* dict = PyDict_New();
 
99
  CORBA::ULong mlen;
 
100
  mlen <<= stream;
 
101
 
 
102
  if (mlen % 2)
 
103
    OMNIORB_THROW(MARSHAL,
 
104
                  MARSHAL_InvalidContextList,
 
105
                  CORBA::COMPLETED_MAYBE);
 
106
 
 
107
  CORBA::ULong count = mlen / 2;
 
108
 
 
109
  for (CORBA::ULong i=0; i < count; i++) {
 
110
    PyObject* k = omniPy::unmarshalRawPyString(stream);
 
111
    PyObject* v = omniPy::unmarshalRawPyString(stream);
 
112
    PyDict_SetItem(dict, k, v);
 
113
    Py_DECREF(k);
 
114
    Py_DECREF(v);
 
115
  }
 
116
  PyObject* r = PyObject_CallFunction(omniPy::pyCORBAContextClass,
 
117
                                      (char*)"sON", "", Py_None, dict);
 
118
  if (!r) {
 
119
    if (omniORB::trace(1)) {
 
120
      {
 
121
        omniORB::logger l;
 
122
        l << "Exception trying to construct Context:\n";
 
123
      }
 
124
      PyErr_Print();
 
125
    }
 
126
    else
 
127
      PyErr_Clear();
 
128
 
 
129
    OMNIORB_THROW(TRANSIENT, TRANSIENT_PythonExceptionInORB,
 
130
                  CORBA::COMPLETED_NO);
 
131
  }
 
132
  return r;
 
133
}
 
134
 
 
135
 
 
136
PyObject*
 
137
omniPy::filterContext(PyObject* p_o, PyObject* c_o)
 
138
{
 
139
  PyObject* values = PyObject_CallMethod(c_o, (char*)"_get_values",
 
140
                                         (char*)"O", p_o);
 
141
 
 
142
  if (values) {
 
143
    PyObject* r = PyObject_CallFunction(omniPy::pyCORBAContextClass,
 
144
                                        (char*)"sON", "", Py_None, values);
 
145
    if (r) return r;
 
146
  }
 
147
  if (omniORB::trace(1)) {
 
148
    {
 
149
      omniORB::logger l;
 
150
      l << "Exception trying to filter Context:\n";
 
151
    }
 
152
    PyErr_Print();
 
153
  }
 
154
  else
 
155
    PyErr_Clear();
 
156
 
 
157
  OMNIORB_THROW(TRANSIENT, TRANSIENT_PythonExceptionInORB,
 
158
                CORBA::COMPLETED_NO);
 
159
  return 0;
 
160
}