freSessionComponentCache.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freSessionComponentCache.cxx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 #include "freSessionComponentCache.h"
00023 
00024 #include "freControllerBase.h"
00025 #include "freSetup.h"
00026 
00027 namespace FREE
00028 {
00029 
00033 
00034 
00035 SessionComponentCache::GenericComponentPointer
00036 SessionComponentCache::
00037 Component() const
00038 {
00039   if (ControllerIsAssigned())
00040   { //if there is a controller the component should be returned by the controller, thus
00041     //the controller can react to the request (e.g. actualize the component) before it
00042     //is used
00043     GenericComponentPointer smpComponent = m_pComponentController->GetGenericComponent(this);
00044     return smpComponent;
00045   }
00046   return m_smpComponent;
00047 };
00048 
00049 void
00050 SessionComponentCache::
00051 SetComponent(GenericComponentType* pComponent)
00052 {
00053   if (ControllerIsAssigned())
00054   { //if there is a controller the component should be set by the controller, thus
00055     //the controller can react to the new component (e.g. store a media to hard disc)
00056     m_pComponentController->SetGenericComponent(pComponent, this);
00057   }
00058   else m_smpComponent = pComponent;
00059 };
00060 
00061 bool
00062 SessionComponentCache::
00063 IsActive() const
00064 {
00065         return m_smpComponent.IsNotNull() && (m_pComponentController!=0);
00066 }; 
00067 
00068 bool
00069 SessionComponentCache::
00070 IsPassiveCache() const
00071 {
00072   return m_bPassive;
00073 }; 
00074 
00075 ComponentSetup*
00076 SessionComponentCache::
00077 Setup() const
00078 {
00079   if (m_smpComponentSetup.IsNotNull()) return m_smpComponentSetup;
00080   
00081   throwExceptionMacro("Setup is not assigned.");
00082 };
00083 
00084 const GenericComponentController*
00085 SessionComponentCache::
00086 Controller() const
00087 {
00088   if (m_pComponentController) return m_pComponentController;
00089   
00090   throwExceptionMacro("Controller is not assigned.");
00091 };
00092 
00093 std::string
00094 SessionComponentCache::
00095 GetControllerID() const
00096 {
00097   return Controller()->ControllerID();
00098 };
00099 
00100 const ComponentID&
00101 SessionComponentCache::
00102 GetComponentID() const
00103 {
00104   return Setup()->GetComponentID();
00105 };
00106 
00107 bool
00108 SessionComponentCache::
00109 HasParentCache() const
00110 {
00111   return m_pParent!=NULL;
00112 };
00113 
00114 const SessionComponentCache*
00115 SessionComponentCache::
00116 GetParentCache() const
00117 { return m_pParent; };
00118 
00119 SessionComponentCache*
00120 SessionComponentCache::
00121 GetParentCache()
00122 { return m_pParent; };
00123 
00124 const SessionComponentCache*
00125 SessionComponentCache::
00126 GetRootCache() const
00127 { 
00128   if (m_pParent) return m_pParent->GetRootCache();
00129   return this;
00130 };
00131 
00132 SessionComponentCache*
00133 SessionComponentCache::
00134 GetRootCache()
00135 { 
00136   if (m_pParent) return m_pParent->GetRootCache();
00137   return this;
00138 };
00139 
00140 SessionComponentCache*
00141 SessionComponentCache::
00142 GetCacheByIDPath(const IDPath& idPath)
00143 {
00144   //Use the const variant of the method and cast the result to non const.
00145   const SessionComponentCache* pConstCache = this->GetConstCacheByIDPath(idPath);
00146 
00147   SessionComponentCache* result = 0;
00148 
00149   if (pConstCache) result = const_cast<SessionComponentCache*>(pConstCache);
00150 
00151   return result;
00152 };
00153 
00154 const SessionComponentCache*
00155 SessionComponentCache::
00156 GetConstCacheByIDPath(const IDPath& idPath) const
00157 {
00158   if (idPath.IsEmpty()) return this;
00159 
00160   //if not dispatch passed path
00161   std::string sTag;
00162   IDPath::PathElementType token = idPath.GetFirstComponent(sTag);
00163 
00164   //This is like self reference by default, hence it won't be checked in the switch statement
00165         //It also covers the case of media or parameters as path elements: they will be skipped while
00166         //searching for a component.
00167   IDPath nextPath = idPath.GetSubIDPath();
00168   const SessionComponentCache* pNext = this;
00169 
00170   if (idPath.IsAbsolute())
00171   {
00172     pNext = GetRootCache();
00173     nextPath = idPath;
00174     nextPath.SetAbsolute(false);
00175   }
00176   else
00177   {
00178     switch(token)
00179     {
00180     case IDPath::PTAnyDescendant: //not allowed
00181       throwExceptionMacro("IDPath must be well defined. No Wildcards of any kind (*, //) are allowed. IDPath: " << idPath.ToStr());
00182     case IDPath::PTParent: //addresses parent
00183       pNext = GetParentCache(); break;
00184     case IDPath::PTAnyChild: //not allowed
00185       throwExceptionMacro("IDPath must be well defined. No Wildcards of any kind (*, //) are allowed. IDPath: " << idPath.ToStr());
00186     case IDPath::PTComponent: //not allowed
00187       pNext = m_SubCaches.GetElement(sTag); break;
00188     case IDPath::PTNULL:
00189       throwExceptionMacro("IDPath is invalid, contains undefined path element . IDPath: " << idPath.ToStr());
00190     }
00191   }
00192 
00193         if (!pNext) return NULL;
00194   return pNext->GetConstCacheByIDPath(nextPath);
00195 };
00196 
00197 IDPath
00198 SessionComponentCache::
00199 GetIDPath() const
00200 {
00201   try
00202   {
00203     return Setup()->GetIDPath();
00204   }
00205   catchAllNPassMacro("Error while retrieving the IDPath of the assigned setup.")
00206 };
00207 
00208 
00209 SessionComponentCache::
00210 SessionComponentCache(SessionComponentCache* pParentCache, ComponentSetup* pSetup,
00211                       const GenericComponentController* pController, GenericComponentType* pComponent,
00212                       bool bIsPassive)
00213 {
00214   m_smpComponent = pComponent;
00215   m_pParent = pParentCache;
00216   m_smpComponentSetup = pSetup;
00217   m_pComponentController = pController;
00218   m_bPassive = bIsPassive;
00219 };
00220 
00221 SessionComponentCache::
00222 SessionComponentCache()
00223 {
00224   m_smpComponent = 0;
00225   m_pParent = 0;
00226   m_smpComponentSetup = 0;
00227   m_pComponentController = 0;
00228   m_bPassive = false;
00229 };
00230 
00231 SessionComponentCache::Pointer
00232 SessionComponentCache::
00233 New(SessionComponentCache* pParentCache, ComponentSetup* pSetup, const GenericComponentController* pController, GenericComponentType* pComponent, bool bIsPassive)
00234 {
00235   Pointer smartPtr;
00236   SessionComponentCache *rawPtr = ::itk::ObjectFactory<SessionComponentCache>::Create();
00237   if(rawPtr == NULL)
00238   {
00239     rawPtr = new SessionComponentCache(pParentCache,pSetup,pController,pComponent,bIsPassive);
00240   }
00241   smartPtr = rawPtr;
00242   rawPtr->UnRegister();
00243   
00244   return smartPtr;
00245 };
00246 
00247 SessionComponentCache&
00248 SessionComponentCache::
00249 operator = (const SessionComponentCache& rSessionComponentCache)
00250 {
00251   if (this == &rSessionComponentCache) return *this;
00252 
00253   m_smpComponentSetup = rSessionComponentCache.Setup();
00254   m_pComponentController = rSessionComponentCache.Controller();
00255   m_SubCaches = rSessionComponentCache.SubCaches();
00256   m_smpComponent = rSessionComponentCache.Component();
00257 
00258   return *this;
00259 };
00260 
00261 SessionComponentCache::
00262 ~SessionComponentCache()
00263 {
00264 };
00265 
00266 
00267 
00271 
00272 SessionComponentCache::GenericComponentType*
00273 DirectSessionComponentAccessor::
00274 GetComponent(const SessionComponentCache* pCache)
00275 {
00276   if (!pCache) throwStaticExceptionMacro("Error; cannot get component, cache pointer is NULL.");
00277 
00278   return pCache->m_smpComponent.GetPointer();
00279 };
00280 
00281 void
00282 DirectSessionComponentAccessor::
00283 SetComponent(SessionComponentCache::GenericComponentType* pComponent, SessionComponentCache* pCache)
00284 {
00285   if (!pCache) throwStaticExceptionMacro("Error; cannot get component, cache pointer is NULL.");
00286 
00287   pCache->m_smpComponent = pComponent;
00288 };
00289 
00290 SessionComponentCache::CacheRepositoryType&
00291 DirectSessionComponentAccessor::
00292 GetRepository(SessionComponentCache* pCache)
00293 {
00294   if (!pCache) throwStaticExceptionMacro("Error; cannot return repository, cache pointer is NULL.");
00295 
00296   return pCache->m_Repository;
00297 };
00298 
00299 const SessionComponentCache::CacheRepositoryType&
00300 DirectSessionComponentAccessor::
00301 GetRepository(const SessionComponentCache* pCache)
00302 {
00303   if (!pCache) throwStaticExceptionMacro("Error; cannot return repository, cache pointer is NULL.");
00304 
00305   return pCache->m_Repository;
00306 };
00307 
00308 SessionComponentCache::GenericComponentType*
00309 DirectSessionComponentAccessor::
00310 GetRepositoryElement(const std::string& sElementKey, const SessionComponentCache* pCache)
00311 {
00312   if (!pCache) throwStaticExceptionMacro("Error; cannot get repository element, cache pointer is NULL.");
00313 
00314   SessionComponentCache::CacheRepositoryType::const_iterator pos = pCache->m_Repository.find(sElementKey);
00315   if (pos!=pCache->m_Repository.end()) return pos->second.GetPointer();
00316 
00317   return NULL;
00318 };
00319 
00320 void
00321 DirectSessionComponentAccessor::
00322 SetRepositoryElement(const std::string& sElementKey, SessionComponentCache::GenericComponentType* pElement, SessionComponentCache* pCache)
00323 {
00324   if (!pCache) throwStaticExceptionMacro("Error; cannot set repository element, cache pointer is NULL.");
00325 
00326   typedef std::pair< std::string, SessionComponentCache::GenericComponentPointer > ValueType;
00327   typedef std::pair< SessionComponentCache::CacheRepositoryType::iterator, bool > PositionType;
00328 
00329   SessionComponentCache::GenericComponentPointer smpElement = pElement;
00330   PositionType pos = pCache->m_Repository.insert( ValueType(sElementKey, smpElement));
00331 
00332   if (pos.second == false)
00333   { //Key already exists so replace component
00334     (pos.first)->second = pElement;
00335   }
00336 };
00337 
00338 void
00339 DirectSessionComponentAccessor::
00340 DeleteRepositoryElement(const std::string& sElementKey, SessionComponentCache* pCache)
00341 {
00342   pCache->m_Repository.erase(sElementKey);
00343 };
00344 
00345 
00346 
00350 
00351 std::string
00352 SessionComponentRepositoryKeys::
00353 ParameterValidity(const std::string& sParameterName)
00354 {
00355   return "ParameterValidityTag_"+sParameterName;
00356 };
00357 
00358 std::string
00359 SessionComponentRepositoryKeys::
00360 MediaValidity(const MediaID& id)
00361 {
00362   return "MediaValidityTag_"+id;
00363 };
00364 
00365 std::string
00366 SessionComponentRepositoryKeys::
00367 Validity(const std::string& id)
00368 {
00369   return "ValidityTag_"+id;
00370 };
00371 
00372 }//end of namespace FREE

Generated at Sat Oct 13 17:27:23 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000