MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLBase/mlBaseList.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00007 
00012 //
00013 // Defines the classes:
00014 //
00015 // - BaseContainerItem    : BaseItem-derived container class for Base-derived objects.
00016 // - BaseList             : BaseListTemplate<BaseContainer*>-derived container class for 
00017 //                          BaseContainer objects.
00018 //----------------------------------------------------------------------------------
00019 
00020 
00021 
00022 #ifndef __mlBaseList_H
00023 #define __mlBaseList_H
00024 
00025 
00026 // ML includes
00027 #ifndef __mlModuleIncludes_H
00028 #include "mlModuleIncludes.h"
00029 #endif
00030 
00031 // Local includes
00032 #ifndef __mlBaseInit_H
00033 #include "mlBaseInit.h"
00034 #endif
00035 #ifndef __mlListBase_H
00036 #include "mlListBase.h"
00037 #endif
00038 
00039 
00040 ML_START_NAMESPACE
00041 
00042 
00043   
00044 // ------------------------------------------------------------------
00045 // Base object class BaseContainerItem
00046 // ------------------------------------------------------------------
00047 
00049 
00055 class MLBASEEXPORT BaseContainerItem : public BaseItem
00056 {
00057 public:
00058     
00060 
00061     
00063     BaseContainerItem (); 
00064     
00066     BaseContainerItem (MLssize_t idParam, const char* nameParam = 0, Base* baseP = 0);
00067     
00069     BaseContainerItem (const BaseContainerItem &other);
00070     
00072     bool operator <(const BaseContainerItem &) const { return false; }
00073     
00075     bool operator ==(const BaseContainerItem &) const { return false; }
00076     
00078     
00080 
00081     
00083     virtual ~BaseContainerItem();
00084     
00086 
00092     virtual void setObject(Base& object);
00093 
00097     virtual Base* getObjectPointer() const {
00098       return _baseObjectP;
00099     }
00100 
00102     virtual const Base* getConstObjectPointer() const {
00103       return const_cast<const Base*>(_baseObjectP);
00104     }
00105 
00109     virtual Base* removeObjectPointer();
00110     
00114     static BaseContainerItem* toBaseContainerItem(Base& baseObj, MLssize_t id = 0, const char* name = NULL);
00115     
00119     virtual BaseContainerItem& operator=(const BaseContainerItem& other);
00120     
00126     virtual BaseContainerItem& copyFrom(const BaseContainerItem& other, bool performDeepCopy = true);
00127     
00129 
00130     
00133     virtual char *getPersistentState () const;
00134     
00136     virtual void setPersistentState (const char *state);
00137     
00140     virtual void clearPersistentState (char *state) const;  
00141     
00143     virtual void addStateToTree(TreeNode* parent) const;
00144     
00146     ML_SET_ADDSTATE_VERSION(0);
00147     
00149     virtual void readStateFromTree(TreeNode* parent);
00150     
00152 
00153 protected:
00154 
00161     void _setObjectPointer(Base* objectP, bool isOwner = true);
00162 
00164     Base* _baseObjectP;
00165     
00169     bool _ownsBaseObject;
00170 
00176     bool _baseObjectIsRefCounted;
00177     
00178 private:
00179     
00181     ML_CLASS_HEADER(BaseContainerItem);
00182 
00183 };
00184 
00185 
00186 // ------------------------------------------------------------------
00188 // ------------------------------------------------------------------
00189 class MLBASEEXPORT BaseListSortParameters {
00190 public:
00192     virtual bool isLessThan(const Base& /*x*/, const Base& /*y*/) { return false;};
00194     virtual ~BaseListSortParameters(){ } /* Make destructor virtual to avoid warnings on GCC */
00195 };
00196 
00197 
00198 
00199 
00200 // ------------------------------------------------------------------
00201 // Class BaseList 
00202 // ------------------------------------------------------------------
00203 
00205 class MLBASEEXPORT BaseList : public BaseListTemplate<BaseContainerItem>
00206 {
00207     
00208 public:
00209     
00211     BaseList () : BaseListTemplate<BaseContainerItem>(true) {}
00212 
00214     virtual BaseList* deepCopy() const { /* TBI: copy each base object */ return NULL; };
00215         
00216 private:
00217     
00219     ML_CLASS_HEADER(BaseList);
00220     
00221 };
00222 
00223 ML_END_NAMESPACE
00224 
00225 namespace std {
00226 
00227 
00229     template<>
00230         struct less <ML_NAMESPACE::BaseContainerItem> : public binary_function <ML_NAMESPACE::BaseContainerItem ,ML_NAMESPACE::BaseContainerItem, bool>
00231     {
00232         
00234         less<ML_NAMESPACE::BaseContainerItem> (ML_NAMESPACE::BaseListSortParameters* sortParams) {
00235             _sortParams = sortParams;
00236         }
00237         
00239         bool operator()(const ML_NAMESPACE::BaseContainerItem &x, const ML_NAMESPACE::BaseContainerItem &y) const
00240         {    
00241             // An item containing an object defined to be less than an item without one:
00242             if (!x.getConstObjectPointer()) { return false; }
00243             if (!y.getConstObjectPointer()) { return true;  }
00244             
00245             if (!_sortParams) { return false; }
00246             
00248             return _sortParams->isLessThan(*x.getConstObjectPointer(), *y.getConstObjectPointer());
00249         };
00250         
00252         ML_NAMESPACE::BaseListSortParameters* _sortParams;
00253         
00254     };
00255 
00256 }
00257 
00258 
00259 
00260 #endif // __mlBaseList_H
00261