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

Managed_Object.h

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

Generated on Mon Jun 16 11:20:07 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002