Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

common/vector_object.hpp

Go to the documentation of this file.
00001 //
00002 // See license.txt for license information.
00003 //
00004 // vector_object.hpp
00005 //
00006 // 4-Jul-2003  phamilton  Created
00007 //
00008 
00009 #ifndef incCOMMON_VECTOR_OBJECT
00010 #define incCOMMON_VECTOR_OBJECT
00011 
00012 // forwards
00013 #include <vector>
00014 #include "object.hpp"
00015 #include "deletable_object.hpp"
00016 #include "outerable_object.hpp"
00017 #include "comparable_object.hpp"
00018 #include <boost/bind.hpp>
00019 #include <algorithm>
00020 
00021 namespace ph {
00022 namespace common {
00023 
00024 class vector_object_helper
00025 /**
00026         Helper for an object which contains a vector of other objects.
00027 */
00028 {
00029 public:
00030 
00031         void helper_delete_object(bool owned)
00032         /**
00033                 Delete the objects attached. Only deletes if the
00034                 objects are actually owned by this.
00035         */
00036         {
00037                 // only delete sub objects if this is owned.
00038                 if (owned)
00039                         std::for_each(_vector.begin(), _vector.end(),
00040                                 boost::bind(&helper_delete_1, _1));
00041                 _vector.clear();
00042         }
00043         
00044         bool helper_add(object_base *parent, 
00045                         object_base *obj, bool own, bool owned)
00046         /**
00047                 Add a new object to the collection.
00048         */
00049         {
00050                 // ownership policies must match.
00051                 if (own != owned)
00052                         return false;
00053                 if (obj->outerable())
00054                         obj->outerable()->outer(parent);
00055                 _vector.push_back(obj);
00056                 return true;
00057         }
00058         
00059         bool helper_remove(object_base *obj)
00060         /**
00061                 remove an object from the collection.
00062         */
00063         {
00064                 std::vector<object_base *>::iterator i=std::find_if(_vector.begin(), _vector.end(),
00065                         boost::bind(is_equal, _1, obj));
00066                 if (i == _vector.end())
00067                         return false;
00068                 _vector.erase(i);
00069                 return true;
00070         }
00071         
00072         bool helper_singleton() const
00073         /**
00074                 Vectors are not singleton objects.
00075         */
00076         {
00077                 return false;
00078         }
00079         
00080         bool helper_count() const
00081         /**
00082                 Return the number of objects in the vector.
00083         */
00084         {
00085                 return _vector.size();
00086         }
00087         
00088 protected:
00089         std::vector<object_base *> _vector;
00090 
00091 private:
00092 
00093         static void helper_delete_1(object_base *obj)
00094         {
00095                 // if the object has a delete interface, then use that.
00096                 if (obj->deletable())
00097                         obj->deletable()->delete_object();
00098                 else
00099                         delete obj;
00100         }
00101         
00102         static bool is_equal(object_base *obj1, object_base *obj2)
00103         {
00104                 // if the object has an equal interface, then use that.
00105                 if (obj1->comparable())
00106                 {
00107                         if (obj1->comparable()->equal(obj2))
00108                                 return true;
00109                 }
00110                 else if (obj1 == obj2)
00111                         return true;
00112                         
00113                 return false;
00114         }
00115         
00116 };
00117 
00118 }; // common
00119 }; // ph
00120 
00121 #endif // incCOMMON_VECTOR_OBJECT

Generated on Wed Apr 5 22:03:27 2006 for cppxmlobj by  doxygen 1.4.3