Framework_Component.cpp

Go to the documentation of this file.
00001 // Framework_Component.cpp
00002 // $Id: Framework_Component.cpp 80826 2008-03-04 14:51:23Z wotte $
00003 
00004 #include "ace/Framework_Component.h"
00005 
00006 #if !defined (__ACE_INLINE__)
00007 #include "ace/Framework_Component.inl"
00008 #endif /* __ACE_INLINE__ */
00009 
00010 #include "ace/Object_Manager.h"
00011 #include "ace/Log_Msg.h"
00012 #include "ace/DLL_Manager.h"
00013 #include "ace/Recursive_Thread_Mutex.h"
00014 #include "ace/OS_NS_string.h"
00015 
00016 ACE_RCSID(ace, Framework_Component, "$Id: Framework_Component.cpp 80826 2008-03-04 14:51:23Z wotte $")
00017 
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019 
00020 ACE_Framework_Component::~ACE_Framework_Component (void)
00021 {
00022   ACE_TRACE ("ACE_Framework_Component::~ACE_Framework_Component");
00023 
00024   ACE::strdelete (const_cast<ACE_TCHAR*> (this->dll_name_));
00025   ACE::strdelete (const_cast<ACE_TCHAR*> (this->name_));
00026 }
00027 
00028 /***************************************************************/
00029 
00030 ACE_ALLOC_HOOK_DEFINE(ACE_Framework_Repository)
00031 
00032 sig_atomic_t ACE_Framework_Repository::shutting_down_ = 0;
00033 
00034 // Pointer to the Singleton instance.
00035 ACE_Framework_Repository *ACE_Framework_Repository::repository_ = 0;
00036 
00037 ACE_Framework_Repository::~ACE_Framework_Repository (void)
00038 {
00039   ACE_TRACE ("ACE_Framework_Repository::~ACE_Framework_Repository");
00040   this->close ();
00041 }
00042 
00043 int
00044 ACE_Framework_Repository::open (int size)
00045 {
00046   ACE_TRACE ("ACE_Framework_Repository::open");
00047 
00048   ACE_Framework_Component **temp = 0;
00049 
00050   ACE_NEW_RETURN (temp,
00051                   ACE_Framework_Component *[size],
00052                   -1);
00053 
00054   this->component_vector_ = temp;
00055   this->total_size_ = size;
00056   return 0;
00057 }
00058 
00059 int
00060 ACE_Framework_Repository::close (void)
00061 {
00062   ACE_TRACE ("ACE_Framework_Repository::close");
00063   ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
00064 
00065   this->shutting_down_ = 1;
00066 
00067   if (this->component_vector_ != 0)
00068     {
00069       // Delete components in reverse order.
00070       for (int i = this->current_size_ - 1; i >= 0; i--)
00071         if (this->component_vector_[i])
00072           {
00073             ACE_Framework_Component *s =
00074               const_cast<ACE_Framework_Component *> (
00075                 this->component_vector_[i]);
00076 
00077             this->component_vector_[i] = 0;
00078             delete s;
00079           }
00080 
00081       delete [] this->component_vector_;
00082       this->component_vector_ = 0;
00083       this->current_size_ = 0;
00084     }
00085 
00086   ACE_DLL_Manager::close_singleton ();
00087   return 0;
00088 }
00089 
00090 ACE_Framework_Repository *
00091 ACE_Framework_Repository::instance (int size)
00092 {
00093   ACE_TRACE ("ACE_Framework_Repository::instance");
00094 
00095   if (ACE_Framework_Repository::repository_ == 0)
00096     {
00097       // Perform Double-Checked Locking Optimization.
00098       ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00099                                 *ACE_Static_Object_Lock::instance (), 0));
00100       if (ACE_Framework_Repository::repository_ == 0)
00101         {
00102           if (ACE_Object_Manager::starting_up () ||
00103               !ACE_Object_Manager::shutting_down ())
00104             {
00105               ACE_NEW_RETURN (ACE_Framework_Repository::repository_,
00106                               ACE_Framework_Repository (size),
00107                               0);
00108             }
00109         }
00110     }
00111 
00112   return ACE_Framework_Repository::repository_;
00113 }
00114 
00115 void
00116 ACE_Framework_Repository::close_singleton (void)
00117 {
00118   ACE_TRACE ("ACE_Framework_Repository::close_singleton");
00119 
00120   ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
00121                      *ACE_Static_Object_Lock::instance ()));
00122 
00123   delete ACE_Framework_Repository::repository_;
00124   ACE_Framework_Repository::repository_ = 0;
00125 }
00126 
00127 int
00128 ACE_Framework_Repository::register_component (ACE_Framework_Component *fc)
00129 {
00130   ACE_TRACE ("ACE_Framework_Repository::register_component");
00131   ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
00132   int i;
00133 
00134   // Check to see if it's already registered
00135   for (i = 0; i < this->current_size_; i++)
00136     if (this->component_vector_[i] &&
00137         fc->this_ == this->component_vector_[i]->this_)
00138       {
00139         ACE_ERROR_RETURN ((LM_ERROR,
00140                            ACE_TEXT ("AFR::register_component: error, ")
00141                            ACE_TEXT ("compenent already registered\n")),
00142                           -1);
00143       }
00144 
00145   if (i < this->total_size_)
00146     {
00147       this->component_vector_[i] = fc;
00148       this->current_size_++;
00149       return 0;
00150     }
00151 
00152   return -1;
00153 }
00154 
00155 int
00156 ACE_Framework_Repository::remove_component (const ACE_TCHAR *name)
00157 {
00158   ACE_TRACE ("ACE_Framework_Repository::remove_component");
00159   ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
00160   int i;
00161 
00162   for (i = 0; i < this->current_size_; i++)
00163     if (this->component_vector_[i] &&
00164         ACE_OS::strcmp (this->component_vector_[i]->name_, name) == 0)
00165       {
00166         delete this->component_vector_[i];
00167         this->component_vector_[i] = 0;
00168         this->compact ();
00169         return 0;
00170       }
00171 
00172   return -1;
00173 }
00174 
00175 int
00176 ACE_Framework_Repository::remove_dll_components (const ACE_TCHAR *dll_name)
00177 {
00178   ACE_TRACE ("ACE_Framework_Repository::remove_dll_components");
00179 
00180   if (this->shutting_down_)
00181     return this->remove_dll_components_i (dll_name);
00182   ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
00183 
00184   return this->remove_dll_components_i (dll_name);
00185 }
00186 
00187 int
00188 ACE_Framework_Repository::remove_dll_components_i (const ACE_TCHAR *dll_name)
00189 {
00190   ACE_TRACE ("ACE_Framework_Repository::remove_dll_components_i");
00191 
00192   int i;
00193   int retval = -1;
00194 
00195   for (i = 0; i < this->current_size_; i++)
00196     if (this->component_vector_[i] &&
00197         ACE_OS::strcmp (this->component_vector_[i]->dll_name_, dll_name) == 0)
00198       {
00199           if (ACE::debug ())
00200             ACE_DEBUG ((LM_DEBUG,
00201                         ACE_TEXT ("AFR::remove_dll_components_i (%s) ")
00202                         ACE_TEXT ("component \"%s\"\n"),
00203                         dll_name, this->component_vector_[i]->name_));
00204         delete this->component_vector_[i];
00205         this->component_vector_[i] = 0;
00206         ++retval;
00207       }
00208 
00209   this->compact ();
00210 
00211   return retval == -1 ? -1 : 0;
00212 }
00213 
00214 void
00215 ACE_Framework_Repository::compact (void)
00216 {
00217   ACE_TRACE ("ACE_Framework_Repository::compact");
00218 
00219   int i;
00220   int start_hole;
00221   int end_hole;
00222 
00223   do
00224     {
00225       start_hole = this->current_size_;
00226       end_hole = this->current_size_;
00227 
00228       // Find hole
00229       for (i = 0; i < this->current_size_; ++i)
00230         {
00231           if (this->component_vector_[i] == 0)
00232             {
00233               if (start_hole == this->current_size_)
00234                 {
00235                   start_hole = i;
00236                   end_hole = i;
00237                 }
00238               else
00239                 end_hole = i;
00240             }
00241           else if (end_hole != this->current_size_)
00242             break;
00243         }
00244 
00245       if (start_hole != this->current_size_)
00246         {
00247           // move the contents and reset current_size_
00248           while (end_hole + 1 < this->current_size_)
00249             {
00250               this->component_vector_[start_hole++] =
00251                 this->component_vector_[++end_hole];
00252             }
00253           // Since start_hole is now one past the last
00254           // active slot.
00255           this->current_size_ = start_hole;
00256         }
00257 
00258     } while (start_hole != this->current_size_);
00259 }
00260 
00261 void
00262 ACE_Framework_Repository::dump (void) const
00263 {
00264 #if defined (ACE_HAS_DUMP)
00265   ACE_TRACE ("ACE_Framework_Repository::dump");
00266 #endif /* ACE_HAS_DUMP */
00267 }
00268 
00269 ACE_Framework_Repository::ACE_Framework_Repository (int size)
00270   : current_size_ (0)
00271 {
00272   ACE_TRACE ("ACE_Framework_Repository::ACE_Framework_Repository");
00273 
00274   if (this->open (size) == -1)
00275     ACE_ERROR ((LM_ERROR,
00276                 ACE_TEXT ("%p\n"),
00277                 ACE_TEXT ("ACE_Framework_Repository")));
00278 }
00279 
00280 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:39 2010 for ACE by  doxygen 1.4.7