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 library is free software; you can redistribute it and/or
00031 // modify it under the terms of the GNU Lesser General Public
00032 // License as published by the Free Software Foundation; either
00033 // version 2.1 of the License, or (at your option) any later version.
00034 //
00035 // This library is distributed in the hope that it will be useful,
00036 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00037 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00038 // Lesser General Public License for more details.
00039 //
00040 // You should have received a copy of the GNU Lesser General Public
00041 // License along with this library; if not, write to the Free Software
00042 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00043 // MA  02110-1301, USA
00045 #ifndef SHVARIANT_HPP
00046 #define SHVARIANT_HPP
00047 
00048 #include <string>
00049 #include <vector>
00050 #include <ostream>
00051 #include "ShDllExport.hpp"
00052 #include "ShPool.hpp"
00053 #include "ShSwizzle.hpp"
00054 #include "ShVariableType.hpp"
00055 #include "ShDataType.hpp"
00056 #include "ShRefCount.hpp"
00057 
00058 namespace SH {
00059 
00060 
00070 class 
00071 SH_DLLEXPORT 
00072 ShVariant: public ShRefCountable {
00073   public:
00074     ShVariant();
00075     virtual ~ShVariant();
00076 
00077 
00081     virtual ShValueType valueType() const = 0; 
00082 
00084     virtual ShDataType dataType() const = 0; 
00085 
00087     virtual bool typeMatches(ShValueType valueType, ShDataType dataType) const = 0;
00088 
00090     virtual const char* typeName() const = 0; 
00091 
00093     virtual int size() const = 0;
00094 
00096     virtual int datasize() const = 0;
00097 
00100     virtual bool managed() const = 0;
00101 
00104     virtual void negate() = 0;
00105 
00106 
00110     virtual void set(ShPointer<const ShVariant> other) = 0;
00111     virtual void set(const ShVariant* other) = 0;
00112     // @}
00113 
00116     virtual void set(ShPointer<const ShVariant> other, int index) = 0;
00117     virtual void set(const ShVariant* other, int index) = 0;
00118     // @}
00119 
00123     virtual void set(ShPointer<const ShVariant> other, bool neg, const ShSwizzle &writemask) = 0;
00124     virtual void set(const ShVariant* other, bool neg, const ShSwizzle &writemask) = 0;
00125     // @}
00126 
00128     virtual ShPointer<ShVariant> get() const = 0;
00129 
00131     virtual ShPointer<ShVariant> get(int index) const = 0;
00132 
00136     virtual ShPointer<ShVariant> get(bool neg, const ShSwizzle &swizzle, int count=1) const = 0; 
00137 
00139     // This uses shDataTypeEquals
00140     //
00141     // @see ShDataType.hpp
00143     virtual bool equals(ShPointer<const ShVariant> other) const = 0;
00144     virtual bool equals(const ShVariant* other) const = 0;
00146 
00148     // @see ShDataType.hpp
00149     virtual bool isTrue() const = 0;
00150 
00152 
00153     virtual void* array() = 0;
00154     virtual const void* array() const = 0;
00155     // @}
00156 
00157 
00159     virtual std::string encode() const = 0;
00160     virtual std::string encode(int index, int repeats=1) const = 0;
00161     virtual std::string encode(bool neg, const ShSwizzle &swizzle) const = 0;
00162 
00167     virtual std::string encodeArray() const = 0;
00168 };
00169 
00170 typedef ShPointer<ShVariant> ShVariantPtr;
00171 typedef ShPointer<const ShVariant> ShVariantCPtr;
00172 
00173 /* A fixed-size array of a specific data type that can act as an ShVariant 
00174  *
00175  * This is different from ShMemory objects which hold arbitrary typed
00176  * data in byte arrays (that eventually may include some 
00177  * unordered collection of several types)
00178  *
00179  * @see ShMemory 
00180  **/ 
00181 template<typename T, ShDataType DT=SH_HOST>
00182 class ShDataVariant: public ShVariant {
00183   public:
00184     static const ShValueType value_type = ShStorageTypeInfo<T>::value_type;
00185     static const ShDataType data_type = DT;
00186     typedef ShDataVariant<T, DT>* PtrType;
00187     typedef const ShDataVariant<T, DT>* CPtrType;
00188     typedef typename ShDataTypeCppType<T, DT>::type DataType;
00189     typedef DataType* iterator;
00190     typedef const DataType* const_iterator;
00191 
00194     ShDataVariant(int N); 
00195 
00197     ShDataVariant(int N, const DataType &value); 
00198 
00202     ShDataVariant(std::string encodedValue);
00203 
00208     ShDataVariant(int N, void *data, bool managed = true);
00209 
00212     ShDataVariant(const ShDataVariant<T, DT> &other);
00213     ShDataVariant(const ShDataVariant<T, DT> &other, bool neg, const ShSwizzle &swizzle, int count=1); 
00214 
00215     virtual ~ShDataVariant();
00216 
00217     ShValueType valueType() const; 
00218     ShDataType dataType() const; 
00219     bool typeMatches(ShValueType valueType, ShDataType dataType) const; 
00220 
00222     const char* typeName() const; 
00223 
00224     //std::string typeName() const; 
00225     
00226     int size() const; 
00227     int datasize() const; 
00228 
00229     bool managed() const;
00230 
00231     void negate();
00232 
00233     void set(ShVariantCPtr other);
00234     void set(const ShVariant* other);
00235     void set(ShVariantCPtr other, int index);
00236     void set(const ShVariant* other, int index);
00237     void set(ShVariantCPtr other, bool neg, const ShSwizzle &writemask);
00238     void set(const ShVariant* other, bool neg, const ShSwizzle &writemask);
00239 
00240     ShVariantPtr get() const; 
00241     ShVariantPtr get(int index) const; 
00242     ShVariantPtr get(bool neg, const ShSwizzle &swizzle, int count=1) const; 
00243 
00244     bool equals(ShVariantCPtr other) const; 
00245     bool equals(const ShVariant* other) const; 
00246 
00247     bool isTrue() const;
00248 
00249     void* array(); 
00250     const void* array() const; 
00251 
00252     DataType& operator[](int index);
00253     const DataType& operator[](int index) const;
00254 
00255     iterator begin();
00256     iterator end();
00257 
00258     const_iterator begin() const;
00259     const_iterator end() const;
00260 
00264     std::string encode() const;
00265     std::string encode(int index, int repeats=1) const; 
00266     std::string encode(bool neg, const ShSwizzle &swizzle) const; 
00267 
00270     //
00273     
00274     std::string encodeArray() const;
00275 
00276 #ifdef SH_USE_MEMORY_POOL
00277     // Memory pool stuff.
00278     void* operator new(std::size_t size);
00279     void operator delete(void* d, std::size_t size);
00280 #endif
00281     
00282   protected:
00283     DataType *m_begin; 
00284     DataType *m_end; 
00285 
00286     bool m_managed; 
00287 
00289     void alloc(int N);
00290 
00291 #ifdef SH_USE_MEMORY_POOL
00292     static ShPool* m_pool;
00293 #endif
00294 };
00295 
00297 
00298 // Cast to the specified data variant using dynamic_cast
00299 //
00300 // Refcounted and non-refcounted versions
00302 template<typename T, ShDataType DT>
00303 ShPointer<ShDataVariant<T, DT> > variant_cast(ShVariantPtr c);
00304 
00305 template<typename T, ShDataType DT>
00306 ShPointer<const ShDataVariant<T, DT> > variant_cast(ShVariantCPtr c);
00307 
00308 template<typename T, ShDataType DT>
00309 ShDataVariant<T, DT>* variant_cast(ShVariant* c);
00310 
00311 template<typename T, ShDataType DT>
00312 const ShDataVariant<T, DT>* variant_cast(const ShVariant* c);
00313 // @}
00314 
00315 // Make a copy of c cast to the requested type 
00317 template<typename T, ShDataType DT>
00318 ShPointer<ShDataVariant<T, DT> > variant_convert(ShVariantCPtr c);
00319 // @}
00320 
00321 
00322 }
00323 
00324 #include "ShVariantImpl.hpp"
00325 
00326 #endif

Generated on Thu Jul 28 17:33:06 2005 for Sh by  doxygen 1.4.3-20050530