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 //
00026 // Sh: A GPU metaprogramming language.
00027 //
00028 // Copyright 2003-2005 Serious Hack Inc.
00029 // 
00030 // This software is provided 'as-is', without any express or implied
00031 // warranty. In no event will the authors be held liable for any damages
00032 // arising from the use of this software.
00033 // 
00034 // Permission is granted to anyone to use this software for any purpose,
00035 // including commercial applications, and to alter it and redistribute it
00036 // freely, subject to the following restrictions:
00037 // 
00038 // 1. The origin of this software must not be misrepresented; you must
00039 // not claim that you wrote the original software. If you use this
00040 // software in a product, an acknowledgment in the product documentation
00041 // would be appreciated but is not required.
00042 // 
00043 // 2. Altered source versions must be plainly marked as such, and must
00044 // not be misrepresented as being the original software.
00045 // 
00046 // 3. This notice may not be removed or altered from any source
00047 // distribution.
00049 #ifndef SHVARIANT_HPP
00050 #define SHVARIANT_HPP
00051 
00052 #include <string>
00053 #include <vector>
00054 #include <ostream>
00055 #include "ShDllExport.hpp"
00056 #include "ShPool.hpp"
00057 #include "ShSwizzle.hpp"
00058 #include "ShVariableType.hpp"
00059 #include "ShDataType.hpp"
00060 #include "ShRefCount.hpp"
00061 
00062 namespace SH {
00063 
00064 
00074 class 
00075 SH_DLLEXPORT 
00076 ShVariant: public ShRefCountable {
00077   public:
00078     ShVariant();
00079     virtual ~ShVariant();
00080 
00081 
00085     virtual ShValueType valueType() const = 0; 
00086 
00088     virtual ShDataType dataType() const = 0; 
00089 
00091     virtual bool typeMatches(ShValueType valueType, ShDataType dataType) const = 0;
00092 
00094     virtual const char* typeName() const = 0; 
00095 
00097     virtual int size() const = 0;
00098 
00100     virtual int datasize() const = 0;
00101 
00104     virtual bool managed() const = 0;
00105 
00108     virtual void negate() = 0;
00109 
00110 
00114     virtual void set(ShPointer<const ShVariant> other) = 0;
00115     virtual void set(const ShVariant* other) = 0;
00116     // @}
00117 
00120     virtual void set(ShPointer<const ShVariant> other, int index) = 0;
00121     virtual void set(const ShVariant* other, int index) = 0;
00122     // @}
00123 
00127     virtual void set(ShPointer<const ShVariant> other, bool neg, const ShSwizzle &writemask) = 0;
00128     virtual void set(const ShVariant* other, bool neg, const ShSwizzle &writemask) = 0;
00129     // @}
00130 
00132     virtual ShPointer<ShVariant> get() const = 0;
00133 
00135     virtual ShPointer<ShVariant> get(int index) const = 0;
00136 
00140     virtual ShPointer<ShVariant> get(bool neg, const ShSwizzle &swizzle, int count=1) const = 0; 
00141 
00143     // This uses shDataTypeEquals
00144     //
00145     // @see ShDataType.hpp
00147     virtual bool equals(ShPointer<const ShVariant> other) const = 0;
00148     virtual bool equals(const ShVariant* other) const = 0;
00150 
00152     // @see ShDataType.hpp
00153     virtual bool isTrue() const = 0;
00154 
00156 
00157     virtual void* array() = 0;
00158     virtual const void* array() const = 0;
00159     // @}
00160 
00161 
00163     virtual std::string encode() const = 0;
00164     virtual std::string encode(int index, int repeats=1) const = 0;
00165     virtual std::string encode(bool neg, const ShSwizzle &swizzle) const = 0;
00166 
00171     virtual std::string encodeArray() const = 0;
00172 };
00173 
00174 typedef ShPointer<ShVariant> ShVariantPtr;
00175 typedef ShPointer<const ShVariant> ShVariantCPtr;
00176 
00177 /* A fixed-size array of a specific data type that can act as an ShVariant 
00178  *
00179  * This is different from ShMemory objects which hold arbitrary typed
00180  * data in byte arrays (that eventually may include some 
00181  * unordered collection of several types)
00182  *
00183  * @see ShMemory 
00184  **/ 
00185 template<typename T, ShDataType DT=SH_HOST>
00186 class ShDataVariant: public ShVariant {
00187   public:
00188     static const ShValueType value_type = ShStorageTypeInfo<T>::value_type;
00189     static const ShDataType data_type = DT;
00190     typedef ShDataVariant<T, DT>* PtrType;
00191     typedef const ShDataVariant<T, DT>* CPtrType;
00192     typedef typename ShDataTypeCppType<T, DT>::type DataType;
00193     typedef DataType* iterator;
00194     typedef const DataType* const_iterator;
00195 
00198     ShDataVariant(int N); 
00199 
00201     ShDataVariant(int N, const DataType &value); 
00202 
00206     ShDataVariant(std::string encodedValue);
00207 
00212     ShDataVariant(int N, void *data, bool managed = true);
00213 
00216     ShDataVariant(const ShDataVariant<T, DT> &other);
00217     ShDataVariant(const ShDataVariant<T, DT> &other, bool neg, const ShSwizzle &swizzle, int count=1); 
00218 
00219     virtual ~ShDataVariant();
00220 
00221     ShValueType valueType() const; 
00222     ShDataType dataType() const; 
00223     bool typeMatches(ShValueType valueType, ShDataType dataType) const; 
00224 
00226     const char* typeName() const; 
00227 
00228     //std::string typeName() const; 
00229     
00230     int size() const; 
00231     int datasize() const; 
00232 
00233     bool managed() const;
00234 
00235     void negate();
00236 
00237     void set(ShVariantCPtr other);
00238     void set(const ShVariant* other);
00239     void set(ShVariantCPtr other, int index);
00240     void set(const ShVariant* other, int index);
00241     void set(ShVariantCPtr other, bool neg, const ShSwizzle &writemask);
00242     void set(const ShVariant* other, bool neg, const ShSwizzle &writemask);
00243 
00244     ShVariantPtr get() const; 
00245     ShVariantPtr get(int index) const; 
00246     ShVariantPtr get(bool neg, const ShSwizzle &swizzle, int count=1) const; 
00247 
00248     bool equals(ShVariantCPtr other) const; 
00249     bool equals(const ShVariant* other) const; 
00250 
00251     bool isTrue() const;
00252 
00253     void* array(); 
00254     const void* array() const; 
00255 
00256     DataType& operator[](int index);
00257     const DataType& operator[](int index) const;
00258 
00259     iterator begin();
00260     iterator end();
00261 
00262     const_iterator begin() const;
00263     const_iterator end() const;
00264 
00268     std::string encode() const;
00269     std::string encode(int index, int repeats=1) const; 
00270     std::string encode(bool neg, const ShSwizzle &swizzle) const; 
00271 
00274     //
00277     
00278     std::string encodeArray() const;
00279 
00280 #ifdef SH_USE_MEMORY_POOL
00281     // Memory pool stuff.
00282     void* operator new(std::size_t size);
00283     void operator delete(void* d, std::size_t size);
00284 #endif
00285     
00286   protected:
00287     DataType *m_begin; 
00288     DataType *m_end; 
00289 
00290     bool m_managed; 
00291 
00293     void alloc(int N);
00294 
00295 #ifdef SH_USE_MEMORY_POOL
00296     static ShPool* m_pool;
00297 #endif
00298 };
00299 
00301 
00302 // Cast to the specified data variant using dynamic_cast
00303 //
00304 // Refcounted and non-refcounted versions
00306 template<typename T, ShDataType DT>
00307 ShPointer<ShDataVariant<T, DT> > variant_cast(ShVariantPtr c);
00308 
00309 template<typename T, ShDataType DT>
00310 ShPointer<const ShDataVariant<T, DT> > variant_cast(ShVariantCPtr c);
00311 
00312 template<typename T, ShDataType DT>
00313 ShDataVariant<T, DT>* variant_cast(ShVariant* c);
00314 
00315 template<typename T, ShDataType DT>
00316 const ShDataVariant<T, DT>* variant_cast(const ShVariant* c);
00317 // @}
00318 
00319 // Make a copy of c cast to the requested type 
00321 template<typename T, ShDataType DT>
00322 ShPointer<ShDataVariant<T, DT> > variant_convert(ShVariantCPtr c);
00323 // @}
00324 
00325 
00326 }
00327 
00328 #include "ShVariantImpl.hpp"
00329 
00330 #endif

Generated on Wed Jun 15 18:12:42 2005 for Sh by  doxygen 1.4.3-20050530