util/objman.cpp

Go to the documentation of this file.
00001 
00002 #include <util/xmlBlasterDef.h>
00003 #include <util/objman.h>
00004 #include <util/Global.h>
00005 #include <cstdlib> //<stdlib.h>
00006 
00007 using namespace std;
00008 using namespace org::xmlBlaster::client::protocol;
00009 using namespace org::xmlBlaster::util::dispatch;
00010 using namespace org::xmlBlaster::util;
00011 
00012 //static int gObjectManagerState = 0;
00013 // Cleanup routine for atexit
00014 extern "C" void object_manager_cleanup()
00015    { 
00016         /*
00017      if(gObjectManagerState != Object_Lifetime_Manager_Base::OBJ_MAN_SHUT_DOWN &&
00018         gObjectManagerState != Object_Lifetime_Manager_Base::OBJ_MAN_SHUTTING_DOWN)
00019        ;//Object_Lifetime_Manager::instance()->fini();
00020         */
00021    }
00022 
00023 namespace org { namespace xmlBlaster { namespace util {
00024 
00025 #define PREALLOCATE_OBJECT(TYPE, ID)\
00026 {\
00027   TYPE* t = new TYPE;\
00028   ManagedObject<TYPE> *obj_p;\
00029   obj_p = new  ManagedObject<TYPE>(t);\
00030   preallocated_object[ID] = obj_p;\
00031 }
00032 
00033 //namespace org { namespace xmlBlaster { namespace util {
00034 
00035 // Static declarations for Object_Lifetime_Manager.
00036 Object_Lifetime_Manager * Object_Lifetime_Manager::instance_ = 0;
00037 ManagedMap              Object_Lifetime_Manager::managedObjectMap_;
00038 ManagedList             Object_Lifetime_Manager::managedObjectList_;
00039 void *                  Object_Lifetime_Manager::preallocated_object[PREALLOCATED_OBJECTS];
00040 
00041 
00042 void Object_Lifetime_Manager::init (void)
00043 {
00044    Object_Lifetime_Manager::instance()->startup();
00045 }
00046 
00047 void Object_Lifetime_Manager::fini (void)
00048 {
00049    Object_Lifetime_Manager::instance()->shutdown();
00050 }
00051 
00058 Object_Lifetime_Manager * Object_Lifetime_Manager::instance (void)
00059 {
00060   if (instance_ == 0) 
00061   {
00062     Object_Lifetime_Manager *instance_pointer =
00063     new Object_Lifetime_Manager;
00064     
00065     instance_pointer->dynamically_allocated_ = 1;
00066   }
00067   return instance_;
00068 }
00069 
00070 int Object_Lifetime_Manager::startup ()
00071 {
00072   if (starting_up_i ()) 
00073   {
00074     // First, indicate that this
00075     // Object_Lifetime_Manager instance
00076     // is being initialized.
00077     object_manager_state_ = OBJ_MAN_INITIALIZING;
00078     if (this == instance_) 
00079     {      
00080       // Create Global as part of ObjectManager startup.
00081       PREALLOCATE_OBJECT(Global, XMLBLASTER_GLOBAL);
00082       object_manager_state_ = OBJ_MAN_INITIALIZED;    
00083     }
00084     return 0;
00085   } 
00086   else 
00087   {
00088     // Hads already initialized.
00089     return 1;
00090   }
00091 }
00092 
00093 int Object_Lifetime_Manager::shutdown ()
00094 {
00095    if (shutting_down_i())
00096       // Too late. Or, maybe too early. Either
00097       // <fini> has already been called, or
00098       // <init> was never called.
00099       return object_manager_state_ == OBJ_MAN_SHUT_DOWN ? 1 : -1;
00100 
00101    // Indicate that the Object_Lifetime_Manager
00102    // instance is being shut down.
00103    // This object manager should be the last one
00104    // to be shut down.
00105    /*gObjectManagerState = */object_manager_state_ = OBJ_MAN_SHUTTING_DOWN;
00106 
00107    // Only clean up Pre-allocated Objects when
00108    // the singleton Instance is being destroyed.
00109    // The sequence is reversed, the last created object is destroyed first.
00110    if (this == instance_) {
00111       ManagedList::reverse_iterator i;
00112       for(i = managedObjectList_.rbegin(); i != managedObjectList_.rend(); ++i) {
00113          Cleanup_Adaptor* adap = *i;
00114          adap->cleanup();
00115          delete adap;
00116       }
00117       managedObjectList_.clear();
00118 
00119       managedObjectMap_.clear();
00120 
00121       // Delete the Global post of list method as a dependency may exist
00122       // between objects existing in both constructs.
00123       // Array is last as all objects placed here are intentional and
00124       // it is assumed user is aware of such semantics.
00125       delete (Cleanup_Adaptor*)preallocated_object[XMLBLASTER_GLOBAL];
00126    }
00127 
00128    // Indicate that this Object_Lifetime_Manager
00129    // instance has been shut down.
00130    /*gObjectManagerState = */object_manager_state_ = OBJ_MAN_SHUT_DOWN;
00131    if (dynamically_allocated_)
00132       delete this;
00133    if (this == instance_)
00134       instance_ = 0;
00135    return 0;
00136 }
00137 
00138 }}} // namespace