Atoms Crowd  7.0.0
MetadataImpl.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <AtomsCore/Globals.h>
11 #include <AtomsCore/Metadata/Metadata.h>
12 #include <AtomsCore/Metadata/MetadataTraits.h>
13 #include <sstream>
14 
15 #ifdef _WIN32
16  #define METADATA_SPEC_DECLARE(TYPE) template class ATOMSCORE_EXPORT MetadataImpl<TYPE>;
17 #else
18  #define METADATA_SPEC_DECLARE(TYPE)
19 #endif
20 
21 #define DECLARE_METADATA(NAME, TYPE) \
22 typedef MetadataImpl<TYPE> NAME; \
23 template<> ATOMSCORE_EXPORT std::string MetadataImpl<TYPE>::staticTypeStr(); \
24 template<> ATOMSCORE_EXPORT unsigned int MetadataImpl<TYPE>::staticTypeId(); \
25 template<> ATOMSCORE_EXPORT std::string MetadataImpl<TYPE>::staticBaseTypeStr(); \
26 METADATA_SPEC_DECLARE(TYPE) \
27 
28 #define DECLARE_METADATA_WITH_BLEND(NAME, TYPE) \
29 typedef MetadataImpl<TYPE> NAME; \
30 template<> ATOMSCORE_EXPORT std::string MetadataImpl<TYPE>::staticTypeStr(); \
31 template<> ATOMSCORE_EXPORT unsigned int MetadataImpl<TYPE>::staticTypeId(); \
32 template<> ATOMSCORE_EXPORT std::string MetadataImpl<TYPE>::staticBaseTypeStr(); \
33 template<> ATOMSCORE_EXPORT bool MetadataImpl<TYPE>::setWithValuesAndWeights(const std::vector<AtomsPtr<Metadata>> &values, const std::vector<double> &weights); \
34 template<> ATOMSCORE_EXPORT bool MetadataImpl<TYPE>::setWithValuesAndWeights(const std::vector<Metadata *> &values, const std::vector<double> &weights); \
35 METADATA_SPEC_DECLARE(TYPE) \
36 
37 #define DEFINE_METADATA_TYPE(NAME, TYPE, ID) \
38 template<> std::string MetadataImpl<TYPE>::staticTypeStr() { return #NAME; } \
39 template<> unsigned int MetadataImpl<TYPE>::staticTypeId() { return ID; } \
40 template<> std::string MetadataImpl<TYPE>::staticBaseTypeStr() { return #TYPE; } \
41 template class MetadataImpl<TYPE>; \
42 
43 #define DEFINE_METADATA_BASE_BLEND(TYPE, CAST_TYPE) \
44 template<> bool MetadataImpl<TYPE>::setWithValuesAndWeights(const std::vector<AtomsPtr<Metadata>> &values, const std::vector<double> &weights) \
45 { \
46  size_t vSize = values.size(); \
47  size_t wSize = weights.size(); \
48  if (vSize == 0 || vSize != wSize) \
49  return false; \
50  m_value = static_cast<TYPE>(static_cast<CAST_TYPE>(std::static_pointer_cast<MetadataImpl<TYPE>>(values[0])->get()) * weights[0]); \
51  for (size_t i = 1; i < vSize; ++i) \
52  m_value += static_cast<TYPE>(static_cast<CAST_TYPE>(std::static_pointer_cast<MetadataImpl<TYPE>>(values[i])->get()) * weights[i]); \
53  return true; \
54 } \
55 \
56 template<> bool MetadataImpl<TYPE>::setWithValuesAndWeights(const std::vector<Metadata *> &values, const std::vector<double> &weights) \
57 { \
58  size_t vSize = values.size(); \
59  size_t wSize = weights.size(); \
60  if (vSize == 0 || vSize != wSize) \
61  return false; \
62  m_value = static_cast<TYPE>(static_cast<CAST_TYPE>(static_cast<MetadataImpl<TYPE> *>(values[0])->get()) * weights[0]); \
63  for (size_t i = 1; i < vSize; ++i) \
64  m_value += static_cast<TYPE>(static_cast<CAST_TYPE>(static_cast<MetadataImpl<TYPE> *>(values[i])->get()) * weights[i]); \
65  return true; \
66 }\
67 
68 #define DEFINE_METADATA_TYPE_WITH_BLEND(NAME, TYPE, ID) \
69 DEFINE_METADATA_TYPE(NAME, TYPE, ID) \
70 DEFINE_METADATA_BASE_BLEND(TYPE, double) \
71 
72 #define DEFINE_METADATA_TYPE_WITH_VECTOR_TYPE_BLEND(NAME, TYPE, ID) \
73 DEFINE_METADATA_TYPE(NAME, TYPE, ID) \
74 DEFINE_METADATA_BASE_BLEND(TYPE, TYPE) \
75 
76 #define DECLARE_MEADATA_COMPARISION(TYPE) \
77 template<> ATOMSCORE_EXPORT bool MetadataImpl<TYPE>::isEqual(Metadata* right, double err) const; \
78 template<> ATOMSCORE_EXPORT bool MetadataImpl<TYPE>::isGreater(Metadata* right) const; \
79 template<> ATOMSCORE_EXPORT bool MetadataImpl<TYPE>::isLess(Metadata* right) const; \
80 
81 #define DEFINE_MEADATA_BASE_COMPARISION(TYPE) \
82 template<> bool MetadataImpl<TYPE>::isEqual(Metadata* right, double err) const \
83 { \
84  if (this == right) return true; \
85  if (!right || right->typeId() != typeId()) return false; \
86  return m_value == static_cast<MetadataImpl<TYPE>*>(right)->get();\
87 }\
88 \
89 template<> bool MetadataImpl<TYPE>::isGreater(Metadata* right) const \
90 {\
91  if (this == right) return false; \
92  if (!right || right->typeId() != typeId()) return false; \
93  return m_value > static_cast<MetadataImpl<TYPE>*>(right)->get();\
94 }\
95 \
96 template<> bool MetadataImpl<TYPE>::isLess(Metadata* right) const\
97 {\
98  if (this == right) return false; \
99  if (!right || right->typeId() != typeId()) return false; \
100  return m_value < static_cast<MetadataImpl<TYPE>*>(right)->get();\
101 }\
102 
103 #define DEFINE_MEADATA_BASE_ABS_COMPARISION(TYPE) \
104 template<> bool MetadataImpl<TYPE>::isEqual(Metadata* right, double err) const \
105 { \
106  if (this == right) return true; \
107  if (!right || right->typeId() != typeId()) return false; \
108  return std::abs(m_value - static_cast<MetadataImpl<TYPE>*>(right)->get()) < err;\
109 }\
110 \
111 template<> bool MetadataImpl<TYPE>::isGreater(Metadata* right) const \
112 {\
113  if (this == right) return false; \
114  if (!right || right->typeId() != typeId()) return false; \
115  return m_value > static_cast<MetadataImpl<TYPE>*>(right)->get();\
116 }\
117 \
118 template<> bool MetadataImpl<TYPE>::isLess(Metadata* right) const\
119 {\
120  if (this == right) return false; \
121  if (!right || right->typeId() != typeId()) return false; \
122  return m_value < static_cast<MetadataImpl<TYPE>*>(right)->get();\
123 }\
124 
125 #define DEFINE_MEADATA_VECTOR_COMPARISION(TYPE) \
126 template<> bool MetadataImpl<TYPE>::isEqual(Metadata* right, double err) const \
127 { \
128  if (this == right) return true; \
129  if (right->typeId() != typeId()) return false; \
130  return m_value.equalWithAbsError(static_cast<MetadataImpl<TYPE>*>(right)->get(), err);\
131 }\
132 \
133 template<> bool MetadataImpl<TYPE>::isGreater(Metadata* right) const \
134 {\
135  if (this == right) return false; \
136  if (right->typeId() != typeId()) return false; \
137  auto& rightValue = static_cast<MetadataImpl<TYPE>*>(right)->get(); \
138  for (unsigned int i = 0; i < m_value.dimensions(); ++i) \
139  if (m_value[i] < rightValue[i]) return false; \
140  return true;\
141 }\
142 \
143 template<> bool MetadataImpl<TYPE>::isLess(Metadata* right) const\
144 {\
145  if (this == right) return false; \
146  if (right->typeId() != typeId()) return false; \
147  auto& rightValue = static_cast<MetadataImpl<TYPE>*>(right)->get(); \
148  for (unsigned int i = 0; i < m_value.dimensions(); ++i) \
149  if (m_value[i] > rightValue[i]) return false; \
150  return true;\
151 }\
152 
153 namespace AtomsCore
154 {
156  template <typename T>
157  class MetadataImpl : public Metadata
158  {
159  public:
160 
162  typedef T valueType;
163 
165  /*
166  \return class type string
167  */
168  std::string typeStr() const;
169 
171  /*
172  \return class type id
173  */
174  unsigned int typeId() const;
175 
177  static std::string staticTypeStr();
178 
180  static unsigned int staticTypeId();
181 
183  static std::string staticBaseTypeStr();
184 
186  MetadataImpl();
187 
189  /*
190  \param value Initial value
191  */
192  MetadataImpl(const T& value);
193 
195  MetadataImpl(const MetadataImpl& other);
196 
198  ~MetadataImpl();
199 
201  MetadataImpl& operator=(const MetadataImpl& other);
202 
204  /*
205  Virtual function to copy the value from one metadata to another using base pointer.
206  \param other
207  */
208  virtual void copy(Metadata *other);
209 
211  /*
212  Clone the obj. It returns a shared pointer to the new allocated object
213  */
214  virtual AtomsPtr<Metadata> clone() const;
215 
217  /*
218  Memory used when serialized
219  */
220  virtual size_t memSize() const;
221 
223  T value() const;
224 
226  /*
227  Get a reference to the stored value
228  */
229  T& get();
230 
232  /*
233  Get a const reference to the stored value
234  */
235  const T& get() const;
236 
238  void set(const T& value);
239 
240 
242  /*
243  \return true if the operation was successfull, false otherwise
244  */
245  virtual bool setWithValuesAndWeights(const std::vector<AtomsPtr<Metadata>> &values, const std::vector<double> &weights);
246  virtual bool setWithValuesAndWeights(const std::vector<Metadata *> &values, const std::vector<double> &weights);
247 
249  virtual void hash(MurmurHash3& hasher) const;
250 
251 
253  virtual bool isEqual(Metadata* right, double err = 0.0) const;
254 
256  virtual bool isGreater(Metadata* right) const;
257 
259  virtual bool isLess(Metadata* right) const;
260 
261  virtual void* dataPtr() { return reinterpret_cast<void*>(&m_value); }
262 
263  public:
264 
266 
270  static Metadata* creator();
271 
273  bool serialise(Archive& outArchive) const;
274 
276  bool deserialise(Archive& inStream);
277 
279  virtual void toString(std::stringstream& ss) const;
280 
281  private:
282 
284  valueType m_value;
285  };
286 }
287 
288 #include "MetadataImpl.impl.h"
Archive class.
Definition: Serialiser.h:29
Base Metadata class.
Definition: Metadata.h:24
Metadata template class.
Definition: MetadataImpl.h:158
bool serialise(Archive &outArchive) const
Binary Serialise.
Definition: MetadataImpl.impl.h:100
T & get()
Get reference.
Definition: MetadataImpl.impl.h:88
virtual bool isGreater(Metadata *right) const
Is greater then.
Definition: MetadataImpl.impl.h:161
MetadataImpl()
Default constructor.
Definition: MetadataImpl.impl.h:4
std::string typeStr() const
Type string.
Definition: MetadataImpl.impl.h:58
bool deserialise(Archive &inStream)
Binary Deserialise.
Definition: MetadataImpl.impl.h:113
~MetadataImpl()
Destructor.
Definition: MetadataImpl.impl.h:19
virtual void toString(std::stringstream &ss) const
String conversion.
Definition: MetadataImpl.impl.h:122
T value() const
Get value.
Definition: MetadataImpl.impl.h:76
virtual bool isEqual(Metadata *right, double err=0.0) const
Equal function.
Definition: MetadataImpl.impl.h:150
virtual void hash(MurmurHash3 &hasher) const
Hash function.
Definition: MetadataImpl.impl.h:144
virtual bool isLess(Metadata *right) const
Is less then.
Definition: MetadataImpl.impl.h:167
virtual bool setWithValuesAndWeights(const std::vector< AtomsPtr< Metadata >> &values, const std::vector< double > &weights)
Set with weighted values.
Definition: MetadataImpl.impl.h:132
T valueType
value type
Definition: MetadataImpl.h:162
static unsigned int staticTypeId()
Class static type string.
MetadataImpl & operator=(const MetadataImpl &other)
Assign operator.
Definition: MetadataImpl.impl.h:31
void set(const T &value)
Set value.
Definition: MetadataImpl.impl.h:70
virtual AtomsPtr< Metadata > clone() const
clone object
Definition: MetadataImpl.impl.h:46
unsigned int typeId() const
Type id.
Definition: MetadataImpl.impl.h:64
virtual size_t memSize() const
Memory size.
Definition: MetadataImpl.impl.h:52
static std::string staticBaseTypeStr()
Class static base type string.
static Metadata * creator()
Creator function.
Definition: MetadataImpl.impl.h:94
static std::string staticTypeStr()
Class static type string.
virtual void copy(Metadata *other)
copy value
Definition: MetadataImpl.impl.h:38
Definition: Hash.h:23
AtomsCore namespace.
Definition: Agent.h:344