Document return values
[ACE_TAO.git] / ACE / ace / Managed_Object.h
blobd18863842af6ecec52c190efb1e0d4ba37849b72
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Managed_Object.h
7 * @author David L. Levine <levine@cs.wustl.edu>
8 */
9 //=============================================================================
11 #ifndef ACE_MANAGED_OBJECT_H
12 #define ACE_MANAGED_OBJECT_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/config-all.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 # pragma once
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/Object_Manager.h"
23 #include "ace/Global_Macros.h"
25 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
27 /**
28 * @class ACE_Cleanup_Adapter
30 * @brief Adapter for ACE_Cleanup objects that allows them to be readily
31 * managed by the ACE_Object_Manager.
33 * This template class adapts an object of any type to be an
34 * ACE_Cleanup object. The object can then be destroyed
35 * type-safely by the ACE_Object_Manager. This class is
36 * typically used to replace a cast; but, it's a bit cleaner and
37 * allows insertion of, say, run-time type identification
38 * internally if desired.
40 template <class TYPE>
41 class ACE_Cleanup_Adapter : public ACE_Cleanup
43 public:
44 /// Default constructor.
45 ACE_Cleanup_Adapter ();
47 /// Virtual destructor, needed by some compilers for vtable placement.
48 virtual ~ACE_Cleanup_Adapter ();
50 /// Accessor for contained object.
51 TYPE &object ();
53 private:
54 ACE_Cleanup_Adapter (const ACE_Cleanup_Adapter<TYPE> &) = delete;
55 void operator= (const ACE_Cleanup_Adapter<TYPE> &) = delete;
57 /// Contained object.
58 TYPE object_;
61 /**
62 * @class ACE_Managed_Object
64 * @brief Wrapper for interface to allocate an object managed by the
65 * ACE_Object_Manager.
67 * This template class wraps an interface that is used to
68 * allocate and access an object that is managed by the
69 * ACE_Object_Manager. Because static template member functions
70 * are not supported by some compilers, it is a separate
71 * (template) class.
72 * This interface is typically used to replace a static object
73 * with one that is dynamically allocated. It helps to avoid
74 * problems with order of static object
75 * construction/destruction. Managed objects won't be allocated
76 * until needed, but should be allocated when first needed. And
77 * they are destroyed in the reverse order of construction.
78 * <get_preallocated_object> accesses a "preallocated" object,
79 * i.e., one that is identified by a value in the
80 * ACE_Object_Manager:: Preallocated_Object enum. These objects
81 * are used internally by the ACE library.
82 * Hooks are provided for the application to preallocate objects
83 * via the same mechanism.
84 * ACE_APPLICATION_PREALLOCATED_OBJECT_DECLARATIONS can be used
85 * to define enum values;
86 * ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS can be used
87 * to define the corresponding objects. The format of the ACE
88 * internal library definitions should be followed. And
89 * similarly, ACE_APPLICATION_PREALLOCATED_ARRAY_DECLARATIONS
90 * and ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS can be
91 * used to preallocate arrays.
92 * By default, preallocation uses dynamic allocation. The
93 * preallocated objects and arrays are allocated off the heap in
94 * the ACE_Object_Manager constructor. To statically place the
95 * preallocated objects in program global data instead of on the
96 * heap, #define ACE_HAS_STATIC_PREALLOCATION prior to building
97 * the ACE library.
99 template <class TYPE>
100 class ACE_Managed_Object
102 public:
103 /// Get the preallocated object identified by "id". Returns a
104 /// pointer to the object. Beware: no error indication is provided,
105 /// because it can _only_ be used for accessing preallocated objects.
106 static TYPE *get_preallocated_object (ACE_Object_Manager::Preallocated_Object identifier)
108 // The preallocated objects are in a separate, "read-only" array so
109 // that this function doesn't need a lock. Also, because it is
110 // intended _only_ for use with hard-code values, it performs no
111 // range checking on "id".
113 // Cast the return type of the the object pointer based
114 // on the type of the function template parameter.
115 return &((ACE_Cleanup_Adapter<TYPE> *)
116 ACE_Object_Manager::preallocated_object[identifier])->object ();
118 /// Get the preallocated array identified by "id". Returns a
119 /// pointer to the array. Beware: no error indication is provided,
120 /// because it can _only_ be used for accessing preallocated arrays.
121 static TYPE *get_preallocated_array (ACE_Object_Manager::Preallocated_Array identifier)
123 // The preallocated array are in a separate, "read-only" array so
124 // that this function doesn't need a lock. Also, because it is
125 // intended _only_ for use with hard-code values, it performs no
126 // range checking on "id".
128 // Cast the return type of the the object pointer based
129 // on the type of the function template parameter.
130 return &((ACE_Cleanup_Adapter<TYPE> *)
131 ACE_Object_Manager::preallocated_array[identifier])->object ();
134 protected:
135 // Disallow instantiation of this class.
136 ACE_Managed_Object () = delete;
138 private:
139 ACE_Managed_Object (const ACE_Managed_Object<TYPE> &) = delete;
140 void operator= (const ACE_Managed_Object<TYPE> &) = delete;
143 ACE_END_VERSIONED_NAMESPACE_DECL
145 #if defined (__ACE_INLINE__)
146 #include "ace/Managed_Object.inl"
147 #endif /* __ACE_INLINE__ */
149 #include "ace/Managed_Object.cpp"
151 #include /**/ "ace/post.h"
153 #endif /* ACE_MANAGED_OBJECT_H */