Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Any.h

Go to the documentation of this file.
00001 // This may look like C, but it's really -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Any.h
00006  *
00007  *  $Id: Any.h,v 1.1.1.4.2.1 2003/04/10 14:04:02 phil Exp $
00008  *
00009  *  @author  Copyright 1994-1995 by Sun Microsystems Inc.
00010  *  @author  Aniruddha Gokhale
00011  */
00012 //=============================================================================
00013 
00014 
00015 #ifndef TAO_ANY_H
00016 #define TAO_ANY_H
00017 #include "ace/pre.h"
00018 
00019 #include "tao/CDR.h"
00020 
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif /* ACE_LACKS_PRAGMA_ONCE */
00024 
00025 #include "tao/Environment.h"
00026 #include "tao/Object.h"
00027 #include "tao/Typecode.h"
00028 
00029 /**
00030  * @class CORBA_Any
00031  *
00032  * @brief Class "Any" can wrap values of any type, with the assistance
00033  * of a TypeCode to describe that type.
00034  *
00035  * This includes three constructors, a destructor, and a "replace"
00036  * method for the "Any" data type.  "Any" values pair a pointer to
00037  * a data structure in the native binary representation (e.g. C
00038  * struct) with a TypeCode that describes that data structure.
00039  * The copy constructor and the destructor each use the TypeCode
00040  * interpreter with specialized "visit" callback routines.  The
00041  * "visit" routines are used respectively to make "deep copies"
00042  * and perform "deep frees" of the aritrary values as described by
00043  * the "Any" value's typecode.
00044  * Note that these "visit" routines are called directly, and they
00045  * choose whether or not to use the TypeCode interpreter to
00046  * examine constituents.  In the simple cases, the "visit"
00047  * routines can do their work without any further calls; only for
00048  * constructed types is the interpreter's knowledge really
00049  * required.
00050  * THREADING NOTE: "Any" is a data structure which must be
00051  * protected by external critical sections.  Like simpler numeric
00052  * types, "Any" instances are accessed and modified atomically.
00053  * This implementation is reentrant, so that independent "Any"
00054  * values may be manipulated concurrently when the underlying
00055  * programming environment is itself reentrant.
00056  */
00057 class TAO_Export CORBA_Any
00058 {
00059 public:
00060 
00061   friend TAO_Export CORBA::Boolean operator>> (TAO_InputCDR& cdr,
00062                                                CORBA::Any &x);
00063 
00064   // = Minor codes for exceptional returns
00065   enum
00066   {
00067     UNINITIALIZED_type = 0xf000,
00068     VALUE_WITHOUT_TYPE,
00069     UNSUPPORTED_OPERATION
00070   };
00071 
00072   // = Initialization and termination operations.
00073 
00074   /// Default constructor.
00075   CORBA_Any (void);
00076 
00077   /// Constructor.
00078   CORBA_Any (CORBA::TypeCode_ptr type);
00079 
00080   // = TAO extension
00081   /**
00082    * Constructor. Used by DynAny and others to optimize Any activities
00083    * by using CDR. The dummy arg is to keep calls like CORBA_Any foo
00084    * (CORBA::TypeCode_ptr bar (NULL), NULL) from being confused with
00085    * the constructor above.
00086    */
00087   CORBA_Any (CORBA::TypeCode_ptr type,
00088              CORBA::UShort dummy,
00089              int byte_order,
00090              const ACE_Message_Block* mb);
00091 
00092   /// Copy constructor.
00093   CORBA_Any (const CORBA_Any &a);
00094 
00095   /// Destructor.
00096   ~CORBA_Any (void);
00097 
00098   /// assignment operator
00099   CORBA_Any &operator= (const CORBA_Any &);
00100 
00101   // = NOTE: 94-9-14 has assignment operator plus many insertion, as
00102   // specified below.
00103 
00104   // =type safe insertion
00105 
00106   /// Insert a short.
00107   void operator<<= (CORBA::Short);
00108 
00109   /// Insert an unsigned short.
00110   void operator<<= (CORBA::UShort);
00111 
00112   /// Insert a long.
00113   void operator<<= (CORBA::Long);
00114 
00115   /// Insert an unsigned long.
00116   void operator<<= (CORBA::ULong);
00117 
00118   /// Insert a long long.
00119   void operator<<= (CORBA::LongLong);
00120 
00121   /// Insert an unsigned long long.
00122   void operator<<= (CORBA::ULongLong);
00123 
00124   /// Insert a float.
00125   void operator<<= (CORBA::Float);
00126 
00127   /// Insert a double.
00128   void operator<<= (CORBA::Double);
00129 
00130   /// Insert a long double.
00131   void operator<<= (CORBA::LongDouble);
00132 
00133   /// Insert an Any, copying.
00134   void operator<<= (const CORBA_Any&);
00135 
00136   /// Insert an Any, non-copying.
00137   void operator<<= (CORBA_Any_ptr);
00138 
00139   /// Insert unbounded string
00140   void operator<<= (const char*);
00141 
00142   /// Insert unbounded wide string.
00143   void operator<<= (const CORBA::WChar*);
00144 
00145   /// Insert a TypeCode.
00146   void operator<<= (CORBA::TypeCode_ptr);
00147 
00148   /// Insert an object reference, copying.
00149   void operator<<= (const CORBA::Object_ptr);
00150 
00151   /// Insert an object reference, non-copying. Any assumes the ownership
00152   /// of the object.
00153   void operator<<= (CORBA::Object_ptr *);
00154 
00155   // =Type safe extraction.
00156 
00157   /// Extract a short.
00158   CORBA::Boolean operator>>= (CORBA::Short&) const;
00159 
00160   /// Extract an unsigned short.
00161   CORBA::Boolean operator>>= (CORBA::UShort&) const;
00162 
00163   /// Extract a long.
00164   CORBA::Boolean operator>>= (CORBA::Long&) const;
00165 
00166   /// Extract an unsigned long.
00167   CORBA::Boolean operator>>= (CORBA::ULong&) const;
00168 
00169   /// Extract a long long.
00170   CORBA::Boolean operator>>= (CORBA::LongLong&) const;
00171 
00172   /// Extract an unsigned long long.
00173   CORBA::Boolean operator>>= (CORBA::ULongLong&) const;
00174 
00175   /// Extract a float.
00176   CORBA::Boolean operator>>= (CORBA::Float&) const;
00177 
00178   /// Extract a double.
00179   CORBA::Boolean operator>>= (CORBA::Double&) const;
00180 
00181   /// Extract a long double.
00182   CORBA::Boolean operator>>= (CORBA::LongDouble&) const;
00183 
00184   /// Extract an Any.
00185   CORBA::Boolean operator>>= (CORBA_Any&) const; // non-spec
00186   CORBA::Boolean operator>>= (const CORBA_Any*&) const;
00187 
00188   /// Extract a TypeCode.
00189   CORBA::Boolean operator>>= (CORBA::TypeCode_ptr&) const;
00190 
00191   /// Extract an unbounded string
00192   CORBA::Boolean operator>>= (const char*&) const;
00193 
00194   /// Extract an unbounded wide string.
00195   CORBA::Boolean operator>>= (const CORBA::WChar*&) const;
00196 
00197   // = Special types.
00198 
00199   // These are needed for insertion and extraction of booleans,
00200   // octets, chars, and bounded strings. CORBA spec requires
00201   // that they be here, we just typedef to the already-defined
00202   // ACE_OutputCDR types.
00203 
00204   typedef ACE_OutputCDR::from_boolean from_boolean;
00205   typedef ACE_OutputCDR::from_octet from_octet;
00206   typedef ACE_OutputCDR::from_char from_char;
00207   typedef ACE_OutputCDR::from_wchar from_wchar;
00208   typedef ACE_OutputCDR::from_string from_string;
00209   typedef ACE_OutputCDR::from_wstring from_wstring;
00210 
00211   /// Insert a boolean.
00212   void operator<<= (from_boolean);
00213 
00214   /// Insert a char.
00215   void operator<<= (from_char);
00216 
00217   /// Insert a wchar.
00218   void operator<<= (from_wchar);
00219 
00220   /// Insert an octet.
00221   void operator<<= (from_octet);
00222 
00223   /// Insert a bounded string.
00224   void operator<<= (from_string);
00225 
00226   /// Insert a bounded wide string.
00227   void operator<<= (from_wstring);
00228 
00229   /// Insert an exception into the Any (copying)
00230   void operator<<= (const CORBA_Exception &exception);
00231 
00232   /// Insert an exception into the Any (non-copying).
00233   void operator<<= (CORBA_Exception *exception);
00234 
00235   // = Special types.
00236 
00237   // These extract octets, chars, booleans, bounded strings, and
00238   // object references. All these are defined in ACE_InputCDR.
00239 
00240   typedef ACE_InputCDR::to_boolean to_boolean;
00241   typedef ACE_InputCDR::to_char to_char;
00242   typedef ACE_InputCDR::to_wchar to_wchar;
00243   typedef ACE_InputCDR::to_octet to_octet;
00244   typedef ACE_InputCDR::to_string to_string;
00245   typedef ACE_InputCDR::to_wstring to_wstring;
00246 
00247   // These are not in ACE.
00248 
00249   struct TAO_Export to_object
00250   {
00251     // This signature reflects the change set out in
00252     // issue 154 of the 1.3 RTF.
00253     to_object (CORBA_Object_out obj);
00254     CORBA::Object_ptr &ref_;
00255   };
00256 
00257   struct TAO_Export to_abstract_base
00258   {
00259     to_abstract_base (CORBA_AbstractBase_ptr &obj);
00260     CORBA::AbstractBase_ptr &ref_;
00261   };
00262 
00263   struct TAO_Export to_value
00264   {
00265     to_value (CORBA_ValueBase *&base);
00266     CORBA::ValueBase *&ref_;
00267   };
00268 
00269   // Extraction of the special types.
00270 
00271   /// extract a boolean
00272   CORBA::Boolean operator>>= (to_boolean) const;
00273 
00274   /// extract an octet
00275   CORBA::Boolean operator>>= (to_octet) const;
00276 
00277   /// extract a char
00278   CORBA::Boolean operator>>= (to_char) const;
00279 
00280   /// extract a wchar
00281   CORBA::Boolean operator>>= (to_wchar) const;
00282 
00283   /// extract a bounded string
00284   CORBA::Boolean operator>>= (to_string) const;
00285 
00286   /// extract a bounded wide string
00287   CORBA::Boolean operator>>= (to_wstring) const;
00288 
00289   /// extract an object reference
00290   CORBA::Boolean operator>>= (to_object) const;
00291 
00292   /// extract an abstract interface
00293   CORBA::Boolean operator>>= (to_abstract_base) const;
00294 
00295   /// extract a valuetype
00296   CORBA::Boolean operator>>= (to_value) const;
00297 
00298   // the following are unsafe operations
00299   // ORBOS/90-01-11, pg 672: For C++ mapping using the CORBA_Environment
00300   // parameter, two forms of the replace method are provided.
00301 
00302   /// Return TypeCode of the element stored in the Any.
00303   CORBA::TypeCode_ptr type (void) const;
00304 
00305   /// For use along with <<= of a value of aliased type when the alias must
00306   /// be preserved.
00307   void type (CORBA::TypeCode_ptr type
00308              ACE_ENV_ARG_DECL_WITH_DEFAULTS);
00309 
00310   /**
00311    * Returns 0 if the Any has not been assigned a value, following the
00312    * CORBA spec (ORBOS/98-01-11) it returns a non-zero value
00313    * otherwise. TAO does *not* guarantee that this value may be casted
00314    * to the contained type safely.
00315    */
00316   const void *value (void) const;
00317 
00318   // = TAO extensions
00319   /**
00320    * Reports whether the Any own the data or not. This is used by the
00321    * >>= operators generated by the IDL compiler. The >>= operator
00322    * checks if the Any owns the data. If it does, then it will simply
00323    * retrieve the data from internal cache.  Otherwise, the operator
00324    * will have to decode the cdr string.
00325    */
00326   CORBA::Boolean any_owns_data (void) const;
00327 
00328   /// Reports whether the Any contains (at some level) a local type.
00329   CORBA::Boolean contains_local (void) const;
00330 
00331   /// Set the member contains_local_.
00332   void contains_local (CORBA::Boolean val);
00333 
00334   /// Message block accessor.
00335   /// Get the byte order inside the CDR stream.
00336   ACE_Message_Block* _tao_get_cdr (void) const;
00337   int _tao_byte_order (void) const;
00338 
00339   /// Generated data types define a 'destructor' function that
00340   /// correctly destroys an object stored in the Any.
00341   typedef void (*_tao_destructor)(void*);
00342 
00343   /// Replace via message block instead of <value_>.
00344   void _tao_replace (CORBA::TypeCode_ptr,
00345                      int byte_order,
00346                      const ACE_Message_Block *mb);
00347 
00348   /// Replace all the contents of the any, used in the <<= operators.
00349   void _tao_replace (CORBA::TypeCode_ptr type,
00350                      int byte_order,
00351                      const ACE_Message_Block *mb,
00352                      CORBA::Boolean any_owns_data,
00353                      void* value,
00354                      CORBA::Any::_tao_destructor destructor);
00355 
00356   /// Replace the value of the Any, used in the >>= operators.
00357   void _tao_replace (CORBA::TypeCode_ptr type,
00358                      CORBA::Boolean any_owns_data,
00359                      void* value,
00360                      CORBA::Any::_tao_destructor destructor);
00361 
00362   /// Encode the contents of the Any into <cdr>
00363   void _tao_encode (TAO_OutputCDR &cdr,
00364                     TAO_ORB_Core *orb_core
00365                     ACE_ENV_ARG_DECL);
00366 
00367   /// Decode the <cdr> using the typecode in the Any object.
00368   void _tao_decode (TAO_InputCDR &cdr
00369                     ACE_ENV_ARG_DECL);
00370 
00371 #if !defined(__GNUC__) || __GNUC__ > 2 || __GNUC_MINOR__ >= 8
00372   typedef CORBA_Any_ptr _ptr_type;
00373   typedef CORBA_Any_var _var_type;
00374 #endif /* __GNUC__ */
00375   // Useful for template programming.
00376 
00377   /// Used to release Anys contained into anys.
00378   static void _tao_any_destructor (void*);
00379   static void _tao_any_string_destructor (void*);
00380   static void _tao_any_wstring_destructor (void*);
00381   static void _tao_any_tc_destructor (void*);
00382 
00383 protected:
00384   /// Release the <value_>.
00385   void free_value (void);
00386 
00387 private:
00388   /// Typecode for the <Any>.
00389   CORBA::TypeCode_var type_;
00390 
00391   /// encoded value.
00392   int byte_order_;
00393   ACE_Message_Block *cdr_;
00394 
00395   /// Flag that indicates the ORB is responsible for deleting the data.
00396   CORBA::Boolean any_owns_data_;
00397 
00398   /// Flag that indicates the Any contains (at some level) a locality-
00399   // constrained type, and so cannot be marshaled.
00400   CORBA::Boolean contains_local_;
00401 
00402   /// Value for the <Any>.
00403   void *value_;
00404 
00405   /// If not zero this is the function used to destroy objects.
00406   CORBA::Any::_tao_destructor destructor_;
00407 
00408   /// Codeset translator factories obtained from the decoding CDR, used to
00409   /// translate strings when actually needed.
00410   ACE_Char_Codeset_Translator *char_translator_;
00411   ACE_WChar_Codeset_Translator *wchar_translator_;
00412 
00413 
00414   // 94-9-14 hides unsigned char insert/extract
00415   void operator<<= (unsigned char);
00416   CORBA::Boolean operator>>= (unsigned char&) const;
00417 
00418   friend class CORBA_NVList;
00419   friend class TAO_Marshal_Any;
00420 };
00421 
00422 /**
00423  * @class CORBA_Any_var
00424  *
00425  * @brief Provide for automatic storage deallocation on going out of
00426  * scope.
00427  */
00428 class TAO_Export CORBA_Any_var
00429 {
00430 public:
00431   /// default constructor
00432   CORBA_Any_var (void);
00433 
00434   /// construct from an Any pointer
00435   CORBA_Any_var (CORBA_Any *a);
00436 
00437   /// copy constructor
00438   CORBA_Any_var (const CORBA_Any_var &a);
00439 
00440   /// destructor
00441   ~CORBA_Any_var (void);
00442 
00443   /// assignment from a pointer to Any
00444   CORBA_Any_var &operator= (CORBA_Any *a);
00445 
00446   /**
00447    * assignment from an Any_var
00448    * This operation requires memory allocation.
00449    * If the allocation fails, *this is returned
00450    * unmodified.
00451    */
00452   CORBA_Any_var &operator= (const CORBA_Any_var &a);
00453 
00454   /// arrow operator (smart pointer)
00455   CORBA_Any *operator-> (void);
00456 
00457   /// cast
00458   operator CORBA_Any &();
00459 
00460   /// cast
00461   operator const CORBA_Any *() const;
00462 
00463   /// cast
00464   operator CORBA_Any *&();
00465 
00466   /// for in Any parameter
00467   const CORBA_Any &in (void) const;
00468 
00469   /// for inout Any parameter
00470   CORBA_Any &inout (void);
00471 
00472   /// for out Any parameter
00473   CORBA_Any *&out (void);
00474 
00475   /// for Any return types
00476   CORBA_Any *_retn (void);
00477 
00478 private:
00479   /// Holds the Any.
00480   CORBA_Any *ptr_;
00481 };
00482 
00483 /**
00484  * @class CORBA_Any_out
00485  *
00486  * @brief CORBA_Any_out
00487  *
00488  * The _out class for CORBA_Any. This is used to help in
00489  * managing the out parameters.
00490  */
00491 class TAO_Export CORBA_Any_out
00492 {
00493 public:
00494   // = operations.
00495 
00496   /// construction from a reference to a CORBA_Any
00497   CORBA_Any_out (CORBA_Any *&p);
00498 
00499   /// construction from a var
00500   CORBA_Any_out (CORBA_Any_var &p);
00501 
00502   /// copy constructor
00503   CORBA_Any_out (const CORBA_Any_out &s);
00504 
00505   /// assignment from a CORBA_Any_out
00506   CORBA_Any_out &operator= (const CORBA_Any_out &s);
00507 
00508   /// assignment from a CORBA_Any
00509   CORBA_Any_out &operator= (CORBA_Any *p);
00510 
00511   /// cast
00512   operator CORBA_Any *&();
00513 
00514   /// return underlying instance
00515   CORBA_Any *& ptr (void);
00516 
00517   CORBA_Any *operator-> (void);
00518 
00519 private:
00520   /// Instance
00521   CORBA_Any *&ptr_;
00522 
00523   /// assignment from _var disallowed
00524   void operator= (const CORBA_Any_var &);
00525 };
00526 
00527 // These operators are too complex to be inline....
00528 TAO_Export CORBA::Boolean operator<< (TAO_OutputCDR& cdr,
00529                                       const CORBA::Any &x);
00530 TAO_Export CORBA::Boolean operator>> (TAO_InputCDR& cdr,
00531                                       CORBA::Any &x);
00532 
00533 #if defined (__ACE_INLINE__)
00534 # include "tao/Any.i"
00535 #else
00536 
00537 // Copying versions of insertion
00538 // operators which are defined as members of the Any class
00539 // must also be defined for Any_var.
00540 TAO_Export void operator<<= (CORBA_Any_var &,
00541                              CORBA::Short);
00542 TAO_Export void operator<<= (CORBA_Any_var &,
00543                              CORBA::UShort);
00544 TAO_Export void operator<<= (CORBA_Any_var &,
00545                              CORBA::Long);
00546 TAO_Export void operator<<= (CORBA_Any_var &,
00547                              CORBA::ULong);
00548 TAO_Export void operator<<= (CORBA_Any_var &,
00549                              CORBA::LongLong);
00550 TAO_Export void operator<<= (CORBA_Any_var &,
00551                              CORBA::ULongLong);
00552 TAO_Export void operator<<= (CORBA_Any_var &,
00553                              CORBA::Float);
00554 TAO_Export void operator<<= (CORBA_Any_var &,
00555                              CORBA::Double);
00556 TAO_Export void operator<<= (CORBA_Any_var &,
00557                              const CORBA_Any&);
00558 TAO_Export void operator<<= (CORBA_Any_var &,
00559                              const char*);
00560 TAO_Export void operator<<= (CORBA_Any_var &,
00561                              CORBA::TypeCode_ptr);
00562 TAO_Export void operator<<= (CORBA_Any_var &,
00563                              const CORBA::Object_ptr);
00564 TAO_Export void operator<<= (CORBA_Any_var &,
00565                              CORBA::Any::from_boolean);
00566 TAO_Export void operator<<= (CORBA_Any_var &,
00567                              CORBA::Any::from_char);
00568 TAO_Export void operator<<= (CORBA_Any_var &,
00569                              CORBA::Any::from_wchar);
00570 TAO_Export void operator<<= (CORBA_Any_var &,
00571                              CORBA::Any::from_octet);
00572 TAO_Export void operator<<= (CORBA_Any_var &,
00573                              CORBA::Any::from_string);
00574 TAO_Export void operator<<= (CORBA_Any_var &,
00575                              CORBA::Any::from_wstring);
00576 
00577 // These are not required by the spec, but will make users
00578 // of other ORBs that are used to them more comfortable.
00579 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00580                                        CORBA::Short&);
00581 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00582                                        CORBA::UShort&);
00583 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00584                                        CORBA::Long&);
00585 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00586                                        CORBA::ULong&);
00587 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00588                                        CORBA::LongLong&);
00589 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00590                                        CORBA::ULongLong&);
00591 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00592                                        CORBA::Float&);
00593 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00594                                        CORBA::Double&);
00595 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00596                                        CORBA_Any&);
00597 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00598                                        CORBA::TypeCode_ptr&);
00599 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00600                                        const char*&);
00601 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00602                                        const CORBA::WChar*&);
00603 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00604                                        CORBA::Any::to_boolean);
00605 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00606                                        CORBA::Any::to_octet);
00607 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00608                                        CORBA::Any::to_char);
00609 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00610                                        CORBA::Any::to_wchar);
00611 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00612                                        CORBA::Any::to_string);
00613 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00614                                        CORBA::Any::to_wstring);
00615 TAO_Export CORBA::Boolean operator>>= (CORBA_Any_var &,
00616                                        CORBA::Any::to_object);
00617 
00618 #endif /* __ACE_INLINE__ */
00619 
00620 #include "ace/post.h"
00621 #endif /* TAO_ANY_H */

Generated on Mon Jun 16 13:47:59 2003 for TAO by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002