Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

TBaseVectorProperty.h

Go to the documentation of this file.
00001 
00046 #ifndef _AMIRE_PROPERTY_T_BASE_VECTOR_PROPERTY_H
00047 #define _AMIRE_PROPERTY_T_BASE_VECTOR_PROPERTY_H
00048 
00049 #include <amire/common/Defines.h>
00050 #include <amire/common/Assert.h>
00051 #include <amire/property/PersistentProperty.h>
00052 #include <amire/property/PropertyTypeManager.h>
00053 #include <amire/property/PropertyWriter.h>
00054 #include <amire/property/PropertyReader.h>
00055 
00056 namespace amire {
00057   namespace property {
00066     template <class type> class AMIRE_EXPORT_API TBaseVectorProperty : public virtual PersistentProperty {
00067     private:
00068       std::vector<type> values;
00069 
00070     public:
00071       inline TBaseVectorProperty(const PropertyTypeManager *propertyTypeManager)
00072           : Property(propertyTypeManager, getBaseType(propertyTypeManager)), PersistentProperty(propertyTypeManager, propertyType), values() {
00073       }
00074 
00075       inline TBaseVectorProperty(int length, const PropertyTypeManager *propertyTypeManager)
00076           : Property(propertyTypeManager, getBaseType(propertyTypeManager)), values(length) {
00077         for (int i = 0; i < length; i++) {
00078           initValue(values[i]);
00079         }
00080       }
00081 
00082       inline TBaseVectorProperty(int length, const type *pvalues, const PropertyTypeManager *propertyTypeManager)
00083           : Property(propertyTypeManager, getBaseType(propertyTypeManager)), PersistentProperty(propertyTypeManager, propertyType), values(length) {
00084         for (int i = 0; i < length; i++) {
00085           values[i] = pvalues[i];
00086         }
00087       }
00088 
00089       inline TBaseVectorProperty(const TBaseVectorProperty<type> &property)
00090           : Property(property.propertyTypeManager, property.propertyType), PersistentProperty(propertyTypeManager, propertyType), values(property.values) {
00091       }
00092 
00093       virtual ~TBaseVectorProperty() {
00094       }
00095 
00096       virtual bool equals(const osg::ref_ptr<const Property> &property) const {
00097         const TBaseVectorProperty<type> *baseVectorProperty = dynamic_cast<const TBaseVectorProperty<type>*>(property.get());
00098         if (!baseVectorProperty) {
00099           return false;
00100         }
00101         int size = int(values.size());
00102         for (int i = 0; i < size; ++i) {
00103           if (!equalsValue(values[i], baseVectorProperty->values[i])) {
00104             return false;
00105           }
00106         }
00107         return true;
00108       }
00109 
00110       virtual osg::ref_ptr<Property> clone() const {
00111         return new TBaseVectorProperty<type>(*this);
00112       }
00113 
00114       virtual void cloneFrom(const osg::ref_ptr<const Property> &property) {
00115         const TBaseVectorProperty<type> *baseVectorProperty = amire::common::guarded_cast<const TBaseVectorProperty<type>, const Property>(property.get());
00116         ASSERT1(baseVectorProperty, "typecast failed");
00117         values = baseVectorProperty->values;
00118         propertyType = baseVectorProperty->propertyType;
00119       }
00120 
00125       inline int size() const {
00126         return int(values.size());
00127       }
00128 
00136       inline const type &get(int index) const {
00137         ASSERT1(index < size(), "index out of bounds!");
00138         return values[index];
00139       }
00140 
00144       inline void clear() {
00145         values.clear();
00146       }
00147 
00152       inline void replace(int length, const type *values) {
00153         if (int(this->values.size()) != length) {
00154           this->values.resize(length);
00155         }
00156         for (int i = 0; i < length; ++i) {
00157           this->values[i] = values[i];
00158         }
00159       }
00160 
00167       inline void set(int index, const type &value) {
00168         ASSERT1(index < size(), "index out of bounds!");
00169         values[index] = value;
00170       }
00171 
00179       inline void insert(int index, type value) {
00180         int size = int(values.size());
00181         ASSERT(index >= 0);
00182         ASSERT(index <= size);
00183         values.push_back(value);
00184         if (index < size) {
00185           for (int i = index; i <=size; ++i) {
00186             values[i + 1] = values[i];
00187           }
00188           values[index] = value;
00189         }
00190       }
00191 
00198       inline void remove(int index) {
00199         int size = int(values.size()) - 1;
00200         ASSERT(index >= 0);
00201         ASSERT(index < size);
00202         for (int i = index; i < size; ++i) {
00203           values[i] = values[i + 1];
00204         }
00205         values.pop_back();
00206       }
00207 
00212       inline void push(type value) {
00213         values.push_back(value);
00214       }
00215 
00220       inline type pop() {
00221         ASSERT(values.size() > 0);
00222         type result = values[values.size() - 1];
00223         values.pop_back();
00224         return result;
00225       }
00226 
00227     private:
00228       inline osg::ref_ptr<const PropertyType> getBaseType(const PropertyTypeManager *propertyTypeManager) {
00229         type value;
00230         initValue(value);
00231         return propertyTypeManager->getBaseVectorType(value);
00232       }
00233     };
00234   }
00235 }
00236 
00237 #endif

Generated on Tue May 20 17:23:53 2003 for AMIRE-Framework by doxygen1.3