build-artifacts/win/product/include/gfcpp/CacheableBuiltins.hpp

Go to the documentation of this file.
00001 #ifndef _GEMFIRE_CACHEABLE_BUILTINS_HPP_
00002 #define _GEMFIRE_CACHEABLE_BUILTINS_HPP_
00003 
00004 /*=========================================================================
00005  * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
00006  * This product is protected by U.S. and international copyright
00007  * and intellectual property laws. Pivotal products are covered by
00008  * more patents listed at http://www.pivotal.io/patents.
00009  *=========================================================================
00010  */
00011 
00017 #include "Cacheable.hpp"
00018 #include "CacheableKey.hpp"
00019 #include "Serializer.hpp"
00020 #include "CacheableKeys.hpp"
00021 #include "CacheableString.hpp"
00022 
00023 
00024 namespace gemfire
00025 {
00026 
00028   extern int gf_sprintf(char* buffer, const char* fmt, ...);
00029 
00031   extern int gf_snprintf(char* buffer, int32_t maxLength, const char* fmt, ...);
00032 
00034   template<typename TObj, int8_t TYPEID, const char* TYPENAME,
00035       const char* SPRINTFSYM, int32_t STRSIZE>
00036   class CacheableKeyType: public CacheableKey
00037   {
00038   protected:
00039     TObj m_value;
00040 
00041     inline CacheableKeyType() :
00042       m_value(gemfire::serializer::zeroObject<TObj>())
00043     {
00044     }
00045 
00046     inline CacheableKeyType(const TObj value) :
00047       m_value(value)
00048     {
00049     }
00050 
00051   public:
00053     inline TObj value() const
00054     {
00055       return m_value;
00056     }
00057 
00058     // Cacheable methods
00059 
00061     virtual void toData(DataOutput& output) const
00062     {
00063       gemfire::serializer::writeObject(output, m_value);
00064     }
00065 
00067     virtual Serializable* fromData(DataInput& input)
00068     {
00069       gemfire::serializer::readObject(input, m_value);
00070       return this;
00071     }
00072 
00079     virtual int32_t classId() const
00080     {
00081       return 0;
00082     }
00083 
00090     virtual int8_t typeId() const
00091     {
00092       return TYPEID;
00093     }
00094 
00096     virtual CacheableStringPtr toString() const
00097     {
00098       char buffer[STRSIZE + 1];
00099       gf_sprintf(buffer, SPRINTFSYM, m_value);
00100       return CacheableString::create(buffer);
00101     }
00102 
00103     // CacheableKey methods
00104 
00106     virtual uint32_t hashcode() const
00107     {
00108       return gemfire::serializer::hashcode(m_value);
00109     }
00110 
00112     virtual bool operator ==(const CacheableKey& other) const
00113     {
00114       if (other.typeId() != TYPEID) {
00115         return false;
00116       }
00117       const CacheableKeyType& otherValue =
00118           static_cast<const CacheableKeyType&> (other);
00119       return gemfire::serializer::equals(m_value, otherValue.m_value);
00120     }
00121 
00123     inline bool operator ==(const TObj other) const
00124     {
00125       return gemfire::serializer::equals(m_value, other);
00126     }
00127 
00132     virtual int32_t logString(char* buffer, int32_t maxLength) const
00133     {
00134       char fmt[64];
00135       gf_sprintf(fmt, "%s( %s )", TYPENAME, SPRINTFSYM);
00136       return gf_snprintf(buffer, maxLength, fmt, m_value);
00137     }
00138 
00147     virtual uint32_t objectSize() const
00148     {
00149       return sizeof(CacheableKeyType);
00150     }
00151   };
00152 
00153   // Forward declaration for SharedArrayPtr
00154   template<typename TObj, int8_t TYPEID> class SharedArrayPtr;
00155 
00157   template<typename TObj>
00158   inline void copyArray(TObj* dest, const TObj* src, int32_t length)
00159   {
00160     memcpy(dest, src, length * sizeof(TObj));
00161   }
00162 
00167   template<typename TObj>
00168   inline void copyArray(SharedPtr<TObj>* dest, const SharedPtr<TObj>* src,
00169       int32_t length)
00170   {
00171     for (int32_t index = 0; index < length; index++) {
00172       dest[index] = src[index];
00173     }
00174   }
00175 
00180   template<typename TObj, int8_t TYPEID>
00181   inline void copyArray(SharedArrayPtr<TObj, TYPEID>* dest,
00182       const SharedArrayPtr<TObj, TYPEID>* src, int32_t length)
00183   {
00184     for (int32_t index = 0; index < length; index++) {
00185       dest[index] = src[index];
00186     }
00187   }
00188 
00190   template<typename TObj, int8_t TYPEID>
00191   class CacheableArrayType: public Cacheable
00192   {
00193   protected:
00194 
00195     TObj* m_value;
00196     int32_t m_length;
00197 
00198     inline CacheableArrayType() :
00199       m_value(NULL), m_length(0)
00200     {
00201     }
00202 
00203     inline CacheableArrayType(int32_t length) :
00204       m_length(length)
00205     {
00206       if (length > 0) {
00207         GF_NEW(m_value, TObj[length]);
00208       }
00209     }
00210 
00211     inline CacheableArrayType(TObj* value, int32_t length) :
00212       m_value(value), m_length(length)
00213     {
00214     }
00215 
00216     inline CacheableArrayType(const TObj* value, int32_t length, bool copy) :
00217       m_value(NULL), m_length(length)
00218     {
00219       if (length > 0) {
00220         GF_NEW(m_value, TObj[length]);
00221         copyArray(m_value, value, length);
00222       }
00223     }
00224 
00225     virtual ~CacheableArrayType()
00226     {
00227       GF_SAFE_DELETE_ARRAY(m_value);
00228     }
00229 
00230   private:
00231 
00232     // Private to disable copy constructor and assignment operator.
00233     CacheableArrayType(const CacheableArrayType& other) :
00234         m_value(other.m_value), m_length(other.m_length)
00235     {
00236     }
00237 
00238     CacheableArrayType& operator =(const CacheableArrayType& other)
00239     {
00240       return *this;
00241     }
00242 
00243   public:
00244 
00246     inline const TObj* value() const
00247     {
00248       return m_value;
00249     }
00250 
00252     inline int32_t length() const
00253     {
00254       return m_length;
00255     }
00256 
00258     inline TObj operator [](uint32_t index) const
00259     {
00260       if ((int32_t) index >= m_length) {
00261         throw OutOfRangeException(
00262             "CacheableArray::operator[]: Index out of range.");
00263       }
00264       return m_value[index];
00265     }
00266 
00267     // Cacheable methods
00268 
00270     virtual void toData(DataOutput& output) const
00271     {
00272       gemfire::serializer::writeObject(output, m_value, m_length);
00273     }
00274 
00276     virtual Serializable* fromData(DataInput& input)
00277     {
00278       GF_SAFE_DELETE_ARRAY(m_value);
00279       gemfire::serializer::readObject(input, m_value, m_length);
00280       return this;
00281     }
00282 
00289     virtual int32_t classId() const
00290     {
00291       return 0;
00292     }
00293 
00300     virtual int8_t typeId() const
00301     {
00302       return TYPEID;
00303     }
00304 
00313     virtual uint32_t objectSize() const
00314     {
00315       return (uint32_t)(sizeof(CacheableArrayType)
00316           + gemfire::serializer::objectSize(m_value, m_length));
00317     }
00318 
00319   };
00320 
00324   template<typename TObj, int8_t TYPEID>
00325   class SharedArrayPtr: public SharedPtr<CacheableArrayType<TObj, TYPEID> >
00326   {
00327   private:
00328 
00329     typedef CacheableArrayType<TObj, TYPEID> TArray;
00330 
00331   public:
00332 
00334     inline SharedArrayPtr() :
00335       SharedPtr<CacheableArrayType<TObj, TYPEID> > ()
00336     {
00337     }
00338 
00340     inline SharedArrayPtr(const TArray* ptr) :
00341       SharedPtr<CacheableArrayType<TObj, TYPEID> > (ptr)
00342     {
00343     }
00344 
00346     inline SharedArrayPtr(const NullSharedBase* ptr) :
00347       SharedPtr<CacheableArrayType<TObj, TYPEID> > (ptr)
00348     {
00349     }
00350 
00352     inline SharedArrayPtr(const SharedArrayPtr& other) :
00353       SharedPtr<CacheableArrayType<TObj, TYPEID> > (other)
00354     {
00355     }
00356 
00358     template<typename TOther, int8_t OTHERID>
00359     inline SharedArrayPtr(const SharedArrayPtr<TOther, OTHERID>& other) :
00360       SharedPtr<CacheableArrayType<TObj, TYPEID> > (other)
00361     {
00362     }
00363 
00365     template<typename TOther>
00366     inline SharedArrayPtr(const SharedPtr<TOther>& other) :
00367       SharedPtr<CacheableArrayType<TObj, TYPEID> > (other)
00368     {
00369     }
00370 
00372     inline TObj operator [](uint32_t index) const
00373     {
00374       return SharedPtr<CacheableArrayType<TObj, TYPEID> >::ptr()-> operator [](
00375           index);
00376     }
00377 
00379     inline Serializable * fromData(DataInput & input)
00380     {
00381       return SharedPtr<CacheableArrayType<TObj, TYPEID> >::ptr()->fromData(input);
00382     }
00383   };
00384 
00386   template<typename TBase, int8_t TYPEID>
00387   class CacheableContainerType: public Cacheable, public TBase
00388   {
00389   protected:
00390 
00391     inline CacheableContainerType() :
00392       TBase()
00393     {
00394     }
00395 
00396     inline CacheableContainerType(const int32_t n) :
00397       TBase(n)
00398     {
00399     }
00400 
00401   public:
00402 
00403     // Cacheable methods
00404 
00406     virtual void toData(DataOutput& output) const
00407     {
00408       gemfire::serializer::writeObject(output, *this);
00409     }
00410 
00412     virtual Serializable* fromData(DataInput& input)
00413     {
00414       gemfire::serializer::readObject(input, *this);
00415       return this;
00416     }
00417 
00424     virtual int32_t classId() const
00425     {
00426       return 0;
00427     }
00428 
00435     virtual int8_t typeId() const
00436     {
00437       return TYPEID;
00438     }
00439 
00448     virtual uint32_t objectSize() const
00449     {
00450       return (uint32_t)(sizeof(CacheableContainerType) +
00451           gemfire::serializer::objectSize(*this));
00452     }
00453   };
00454 
00455 
00456 #ifdef _SOLARIS
00457 #define TEMPLATE_EXPORT template class
00458 #else
00459 #ifdef BUILD_CPPCACHE
00460 #define TEMPLATE_EXPORT template class CPPCACHE_EXPORT
00461 #else
00462 #define TEMPLATE_EXPORT extern template class CPPCACHE_EXPORT
00463 #endif
00464 #endif
00465 
00466 
00467 // Disable extern template warning on MSVC compiler
00468 #ifdef _MSC_VER
00469 #pragma warning(disable: 4231)
00470 #endif
00471 
00472 #define _GF_CACHEABLE_KEY_TYPE_DEF_(p, k, sz)                           \
00473   extern const char tName_##k [];                                       \
00474   extern const char tStr_##k [];                                        \
00475   TEMPLATE_EXPORT CacheableKeyType<p, GemfireTypeIds::k,                \
00476       tName_##k, tStr_##k, sz>;                                         \
00477   typedef CacheableKeyType<p, GemfireTypeIds::k,                        \
00478     tName_##k, tStr_##k, sz> _##k;                                      \
00479   class CPPCACHE_EXPORT k;                                              \
00480   typedef SharedPtr<k> k##Ptr;
00481 
00482 // use a class instead of typedef for bug #283
00483 #define _GF_CACHEABLE_KEY_TYPE_(p, k, sz)                               \
00484   class CPPCACHE_EXPORT k: public _##k                                  \
00485   {                                                                     \
00486   protected:                                                            \
00487     inline k() : _##k() { }                                             \
00488     inline k(const p value) : _##k(value) { }                           \
00489   public:                                                               \
00490      \
00491     static Serializable* createDeserializable() {                       \
00492       return new k();                                                   \
00493     }                                                                   \
00494            \
00495     inline static k##Ptr create() {                                     \
00496       return k##Ptr(new k());                                           \
00497     }                                                                   \
00498  \
00499     inline static k##Ptr create(const p value) {                        \
00500       return k##Ptr(new k(value));                                      \
00501     }                                                                   \
00502   };                                                                    \
00503   inline CacheableKeyPtr createKey(const p value) {                     \
00504     return k::create(value);                                            \
00505   }                                                                     \
00506   inline CacheablePtr createValue(const p value) {                      \
00507     return k::create(value);                                            \
00508   }                                                                     \
00509 
00510 
00511 #define _GF_CACHEABLE_ARRAY_TYPE_DEF_(p, c)                             \
00512   TEMPLATE_EXPORT CacheableArrayType<p, GemfireTypeIds::c>;             \
00513   typedef CacheableArrayType<p, GemfireTypeIds::c> _##c;                \
00514   class CPPCACHE_EXPORT c;                                              \
00515   typedef SharedArrayPtr<p, GemfireTypeIds::c> c##Ptr;
00516 
00517 // use a class instead of typedef for bug #283
00518 #define _GF_CACHEABLE_ARRAY_TYPE_(p, c)                                 \
00519   class CPPCACHE_EXPORT c: public _##c                                  \
00520   {                                                                     \
00521   protected:                                                            \
00522     inline c() : _##c() { }                                             \
00523     inline c(int32_t length) : _##c(length) { }                         \
00524     inline c(p* value, int32_t length) : _##c(value, length) { }        \
00525     inline c(const p* value, int32_t length, bool copy) :               \
00526       _##c(value, length, true) { }                                     \
00527   private:                                                              \
00528     /* Private to disable copy constructor and assignment operator. */  \
00529     c(const c& other);                                                  \
00530     c& operator =(const c& other);                                      \
00531   public:                                                               \
00532      \
00533     static Serializable* createDeserializable() {                       \
00534       return new c();                                                   \
00535     }                                                                   \
00536            \
00537     inline static c##Ptr create() {                                     \
00538       return c##Ptr(new c());                                           \
00539     }                                                                   \
00540   \
00541     inline static c##Ptr create(int32_t length) {                       \
00542       return c##Ptr(new c(length));                                     \
00543     }                                                                   \
00544        \
00545     inline static c##Ptr create(const p* value, int32_t length) {       \
00546       return (value != NULL ? c##Ptr(new c(value, length, true)) :      \
00547           NULLPTR);                                                     \
00548     }                                                                   \
00549                                                                  \
00557     inline static c##Ptr createNoCopy(p* value, int32_t length) {       \
00558       return (value != NULL ? c##Ptr(new c(value, length)) :            \
00559           NULLPTR);                                                     \
00560     }                                                                   \
00561   };
00562 
00563 
00564 #define _GF_CACHEABLE_CONTAINER_TYPE_DEF_(p, c)                         \
00565   TEMPLATE_EXPORT CacheableContainerType<p, GemfireTypeIds::c>;         \
00566   typedef CacheableContainerType<p, GemfireTypeIds::c> _##c;            \
00567   class CPPCACHE_EXPORT c;                                              \
00568   typedef SharedPtr<c> c##Ptr;
00569 
00570 // use a class instead of typedef for bug #283
00571 #define _GF_CACHEABLE_CONTAINER_TYPE_(p, c)                             \
00572   class CPPCACHE_EXPORT c: public _##c                                  \
00573   {                                                                     \
00574   protected:                                                            \
00575     inline c() : _##c() { }                                             \
00576     inline c(const int32_t n) : _##c(n) { }                              \
00577   public:                                                               \
00578                                       \
00579     typedef p::Iterator Iterator;                                       \
00580      \
00581     static Serializable* createDeserializable() {                       \
00582       return new c();                                                   \
00583     }                                                                   \
00584                \
00585     inline static c##Ptr create() {                                     \
00586       return c##Ptr(new c());                                           \
00587     }                                                                   \
00588   \
00589     inline static c##Ptr create(const int32_t n) {                       \
00590       return c##Ptr(new c(n));                                          \
00591     }                                                                   \
00592   };
00593 
00594 
00595 
00596   // Instantiations for the built-in CacheableKeys
00597 
00598   _GF_CACHEABLE_KEY_TYPE_DEF_(bool, CacheableBoolean, 3);
00603   _GF_CACHEABLE_KEY_TYPE_(bool, CacheableBoolean, 3);
00604 
00605   _GF_CACHEABLE_ARRAY_TYPE_DEF_(bool, BooleanArray);
00610   _GF_CACHEABLE_ARRAY_TYPE_(bool, BooleanArray);
00611 
00612   _GF_CACHEABLE_KEY_TYPE_DEF_(uint8_t, CacheableByte, 15);
00617   _GF_CACHEABLE_KEY_TYPE_(uint8_t, CacheableByte, 15);
00618 
00619   _GF_CACHEABLE_KEY_TYPE_DEF_(double, CacheableDouble, 63);
00624   _GF_CACHEABLE_KEY_TYPE_(double, CacheableDouble, 63);
00625 
00626   _GF_CACHEABLE_KEY_TYPE_DEF_(float, CacheableFloat, 63);
00631   _GF_CACHEABLE_KEY_TYPE_(float, CacheableFloat, 63);
00632 
00633   _GF_CACHEABLE_KEY_TYPE_DEF_(int16_t, CacheableInt16, 15);
00638   _GF_CACHEABLE_KEY_TYPE_(int16_t, CacheableInt16, 15);
00639 
00640   _GF_CACHEABLE_KEY_TYPE_DEF_(int32_t, CacheableInt32, 15);
00645   _GF_CACHEABLE_KEY_TYPE_(int32_t, CacheableInt32, 15);
00646 
00647   _GF_CACHEABLE_KEY_TYPE_DEF_(int64_t, CacheableInt64, 31);
00652   _GF_CACHEABLE_KEY_TYPE_(int64_t, CacheableInt64, 31);
00653 
00654   _GF_CACHEABLE_KEY_TYPE_DEF_(wchar_t, CacheableWideChar, 3);
00659   _GF_CACHEABLE_KEY_TYPE_(wchar_t, CacheableWideChar, 3);
00660 
00661 
00662   _GF_CACHEABLE_ARRAY_TYPE_DEF_(wchar_t, CharArray);
00667   _GF_CACHEABLE_ARRAY_TYPE_(wchar_t, CharArray);
00668 
00669   // Instantiations for array built-in Cacheables
00670 
00671   _GF_CACHEABLE_ARRAY_TYPE_DEF_(uint8_t, CacheableBytes);
00676   _GF_CACHEABLE_ARRAY_TYPE_(uint8_t, CacheableBytes);
00677 
00678   _GF_CACHEABLE_ARRAY_TYPE_DEF_(double, CacheableDoubleArray);
00683   _GF_CACHEABLE_ARRAY_TYPE_(double, CacheableDoubleArray);
00684 
00685   _GF_CACHEABLE_ARRAY_TYPE_DEF_(float, CacheableFloatArray);
00690   _GF_CACHEABLE_ARRAY_TYPE_(float, CacheableFloatArray);
00691 
00692   _GF_CACHEABLE_ARRAY_TYPE_DEF_(int16_t, CacheableInt16Array);
00697   _GF_CACHEABLE_ARRAY_TYPE_(int16_t, CacheableInt16Array);
00698 
00699   _GF_CACHEABLE_ARRAY_TYPE_DEF_(int32_t, CacheableInt32Array);
00704   _GF_CACHEABLE_ARRAY_TYPE_(int32_t, CacheableInt32Array);
00705 
00706   _GF_CACHEABLE_ARRAY_TYPE_DEF_(int64_t, CacheableInt64Array);
00711   _GF_CACHEABLE_ARRAY_TYPE_(int64_t, CacheableInt64Array);
00712 
00713   _GF_CACHEABLE_ARRAY_TYPE_DEF_(CacheableStringPtr, CacheableStringArray);
00718   _GF_CACHEABLE_ARRAY_TYPE_(CacheableStringPtr, CacheableStringArray);
00719 
00720 
00721   // Instantiations for container types (Vector/HashMap/HashSet) Cacheables
00722 
00723   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_VectorOfCacheable, CacheableVector);
00728   _GF_CACHEABLE_CONTAINER_TYPE_(_VectorOfCacheable, CacheableVector);
00729 
00730   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_HashMapOfCacheable, CacheableHashMap);
00735   _GF_CACHEABLE_CONTAINER_TYPE_(_HashMapOfCacheable, CacheableHashMap);
00736 
00737   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_HashSetOfCacheableKey, CacheableHashSet);
00742   _GF_CACHEABLE_CONTAINER_TYPE_(_HashSetOfCacheableKey, CacheableHashSet);
00743 
00744   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_VectorOfCacheable, CacheableArrayList);
00749   _GF_CACHEABLE_CONTAINER_TYPE_(_VectorOfCacheable, CacheableArrayList);
00750 
00751   //linketlist for JSON formattor issue
00752   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_VectorOfCacheable, CacheableLinkedList);
00757   _GF_CACHEABLE_CONTAINER_TYPE_(_VectorOfCacheable, CacheableLinkedList);
00758 
00759   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_VectorOfCacheable, CacheableStack);
00764   _GF_CACHEABLE_CONTAINER_TYPE_(_VectorOfCacheable, CacheableStack);
00765 
00766   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_HashMapOfCacheable, CacheableHashTable);
00771   _GF_CACHEABLE_CONTAINER_TYPE_(_HashMapOfCacheable, CacheableHashTable);
00772 
00773   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_HashMapOfCacheable,
00774       CacheableIdentityHashMap);
00782   _GF_CACHEABLE_CONTAINER_TYPE_(_HashMapOfCacheable,
00783       CacheableIdentityHashMap);
00784 
00785   _GF_CACHEABLE_CONTAINER_TYPE_DEF_(_HashSetOfCacheableKey,
00786       CacheableLinkedHashSet);
00794   _GF_CACHEABLE_CONTAINER_TYPE_(_HashSetOfCacheableKey,
00795       CacheableLinkedHashSet);
00796 }
00797 
00798 
00799 #endif // _GEMFIRE_CACHEABLE_BUILTINS_HPP_

GemFire C++ Cache API Documentation