Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShVariant.hpp

00001 
00002 //
00027 #ifndef SHVARIANT_HPP
00028 #define SHVARIANT_HPP
00029 
00030 #include <string>
00031 #include <vector>
00032 #include <ostream>
00033 #include "ShDllExport.hpp"
00034 #include "ShPool.hpp"
00035 #include "ShSwizzle.hpp"
00036 #include "ShVariableType.hpp"
00037 #include "ShDataType.hpp"
00038 #include "ShRefCount.hpp"
00039 
00040 namespace SH {
00041 
00042 
00052 class 
00053 SH_DLLEXPORT 
00054 ShVariant: public ShRefCountable {
00055   public:
00056     ShVariant();
00057     virtual ~ShVariant();
00058 
00059 
00063     virtual ShValueType valueType() const = 0; 
00064 
00066     virtual ShDataType dataType() const = 0; 
00067 
00069     virtual bool typeMatches(ShValueType valueType, ShDataType dataType) const = 0;
00070 
00072     virtual const char* typeName() const = 0; 
00073 
00075     virtual int size() const = 0;
00076 
00078     virtual int datasize() const = 0;
00079 
00082     virtual bool managed() const = 0;
00083 
00086     virtual void negate() = 0;
00087 
00088 
00092     virtual void set(ShPointer<const ShVariant> other) = 0;
00093     virtual void set(const ShVariant* other) = 0;
00094     // @}
00095 
00098     virtual void set(ShPointer<const ShVariant> other, int index) = 0;
00099     virtual void set(const ShVariant* other, int index) = 0;
00100     // @}
00101 
00105     virtual void set(ShPointer<const ShVariant> other, bool neg, const ShSwizzle &writemask) = 0;
00106     virtual void set(const ShVariant* other, bool neg, const ShSwizzle &writemask) = 0;
00107     // @}
00108 
00110     virtual ShPointer<ShVariant> get() const = 0;
00111 
00113     virtual ShPointer<ShVariant> get(int index) const = 0;
00114 
00117     virtual ShPointer<ShVariant> get(bool neg, const ShSwizzle &swizzle) const = 0; 
00118 
00120     // This uses shDataTypeEquals
00121     //
00122     // @see ShDataType.hpp
00124     virtual bool equals(ShPointer<const ShVariant> other) const = 0;
00125     virtual bool equals(const ShVariant* other) const = 0;
00127 
00129     // @see ShDataType.hpp
00130     virtual bool isTrue() const = 0;
00131 
00133 
00134     virtual void* array() = 0;
00135     virtual const void* array() const = 0;
00136     // @}
00137 
00138 
00140     virtual std::string encode() const = 0;
00141     virtual std::string encode(int index, int repeats=1) const = 0;
00142     virtual std::string encode(bool neg, const ShSwizzle &swizzle) const = 0;
00143 
00148     virtual std::string encodeArray() const = 0;
00149 };
00150 
00151 typedef ShPointer<ShVariant> ShVariantPtr;
00152 typedef ShPointer<const ShVariant> ShVariantCPtr;
00153 
00154 /* A fixed-size array of a specific data type that can act as an ShVariant 
00155  *
00156  * This is different from ShMemory objects which hold arbitrary typed
00157  * data in byte arrays (that eventually may include some 
00158  * unordered collection of several types)
00159  *
00160  * @see ShMemory 
00161  **/ 
00162 template<typename T, ShDataType DT>
00163 class ShDataVariant: public ShVariant {
00164   public:
00165     static const ShValueType value_type = ShStorageTypeInfo<T>::value_type;
00166     typedef ShPointer<ShDataVariant<T, DT> > PtrType;
00167     typedef ShPointer<const ShDataVariant<T, DT> > CPtrType;
00168     typedef typename ShDataTypeCppType<T, DT>::type DataType;
00169     typedef DataType* iterator;
00170     typedef const DataType* const_iterator;
00171 
00174     ShDataVariant(int N); 
00175 
00177     ShDataVariant(int N, const DataType &value); 
00178 
00182     ShDataVariant(std::string encodedValue);
00183 
00188     ShDataVariant(void *data, int N, bool managed = true);
00189 
00192     ShDataVariant(const ShDataVariant<T, DT> &other);
00193     ShDataVariant(const ShDataVariant<T, DT> &other, bool neg, const ShSwizzle &swizzle); 
00194 
00195     ~ShDataVariant();
00196 
00197     ShValueType valueType() const; 
00198     ShDataType dataType() const; 
00199     bool typeMatches(ShValueType valueType, ShDataType dataType) const; 
00200 
00202     const char* typeName() const; 
00203 
00204     //std::string typeName() const; 
00205     
00206     int size() const; 
00207     int datasize() const; 
00208 
00209     bool managed() const;
00210 
00211     void negate();
00212 
00213     void set(ShVariantCPtr other);
00214     void set(const ShVariant* other);
00215     void set(ShVariantCPtr other, int index);
00216     void set(const ShVariant* other, int index);
00217     void set(ShVariantCPtr other, bool neg, const ShSwizzle &writemask);
00218     void set(const ShVariant* other, bool neg, const ShSwizzle &writemask);
00219 
00220     ShVariantPtr get() const; 
00221     ShVariantPtr get(int index) const; 
00222     ShVariantPtr get(bool neg, const ShSwizzle &swizzle) const; 
00223 
00224     bool equals(ShVariantCPtr other) const; 
00225     bool equals(const ShVariant* other) const; 
00226 
00227     bool isTrue() const;
00228 
00229     void* array(); 
00230     const void* array() const; 
00231 
00232     DataType& operator[](int index);
00233     const DataType& operator[](int index) const;
00234 
00235     iterator begin();
00236     iterator end();
00237 
00238     const_iterator begin() const;
00239     const_iterator end() const;
00240 
00244     std::string encode() const;
00245     std::string encode(int index, int repeats=1) const; 
00246     std::string encode(bool neg, const ShSwizzle &swizzle) const; 
00247 
00250     //
00253     
00254     std::string encodeArray() const;
00255 
00256 #ifdef SH_USE_MEMORY_POOL
00257     // Memory pool stuff.
00258     void* operator new(std::size_t size);
00259     void operator delete(void* d, std::size_t size);
00260 #endif
00261     
00262   protected:
00263     DataType *m_begin; 
00264     DataType *m_end; 
00265 
00266     bool m_managed; 
00267 
00269     void alloc(int N);
00270 
00271 #ifdef SH_USE_MEMORY_POOL
00272     static ShPool* m_pool;
00273 #endif
00274 };
00275 
00277 
00278 // Cast to the specified data variant using dynamic_cast
00279 //
00280 // Refcounted and non-refcounted versions
00282 template<typename T, ShDataType DT>
00283 ShPointer<ShDataVariant<T, DT> > variant_cast(ShVariantPtr c);
00284 
00285 template<typename T, ShDataType DT>
00286 ShPointer<const ShDataVariant<T, DT> > variant_cast(ShVariantCPtr c);
00287 
00288 template<typename T, ShDataType DT>
00289 ShDataVariant<T, DT>* variant_cast(ShVariant* c);
00290 
00291 template<typename T, ShDataType DT>
00292 const ShDataVariant<T, DT>* variant_cast(const ShVariant* c);
00293 // @}
00294 
00295 // Make a copy of c cast to the requested type 
00297 template<typename T, ShDataType DT>
00298 ShPointer<ShDataVariant<T, DT> > variant_convert(ShVariantCPtr c);
00299 // @}
00300 
00301 
00302 }
00303 
00304 #include "ShVariantImpl.hpp"
00305 
00306 #endif

Generated on Mon Jan 24 18:36:36 2005 for Sh by  doxygen 1.4.1