cmtkObject.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkObject_h_included_
00034 #define __cmtkObject_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #ifndef NULL
00039 #define NULL 0
00040 #endif
00041 
00045 #define cmtkGetSetMacroObject(t,name) \
00046   protected: t *m_##name; \
00047   public:    void Set##name( t *const v ) { if (v) v->Reference(); if (this->m_##name) this->m_##name->Delete(); this->m_##name = v; } \
00048              t* Get##name() { return this->m_##name; } \
00049              const t* Get##name() const { return this->m_##name; } \
00050              void Get##name( t*& to ) { to = this->m_##name; }
00051 
00055 #define igsClassParameter(type,name) \
00056   public: type name; \
00057              void Set##name( const type v ) { if ( name != v ) this->UpdateModifiedTime(); name = v; } \
00058              type Get##name() const { return name; } \
00059              void Get##name( type& to ) { to = name; }
00060 
00064 #define cmtkClassParameter(type,name) \
00065   public: type m_##name; \
00066              void Set##name( const type v ) { if ( this->m_##name != v ) this->UpdateModifiedTime(); this->m_##name = v; } \
00067              type Get##name() const { return this->m_##name; } \
00068              void Get##name( type& to ) { to = this->m_##name; }
00069 
00073 #define igsClassParameter2Array(type,name) \
00074   protected: type name[2]; \
00075   public:    void Set##name( const type v0, const type v1 ) \
00076                   { if ( (name[0] != v0) || (name[1] != v1) ) this->UpdateModifiedTime(); name[0] = v0; name[1] = v1; } \
00077              void SetByIndex##name( const int axis, const type v ) \
00078                   { if (name[axis] != v) this->UpdateModifiedTime(); name[axis] = v; } \
00079              void Set##name( const type* v ) \
00080                   { if ( (name[0] != v[0]) || (name[1] != v[1]) ) this->UpdateModifiedTime(); name[0] = v[0]; name[1] = v[1]; } \
00081              void Get##name( type &v0, type &v1 ) const \
00082                   { v0 = name[0]; v1 = name[1]; } \
00083              void Get##name( type *const v ) const \
00084                   { v[0] = name[0]; v[1] = name[1]; } \
00085              const type* Get##name() const { return name; } \
00086              type* Get##name() { return name; } \
00087              type Get##name( const int i ) const { return name[i]; }
00088 
00092 #define igsClassParameter3Array(type,name) \
00093   protected: type name[3]; \
00094   public:    void Set##name( const type v0, const type v1, const type v2 ) \
00095                   { if ( (name[0] != v0) || (name[1] != v1) || (name[2] != v2) ) this->UpdateModifiedTime(); name[0] = v0; name[1] = v1; name[2] = v2; } \
00096              void SetByIndex##name( const int axis, const type v ) \
00097                   { if (name[axis] != v) this->UpdateModifiedTime(); name[axis] = v; } \
00098              void Set##name( const type* v ) \
00099                   { if ( (name[0] != v[0]) || (name[1] != v[1]) || (name[2] != v[2]) ) this->UpdateModifiedTime(); name[0] = v[0]; name[1] = v[1]; name[2] = v[2]; } \
00100              void Get##name( type &v0, type &v1, type &v2 ) const \
00101                   { v0 = name[0]; v1 = name[1]; v2 = name[2]; } \
00102              void Get##name( type *const v ) const \
00103                   { v[0] = name[0]; v[1] = name[1]; v[2] = name[2]; } \
00104              const type* Get##name() const { return name; } \
00105              type* Get##name() { return name; } \
00106              type Get##name( const int i ) const { return name[i]; }
00107 
00108 namespace
00109 cmtk
00110 {
00111 
00114 
00122 extern long CurrentTime;
00123 
00128 class Object 
00129 {
00130 public:
00136   virtual int Reference() const 
00137   {
00138     return ++ReferenceCount;
00139   }
00140 
00145   virtual void Delete() 
00146   {
00147     if ( (--ReferenceCount) <= 0 ) delete this;
00148   }
00149   
00153   void SetReferenceCount ( const int referenceCount ) 
00154   {
00155     ReferenceCount = referenceCount;
00156   }
00157   
00160   int GetReferenceCount () const 
00161   { 
00162     return ReferenceCount;
00163   }
00164   
00166   long GetModifiedTime() const
00167   { 
00168     return ModifiedTime;
00169   }
00170   
00172   void UpdateModifiedTime() 
00173   {
00174     ModifiedTime = this->GetCurrentTime(); 
00175   }
00176   
00181   void UpdateModifiedTime( long modifiedTime ) 
00182   { 
00183     if ( modifiedTime > ModifiedTime ) 
00184       ModifiedTime = modifiedTime;
00185   }
00186   
00201   template<class C> bool ReplaceObject( C*& to, C *const from ) 
00202   {
00203     if ( from == to ) return false;
00204     if ( from ) from->Reference();    
00205     if ( to ) to->Delete();
00206     to = from;
00207     this->UpdateModifiedTime();
00208     return true;
00209   }
00210 
00214   Object() 
00215   { 
00216     ReferenceCount = 1; ModifiedTime = 0;
00217   }
00218 
00223   virtual ~Object() {};
00224 
00231   static long GetCurrentTime () 
00232   { 
00233     return CurrentTime++; 
00234   }
00235   
00236 private:
00238   mutable int ReferenceCount;
00239 
00244   long ModifiedTime;
00245 };
00246 
00248 
00249 } // namespace cmtk
00250 
00251 #endif // #ifndef __cmtkObject_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines