1 // -*- Mode: C++; -*-
2 //                            Package   : omniORB
3 // CORBA_Any.h                Created on: 2001/08/17
4 //                            Author    : Duncan Grisby (dgrisby)
5 //
6 //    Copyright (C) 2004-2011 Apasphere Ltd.
7 //    Copyright (C) 2001 AT&T Laboratories Cambridge
8 //
9 //    This file is part of the omniORB library
10 //
11 //    The omniORB library is free software; you can redistribute it and/or
12 //    modify it under the terms of the GNU Lesser General Public
13 //    License as published by the Free Software Foundation; either
14 //    version 2.1 of the License, or (at your option) any later 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 GNU
19 //    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, see http://www.gnu.org/licenses/
23 //
24 //
25 // Description:
26 //    CORBA::Any
27 //
28 
29 #ifndef INSIDE_OMNIORB_CORBA_MODULE
30 #  error "Must only be #included by CORBA.h"
31 #endif
32 
33 //////////////////////////////////////////////////////////////////////
34 ///////////////////////////////// Any ////////////////////////////////
35 //////////////////////////////////////////////////////////////////////
36 
37 class Any {
38 public:
39   Any();
40 
41   ~Any();
42 
43   Any(const Any& a);
44 
45   // Marshalling operators
46   void operator>>= (cdrStream& s) const;
47   void operator<<= (cdrStream& s);
48 
49   // OMG Insertion operators
50   Any& operator=(const Any& a);
51 
52   void operator<<=(Short s);
53   void operator<<=(UShort u);
54   void operator<<=(Long l);
55   void operator<<=(ULong u);
56 #ifdef HAS_LongLong
57   void operator<<=(LongLong  l);
58   void operator<<=(ULongLong u);
59 #endif
60 #if !defined(NO_FLOAT)
61   void operator<<=(Float f);
62   void operator<<=(Double d);
63 #ifdef HAS_LongDouble
64   void operator<<=(LongDouble l);
65 #endif
66 #endif
67   void operator<<=(const Any& a);     // copying
68   void operator<<=(Any* a);           // non-copying
69   void operator<<=(TypeCode_ptr tc);  // copying
70   void operator<<=(TypeCode_ptr* tc); // non-copying
71   void operator<<=(Object_ptr obj);   // copying
72   void operator<<=(Object_ptr* obj);  // non-copying
73   void operator<<=(const char* s);
74   void operator<<=(const WChar* s);
75 
76   struct from_boolean {
from_booleanfrom_boolean77     from_boolean(Boolean b) : val(b) {}
78     Boolean val;
79   };
80   struct from_octet {
from_octetfrom_octet81     from_octet(Octet b) : val(b) {}
82     Octet val;
83   };
84   struct from_char {
from_charfrom_char85     from_char(Char b) : val(b) {}
86     Char val;
87   };
88   struct from_wchar {
from_wcharfrom_wchar89     from_wchar(WChar b) : val(b) {}
90     WChar val;
91   };
92   struct from_string {
93     from_string(const char* s, ULong b, Boolean nocopy = 0)
valfrom_string94       : val(OMNI_CONST_CAST(char*, s)), bound(b), nc(nocopy) { }
95     from_string(char* s, ULong b, Boolean nocopy = 0)
valfrom_string96       : val(s), bound(b), nc(nocopy) { }   // deprecated
97 
98     char* val;
99     ULong bound;
100     Boolean nc;
101   };
102   struct from_wstring {
103     from_wstring(const WChar* s, ULong b, Boolean nocopy = 0)
valfrom_wstring104       : val(OMNI_CONST_CAST(WChar*, s)), bound(b), nc(nocopy) { }
105     from_wstring(WChar* s, ULong b, Boolean nocopy = 0)
valfrom_wstring106       : val(s), bound(b), nc(nocopy) { }   // deprecated
107 
108     WChar* val;
109     ULong bound;
110     Boolean nc;
111   };
112   struct from_fixed {
from_fixedfrom_fixed113     from_fixed(const Fixed& f, UShort d, UShort s)
114       : val(f), digits(d), scale(s) {}
115 
116     const Fixed& val;
117     UShort       digits;
118     UShort       scale;
119   };
120 
121   void operator<<=(from_boolean f);
122   void operator<<=(from_char c);
123   void operator<<=(from_wchar wc);
124   void operator<<=(from_octet o);
125   void operator<<=(from_string s);
126   void operator<<=(from_wstring s);
127   void operator<<=(from_fixed f);
128 
129   // OMG Extraction operators
130   Boolean operator>>=(Short& s) const;
131   Boolean operator>>=(UShort& u) const;
132   Boolean operator>>=(Long& l) const;
133   Boolean operator>>=(ULong& u) const;
134 #ifdef HAS_LongLong
135   Boolean operator>>=(LongLong&  l) const;
136   Boolean operator>>=(ULongLong& u) const;
137 #endif
138 #if !defined(NO_FLOAT)
139   Boolean operator>>=(Float& f) const;
140   Boolean operator>>=(Double& d) const;
141 #ifdef HAS_LongDouble
142   Boolean operator>>=(LongDouble& l) const;
143 #endif
144 #endif
145   Boolean operator>>=(const Any*& a) const;
146   Boolean operator>>=(Any*& a) const;  // deprecated
147   Boolean operator>>=(Any& a) const;  // pre CORBA-2.3, obsolete; do not use.
148   Boolean operator>>=(TypeCode_ptr& tc) const;
149   Boolean operator>>=(Object_ptr& obj) const;
150   Boolean operator>>=(const char*& s) const;
151   Boolean operator>>=(const WChar*& s) const;
152 
153   struct to_boolean {
to_booleanto_boolean154     to_boolean(Boolean& b) : ref(b) {}
155     Boolean& ref;
156   };
157   struct to_char {
to_charto_char158     to_char(Char& b) : ref(b) {}
159     Char& ref;
160   };
161   struct to_wchar {
to_wcharto_wchar162     to_wchar(WChar& b) : ref(b) {}
163     WChar& ref;
164   };
165   struct to_octet {
to_octetto_octet166     to_octet(Octet& b) : ref(b) {}
167     Octet& ref;
168   };
169   struct to_string {
to_stringto_string170     to_string(const char*& s, ULong b) :
171       val(OMNI_CONST_CAST(char*&,s)), bound(b) { }
172 
to_stringto_string173     to_string(char*& s, ULong b) : val(s), bound(b) { } // deprecated
174 
175     char*& val;
176     ULong bound;
177   };
178   struct to_wstring {
to_wstringto_wstring179     to_wstring(const WChar*& s, ULong b) :
180       val(OMNI_CONST_CAST(WChar*&,s)), bound(b) { }
181 
to_wstringto_wstring182     to_wstring(WChar*& s, ULong b) : val(s), bound(b) { } // deprecated
183 
184     WChar*& val;
185     ULong bound;
186   };
187   struct to_fixed {
to_fixedto_fixed188     to_fixed(Fixed& f, UShort d, UShort s)
189       : val(f), digits(d), scale(s) {}
190 
191     Fixed& val;
192     UShort digits;
193     UShort scale;
194   };
195   struct to_object {
to_objectto_object196     to_object(Object_out obj) : ref(obj._data) { }
197     Object_ptr& ref;
198   };
199   struct to_abstract_base {
to_abstract_baseto_abstract_base200     to_abstract_base(AbstractBase_ptr& base) : ref(base) {}
201     AbstractBase_ptr& ref;
202   };
203   struct to_value {
to_valueto_value204     to_value(ValueBase*& base) : ref(base) {}
205     ValueBase*& ref;
206   };
207 
208   Boolean operator>>=(to_boolean b) const;
209   Boolean operator>>=(to_char c) const;
210   Boolean operator>>=(to_wchar wc) const;
211   Boolean operator>>=(to_octet o) const;
212   Boolean operator>>=(to_string s) const;
213   Boolean operator>>=(to_wstring s) const;
214   Boolean operator>>=(to_fixed f) const;
215   Boolean operator>>=(to_object o) const;
216   Boolean operator>>=(to_abstract_base a) const;
217   Boolean operator>>=(to_value v) const;
218   Boolean operator>>=(const CORBA::SystemException*& e) const;
219 
220   TypeCode_ptr type() const;
221   void type(TypeCode_ptr);
222 
223 
224   //
225   // omniORB non-portable extensions
226   //
227 
228   TypeCode_ptr NP_type() const;
229   // Non-portable equivalent of type() that borrows a reference to the
230   // TypeCode.
231 
NP_swap(CORBA::Any & other)232   inline void NP_swap(CORBA::Any& other)
233   {
234     CORBA::TypeCode_ptr tc         = pd_tc;
235     cdrAnyMemoryStream* mbuf	   = pd_mbuf;
236     void*		data	   = pd_data;
237     pr_marshal_fn	marshal	   = pd_marshal;
238     pr_destructor_fn	destructor = pd_destructor;
239 
240     pd_tc         = other.pd_tc;
241     pd_mbuf	  = other.pd_mbuf;
242     pd_data	  = other.pd_data;
243     pd_marshal	  = other.pd_marshal;
244     pd_destructor = other.pd_destructor;
245 
246     other.pd_tc         = tc;
247     other.pd_mbuf	= mbuf;
248     other.pd_data	= data;
249     other.pd_marshal	= marshal;
250     other.pd_destructor = destructor;
251   }
252 
253   //
254   // Non-typesafe functions.
255   //
256 
257   Any(TypeCode_ptr tc, void* value, Boolean release = 0);
258   void replace(TypeCode_ptr TCp, void* value, Boolean release = 0);
259   const void* value() const;
260 
261 
262   //
263   // omniORB data-only marshalling functions
264   //
265 
266   void NP_marshalDataOnly(cdrStream& s) const;
267   void NP_unmarshalDataOnly(cdrStream& s);
268 
269   //
270   // omniORB internal stub support routines
271   //
272 
273   // Functions provided by the stubs
274   typedef void(*pr_marshal_fn)   (cdrStream&, void*);
275   typedef void(*pr_unmarshal_fn) (cdrStream&, void*&);
276   typedef void(*pr_destructor_fn)(void*);
277 
278   inline Boolean PR_equivalent(TypeCode_ptr tc);
279   // True if the given TypeCode is equivalent to the Any's TypeCode.
280 
281   void PR_insert(TypeCode_ptr newtc, pr_marshal_fn marshal, void* data);
282   // Insert data into the Any's buffer. Used for simple types.
283 
284   void PR_insert(TypeCode_ptr newtc, pr_marshal_fn marshal,
285 		 pr_destructor_fn destructor, void* data);
286   // Set data pointer, to be possibly marshalled later. Deallocates
287   // memory buffer and/or data pointer if necessary.
288 
289   Boolean PR_extract(TypeCode_ptr     tc,
290 		     pr_unmarshal_fn  unmarshal,
291 		     void*            data) const;
292   // Extract a simple type from the Any's memory buffer. data is a
293   // pointer to the place in memory to unmarshal to.
294 
295   Boolean PR_extract(TypeCode_ptr     tc,
296 		     pr_unmarshal_fn  unmarshal,
297 		     pr_marshal_fn    marshal,
298 		     pr_destructor_fn destructor,
299 		     void*&           data) const;
300   // Extract data from the Any if the TypeCode matches. If the data is
301   // already in unmarshalled form, just sets the data pointer to point
302   // to the existing data. Otherwise, unmarshals the value from the
303   // memory buffer and caches it in the Any for future extractions.
304   //
305   // Function is marked const to satisfy standard extraction
306   // interfaces, but it can actually modify the Any.
307 
308 
309   cdrAnyMemoryStream& PR_streamToRead() const;
310   // Pack Any into a memory stream if necessary, and return it.
311 
312   cdrAnyMemoryStream& PR_streamToWrite();
313   // Clear the contents and allocate a memory stream for writing into.
314 
315 
316   void PR_clearData();
317   // Clear the contents ready to insert a different value.
318 
319 private:
320   void operator<<=(unsigned char);
321   Boolean operator>>=(unsigned char&) const;
322   // Not implemented.
323 
324   TypeCode_ptr pd_tc;
325 
326   // The Any contents can be stored in marshalled form, or as a
327   // pointer to the unmarshalled data, or both. When stored as a
328   // pointer to the data, the pd_marshal and pd_destructor members are
329   // set. Once one of the pointers has been set, it is never unset
330   // until the Any is cleared or destroyed.
331   cdrAnyMemoryStream* pd_mbuf;
332   void*               pd_data;
333   pr_marshal_fn       pd_marshal;
334   pr_destructor_fn    pd_destructor;
335 };
336