Tapkee
policy.hpp
Go to the documentation of this file.
00001 /* This software is distributed under BSD 3-clause license (see LICENSE file).
00002  *
00003  * Copyright (c) 2012-2013 Sergey Lisitsyn
00004  */
00005 
00006 #ifndef TAPKEE_POLICY_H_
00007 #define TAPKEE_POLICY_H_
00008 
00009 namespace tapkee
00010 {
00011 namespace tapkee_internal
00012 {
00013 
00014 struct TypePolicyBase
00015 {
00016     virtual ~TypePolicyBase() {}
00017     virtual void copyFromValue(void const*, void**) const = 0;
00018     virtual void* getValue(void**) const = 0;
00019     virtual void free(void**) const = 0;
00020     virtual void clone(void* const*, void**) const = 0;
00021     virtual void move(void* const*, void**) const = 0;
00022 };
00023 
00024 template <typename T>
00025 struct PointerTypePolicyImpl : public TypePolicyBase
00026 {
00027     inline virtual void copyFromValue(void const* src, void** dest) const
00028     {
00029         *dest = new T(*reinterpret_cast<T const*>(src));
00030     }
00031     inline virtual void* getValue(void** src) const
00032     {
00033         return *src;
00034     }
00035     inline virtual void free(void** src) const
00036     {
00037         if (*src) 
00038             delete (*reinterpret_cast<T**>(src));
00039         *src = NULL;
00040     }
00041     virtual void clone(void* const* src, void** dest) const
00042     {
00043         if (*dest) 
00044             (*reinterpret_cast<T**>(dest))->~T();
00045         *dest = new T(**reinterpret_cast<T* const*>(src));
00046     }
00047     inline virtual void move(void* const* src, void** dest) const
00048     {
00049         (*reinterpret_cast<T**>(dest))->~T();
00050         **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src); 
00051     }
00052 };
00053 
00054 template <typename T>
00055 TypePolicyBase* getPolicy()
00056 {
00057     static PointerTypePolicyImpl<T> policy;
00058     return &policy;
00059 }
00060 
00061 struct CheckerPolicyBase
00062 {
00063     virtual ~CheckerPolicyBase() {}
00064     virtual bool isInRange(void* const*, void*, void*) const = 0;
00065     virtual bool isEqual(void* const*, void*) const = 0;
00066     virtual bool isNotEqual(void* const*, void*) const = 0;
00067     virtual bool isPositive(void* const*) const = 0;
00068     virtual bool isNonNegative(void * const*) const = 0;
00069     virtual bool isNegative(void* const*) const = 0;
00070     virtual bool isNonPositive(void * const*) const = 0;
00071     virtual bool isGreater(void* const*, void*) const = 0;
00072     virtual bool isLesser(void* const*, void*) const = 0;
00073 };
00074 
00075 template <typename T>
00076 struct PointerCheckerPolicyImpl : public CheckerPolicyBase
00077 {
00078     virtual bool isInRange(void* const*, void*, void*) const
00079     {
00080         return false;
00081     }
00082     virtual bool isEqual(void* const*, void*) const
00083     {
00084         return false;
00085     }
00086     virtual bool isNotEqual(void* const*, void*) const
00087     {
00088         return false;
00089     }
00090     virtual bool isPositive(void* const*) const
00091     {
00092         return false;
00093     }
00094     virtual bool isNonNegative(void* const*) const
00095     {
00096         return false;
00097     }
00098     virtual bool isNegative(void* const*) const
00099     {
00100         return false;
00101     }
00102     virtual bool isNonPositive(void* const*) const
00103     {
00104         return false;
00105     }
00106     virtual bool isGreater(void* const*, void*) const
00107     {
00108         return false;
00109     }
00110     virtual bool isLesser(void* const*, void*) const
00111     {
00112         return false;
00113     }
00114 };
00115 
00116 #define default_policy_for(Type) \
00117 template <>                                                                     \
00118 struct PointerCheckerPolicyImpl<Type> : public CheckerPolicyBase                \
00119 {                                                                               \
00120     inline Type value(void* v) const                                            \
00121     {                                                                           \
00122         return *reinterpret_cast<Type*>(v);                                     \
00123     }                                                                           \
00124     virtual bool isInRange(void* const* src, void* lower, void* upper) const    \
00125     {                                                                           \
00126         Type v = value(*src);                                                   \
00127         Type l = value(lower);                                                  \
00128         Type u = value(upper);                                                  \
00129         return (v>=l) && (v<u);                                                 \
00130     }                                                                           \
00131     virtual bool isEqual(void* const* src, void* other_src) const               \
00132     {                                                                           \
00133         Type v = value(*src);                                                   \
00134         Type ov = value(other_src);                                             \
00135         return (v==ov);                                                         \
00136     }                                                                           \
00137     virtual bool isNotEqual(void* const* src, void* other_src) const            \
00138     {                                                                           \
00139         Type v = value(*src);                                                   \
00140         Type ov = value(other_src);                                             \
00141         return (v!=ov);                                                         \
00142     }                                                                           \
00143     virtual bool isPositive(void* const* src) const                             \
00144     {                                                                           \
00145         Type v = value(*src);                                                   \
00146         return (v>0);                                                           \
00147     }                                                                           \
00148     virtual bool isNonNegative(void* const* src) const                          \
00149     {                                                                           \
00150         Type v = value(*src);                                                   \
00151         return (v>=0);                                                          \
00152     }                                                                           \
00153     virtual bool isNegative(void* const* src) const                             \
00154     {                                                                           \
00155         Type v = value(*src);                                                   \
00156         return (v<0);                                                           \
00157     }                                                                           \
00158     virtual bool isNonPositive(void* const* src) const                          \
00159     {                                                                           \
00160         Type v = value(*src);                                                   \
00161         return (v<=0);                                                          \
00162     }                                                                           \
00163     virtual bool isGreater(void* const* src, void* lower) const                 \
00164     {                                                                           \
00165         Type v = value(*src);                                                   \
00166         return (v>value(lower));                                                \
00167     }                                                                           \
00168     virtual bool isLesser(void* const* src, void* upper) const                  \
00169     {                                                                           \
00170         Type v = value(*src);                                                   \
00171         return (v<value(upper));                                                \
00172     }                                                                           \
00173 }
00174 
00175 default_policy_for(double);
00176 default_policy_for(float);
00177 default_policy_for(int);
00178 
00179 template <typename T>
00180 CheckerPolicyBase* getCheckerPolicy()
00181 {
00182     static PointerCheckerPolicyImpl<T> policy;
00183     return &policy;
00184 }
00185 
00186 }
00187 }
00188 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines