Tapkee
policy.hpp
Go to the documentation of this file.
1 
29 #ifndef STICHWORT_POLICY_H_
30 #define STICHWORT_POLICY_H_
31 
32 #include <string>
33 #include <sstream>
34 
35 namespace stichwort
36 {
37 namespace stichwort_internal
38 {
39 namespace streams_sfinae
40 {
41  typedef char yes;
42  typedef long no;
43 
44  struct any_wrapper
45  {
46  template <class T> any_wrapper(const T&);
47  };
48  no operator<<(const any_wrapper&, const any_wrapper&);
49  template <class T> yes check(T const&);
50  no check(no);
51 
52  template <typename S, typename T>
54  {
55  static S& s;
56  static T& x;
57  static const bool value = sizeof(check(s << x)) == sizeof(yes);
58  };
59 }
60 
62 {
63  virtual ~TypePolicyBase() {}
64  virtual void copyFromValue(void const*, void**) const = 0;
65  virtual void* getValue(void**) const = 0;
66  virtual void free(void**) const = 0;
67  virtual void clone(void* const*, void**) const = 0;
68  virtual void move(void* const*, void**) const = 0;
69  virtual std::string repr(void **) const = 0;
70 };
71 
72 template <typename T, bool>
74 {
75  std::string operator()(const TypePolicyBase* const impl, void** src) const;
76 };
77 
78 template <typename T>
80 {
81  inline virtual void copyFromValue(void const* src, void** dest) const
82  {
83  *dest = new T(*reinterpret_cast<T const*>(src));
84  }
85  inline virtual void* getValue(void** src) const
86  {
87  return *src;
88  }
89  inline virtual void free(void** src) const
90  {
91  if (*src)
92  delete (*reinterpret_cast<T**>(src));
93  *src = NULL;
94  }
95  virtual void clone(void* const* src, void** dest) const
96  {
97  if (*dest)
98  (*reinterpret_cast<T**>(dest))->~T();
99  *dest = new T(**reinterpret_cast<T* const*>(src));
100  }
101  inline virtual void move(void* const* src, void** dest) const
102  {
103  (*reinterpret_cast<T**>(dest))->~T();
104  **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
105  }
106  inline virtual std::string repr(void** src) const
107  {
109  }
110 };
111 
112 struct EmptyType;
113 
114 template <>
115 std::string PointerTypePolicyImpl<EmptyType>::repr(void**) const
116 {
117  return "uninitialized";
118 }
119 
120 template <typename T>
122 {
123  std::string operator()(const TypePolicyBase* const impl, void** src) const
124  {
125  void* vv = impl->getValue(src);
126  T* vp = reinterpret_cast<T*>(vv);
127  T v = *vp;
128  std::stringstream ss;
129  ss << v;
130  return ss.str();
131  }
132 };
133 
134 template <typename T>
136 {
137  std::string operator()(const TypePolicyBase* const, void**) const
138  {
139  return "(can't obtain value)";
140  }
141 };
142 
143 template <typename T>
145 {
146  static PointerTypePolicyImpl<T> policy;
147  return &policy;
148 }
149 
150 }
151 }
152 #endif
virtual void * getValue(void **) const =0
TypePolicyBase * getPolicy()
Definition: policy.hpp:144
The namespace that contains implementations for the keywords.
virtual void clone(void *const *src, void **dest) const
Definition: policy.hpp:95
std::string operator()(const TypePolicyBase *const, void **) const
Definition: policy.hpp:137
no operator<<(const any_wrapper &, const any_wrapper &)
std::string operator()(const TypePolicyBase *const impl, void **src) const
Definition: policy.hpp:123
virtual std::string repr(void **src) const
Definition: policy.hpp:106
virtual void * getValue(void **src) const
Definition: policy.hpp:85
virtual void move(void *const *src, void **dest) const
Definition: policy.hpp:101
virtual void copyFromValue(void const *src, void **dest) const
Definition: policy.hpp:81