00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Managed_Object.h 00006 * 00007 * Managed_Object.h,v 4.35 2006/01/25 19:53:55 jwillemsen Exp 00008 * 00009 * @author David L. Levine <levine@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_MANAGED_OBJECT_H 00014 #define ACE_MANAGED_OBJECT_H 00015 00016 #include /**/ "ace/pre.h" 00017 00018 #include "ace/config-all.h" 00019 00020 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00021 # pragma once 00022 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00023 00024 #include "ace/Object_Manager.h" 00025 #include "ace/Global_Macros.h" 00026 00027 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00028 00029 /** 00030 * @class ACE_Cleanup_Adapter 00031 * 00032 * @brief Adapter for ACE_Cleanup objects that allows them to be readily 00033 * managed by the ACE_Object_Manager. 00034 * 00035 * This template class adapts an object of any type to be an 00036 * ACE_Cleanup object. The object can then be destroyed 00037 * type-safely by the ACE_Object_Manager. This class is 00038 * typically used to replace a cast; but, it's a bit cleaner and 00039 * allows insertion of, say, run-time type identification 00040 * internally if desired. 00041 */ 00042 template <class TYPE> 00043 class ACE_Cleanup_Adapter : public ACE_Cleanup 00044 { 00045 public: 00046 /// Default constructor. 00047 ACE_Cleanup_Adapter (void); 00048 00049 /// Virtual destructor, needed by some compilers for vtable placement. 00050 virtual ~ACE_Cleanup_Adapter (void); 00051 00052 /// Accessor for contained object. 00053 TYPE &object (void); 00054 00055 private: 00056 ACE_UNIMPLEMENTED_FUNC (ACE_Cleanup_Adapter (const ACE_Cleanup_Adapter<TYPE> &)) 00057 ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Cleanup_Adapter<TYPE> &)) 00058 00059 /// Contained object. 00060 TYPE object_; 00061 }; 00062 00063 /** 00064 * @class ACE_Managed_Object 00065 * 00066 * @brief Wrapper for interface to allocate an object managed by the 00067 * ACE_Object_Manager. 00068 * 00069 * This template class wraps an interface that is used to 00070 * allocate and access an object that is managed by the 00071 * ACE_Object_Manager. Because static template member functions 00072 * are not supported by some compilers, it is a separate 00073 * (template) class. 00074 * This interface is typically used to replace a static object 00075 * with one that is dynamically allocated. It helps to avoid 00076 * problems with order of static object 00077 * construction/destruction. Managed objects won't be allocated 00078 * until needed, but should be allocated when first needed. And 00079 * they are destroyed in the reverse order of construction. 00080 * <get_preallocated_object> accesses a "preallocated" object, 00081 * i.e., one that is identified by a value in the 00082 * ACE_Object_Manager:: Preallocated_Object enum. These objects 00083 * are used internally by the ACE library. 00084 * Hooks are provided for the application to preallocate objects 00085 * via the same mechanism. 00086 * ACE_APPLICATION_PREALLOCATED_OBJECT_DECLARATIONS can be used 00087 * to define enum values; 00088 * ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS can be used 00089 * to define the corresponding objects. The format of the ACE 00090 * internal library definitions should be followed. And 00091 * similarly, ACE_APPLICATION_PREALLOCATED_ARRAY_DECLARATIONS 00092 * and ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS can be 00093 * used to preallocate arrays. 00094 * By default, preallocation uses dynamic allocation. The 00095 * preallocated objects and arrays are allocated off the heap in 00096 * the ACE_Object_Manager constructor. To statically place the 00097 * preallocated objects in program global data instead of on the 00098 * heap, #define ACE_HAS_STATIC_PREALLOCATION prior to building 00099 * the ACE library. 00100 */ 00101 template <class TYPE> 00102 class ACE_Managed_Object 00103 { 00104 public: 00105 static TYPE *get_preallocated_object (ACE_Object_Manager::Preallocated_Object identifier) 00106 { 00107 // The preallocated objects are in a separate, "read-only" array so 00108 // that this function doesn't need a lock. Also, because it is 00109 // intended _only_ for use with hard-code values, it performs no 00110 // range checking on "id". 00111 00112 // Cast the return type of the the object pointer based 00113 // on the type of the function template parameter. 00114 return &((ACE_Cleanup_Adapter<TYPE> *) 00115 ACE_Object_Manager::preallocated_object[identifier])->object (); 00116 } 00117 // Get the preallocated object identified by "id". Returns a 00118 // pointer to the object. Beware: no error indication is provided, 00119 // because it can _only_ be used for accessing preallocated objects. 00120 // @note The function definition is inlined here so that it compiles 00121 // on AIX 4.1 w/xlC v. 3.01. 00122 00123 static TYPE *get_preallocated_array (ACE_Object_Manager::Preallocated_Array identifier) 00124 { 00125 // The preallocated array are in a separate, "read-only" array so 00126 // that this function doesn't need a lock. Also, because it is 00127 // intended _only_ for use with hard-code values, it performs no 00128 // range checking on "id". 00129 00130 // Cast the return type of the the object pointer based 00131 // on the type of the function template parameter. 00132 return &((ACE_Cleanup_Adapter<TYPE> *) 00133 ACE_Object_Manager::preallocated_array[identifier])->object (); 00134 } 00135 // Get the preallocated array identified by "id". Returns a 00136 // pointer to the array. Beware: no error indication is provided, 00137 // because it can _only_ be used for accessing preallocated arrays. 00138 // @note The function definition is inlined here so that it compiles 00139 // on AIX 4.1 w/xlC v. 3.01. 00140 00141 protected: 00142 00143 // Disallow instantiation of this class. 00144 ACE_UNIMPLEMENTED_FUNC (ACE_Managed_Object (void)) 00145 00146 private: 00147 00148 ACE_UNIMPLEMENTED_FUNC (ACE_Managed_Object (const ACE_Managed_Object<TYPE> &)) 00149 ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Managed_Object<TYPE> &)) 00150 }; 00151 00152 ACE_END_VERSIONED_NAMESPACE_DECL 00153 00154 #if defined (__ACE_INLINE__) 00155 #include "ace/Managed_Object.inl" 00156 #endif /* __ACE_INLINE__ */ 00157 00158 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00159 #include "ace/Managed_Object.cpp" 00160 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00161 00162 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00163 #pragma implementation ("Managed_Object.cpp") 00164 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00165 00166 #include /**/ "ace/post.h" 00167 00168 #endif /* ACE_MANAGED_OBJECT_H */