00001
00002
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
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
00139 virtual ShPointer<ShVariant> get(bool neg, const ShSwizzle &swizzle) const = 0;
00140
00142
00143
00144
00146 virtual bool equals(ShPointer<const ShVariant> other) const = 0;
00147 virtual bool equals(const ShVariant* other) const = 0;
00149
00151
00152 virtual bool isTrue() const = 0;
00153
00155
00156 virtual void* array() = 0;
00157 virtual const void* array() const = 0;
00158
00159
00160
00162 virtual std::string encode() const = 0;
00163 virtual std::string encode(int index, int repeats=1) const = 0;
00164 virtual std::string encode(bool neg, const ShSwizzle &swizzle) const = 0;
00165
00170 virtual std::string encodeArray() const = 0;
00171 };
00172
00173 typedef ShPointer<ShVariant> ShVariantPtr;
00174 typedef ShPointer<const ShVariant> ShVariantCPtr;
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 template<typename T, ShDataType DT=SH_HOST>
00185 class ShDataVariant: public ShVariant {
00186 public:
00187 static const ShValueType value_type = ShStorageTypeInfo<T>::value_type;
00188 static const ShDataType data_type = DT;
00189 typedef ShDataVariant<T, DT>* PtrType;
00190 typedef const ShDataVariant<T, DT>* CPtrType;
00191 typedef typename ShDataTypeCppType<T, DT>::type DataType;
00192 typedef DataType* iterator;
00193 typedef const DataType* const_iterator;
00194
00197 ShDataVariant(int N);
00198
00200 ShDataVariant(int N, const DataType &value);
00201
00205 ShDataVariant(std::string encodedValue);
00206
00211 ShDataVariant(void *data, int N, bool managed = true);
00212
00215 ShDataVariant(const ShDataVariant<T, DT> &other);
00216 ShDataVariant(const ShDataVariant<T, DT> &other, bool neg, const ShSwizzle &swizzle);
00217
00218 virtual ~ShDataVariant();
00219
00220 ShValueType valueType() const;
00221 ShDataType dataType() const;
00222 bool typeMatches(ShValueType valueType, ShDataType dataType) const;
00223
00225 const char* typeName() const;
00226
00227
00228
00229 int size() const;
00230 int datasize() const;
00231
00232 bool managed() const;
00233
00234 void negate();
00235
00236 void set(ShVariantCPtr other);
00237 void set(const ShVariant* other);
00238 void set(ShVariantCPtr other, int index);
00239 void set(const ShVariant* other, int index);
00240 void set(ShVariantCPtr other, bool neg, const ShSwizzle &writemask);
00241 void set(const ShVariant* other, bool neg, const ShSwizzle &writemask);
00242
00243 ShVariantPtr get() const;
00244 ShVariantPtr get(int index) const;
00245 ShVariantPtr get(bool neg, const ShSwizzle &swizzle) const;
00246
00247 bool equals(ShVariantCPtr other) const;
00248 bool equals(const ShVariant* other) const;
00249
00250 bool isTrue() const;
00251
00252 void* array();
00253 const void* array() const;
00254
00255 DataType& operator[](int index);
00256 const DataType& operator[](int index) const;
00257
00258 iterator begin();
00259 iterator end();
00260
00261 const_iterator begin() const;
00262 const_iterator end() const;
00263
00267 std::string encode() const;
00268 std::string encode(int index, int repeats=1) const;
00269 std::string encode(bool neg, const ShSwizzle &swizzle) const;
00270
00273
00276
00277 std::string encodeArray() const;
00278
00279 #ifdef SH_USE_MEMORY_POOL
00280
00281 void* operator new(std::size_t size);
00282 void operator delete(void* d, std::size_t size);
00283 #endif
00284
00285 protected:
00286 DataType *m_begin;
00287 DataType *m_end;
00288
00289 bool m_managed;
00290
00292 void alloc(int N);
00293
00294 #ifdef SH_USE_MEMORY_POOL
00295 static ShPool* m_pool;
00296 #endif
00297 };
00298
00300
00301
00302
00303
00305 template<typename T, ShDataType DT>
00306 ShPointer<ShDataVariant<T, DT> > variant_cast(ShVariantPtr c);
00307
00308 template<typename T, ShDataType DT>
00309 ShPointer<const ShDataVariant<T, DT> > variant_cast(ShVariantCPtr c);
00310
00311 template<typename T, ShDataType DT>
00312 ShDataVariant<T, DT>* variant_cast(ShVariant* c);
00313
00314 template<typename T, ShDataType DT>
00315 const ShDataVariant<T, DT>* variant_cast(const ShVariant* c);
00316
00317
00318
00320 template<typename T, ShDataType DT>
00321 ShPointer<ShDataVariant<T, DT> > variant_convert(ShVariantCPtr c);
00322
00323
00324
00325 }
00326
00327 #include "ShVariantImpl.hpp"
00328
00329 #endif