RMF
traits.h
Go to the documentation of this file.
1 /**
2  * \file RMF/traits.h
3  * \brief Handle read/write of Model data from/to files.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef RMF_TRAITS_H
10 #define RMF_TRAITS_H
11 
12 #include "RMF/config.h"
13 #include "RMF/types.h"
14 #include <boost/cstdint.hpp>
15 #include <limits>
16 #include <cmath>
17 #if RMF_HAS_DEPRECATED_BACKENDS
18 #include "RMF/HDF5/types.h"
19 #endif
20 
21 RMF_ENABLE_WARNINGS
22 
23 namespace RMF {
24 
25 #ifndef SWIG
26 //! Traits classes describing how RMF uses types for storing data
27 /** Traits classes that describe how types are handled. Currently, adding a new
28  type is beyond the scope of the documentation, so traits is not documented.
29 */
30 template <class T>
31 struct Traits {};
32 
33 template <class T>
34 struct ValueTraitsBase {
35  typedef T Type;
36  typedef std::vector<T> Types;
37  typedef T ArgumentType;
38  typedef T ReturnType;
39  static bool get_are_equal(T a, T b) { return a == b; }
40 };
41 
42 template <class T>
43 struct ReferenceTraitsBase {
44  typedef T Type;
45  typedef std::vector<T> Types;
46  typedef const T& ArgumentType;
47  typedef const T& ReturnType;
48 };
49 
50 template <>
51 struct Traits<Int> : public ValueTraitsBase<Int> {
52  static bool get_is_null_value(const Type& t) { return t == get_null_value(); }
53  static ReturnType get_null_value() {
54  return std::numeric_limits<Type>::max();
55  }
56 #if RMF_HAS_DEPRECATED_BACKENDS
57  typedef HDF5::IntTraits HDF5Traits;
58  typedef boost::int32_t AvroType;
59 #endif
60  static std::string get_tag() { return "ki"; }
61  static std::string get_name() { return "int"; }
62 };
63 
64 template <>
65 struct Traits<Float> : public ValueTraitsBase<Float> {
66  static bool get_is_null_value(const Type& t) {
67  return t >= std::numeric_limits<Float>::max();
68  }
69  static ReturnType get_null_value() {
70  return std::numeric_limits<Type>::infinity();
71  }
72 #if RMF_HAS_DEPRECATED_BACKENDS
73  typedef HDF5::FloatTraits HDF5Traits;
74  typedef double AvroType;
75 #endif
76  static bool get_are_equal(ArgumentType a, ArgumentType b) {
77  return std::abs(a - b) < .0000001 * std::abs(a + b) + .000000001;
78  }
79  static std::string get_tag() { return "kf"; }
80  static std::string get_name() { return "float"; }
81 };
82 
83 template <>
84 struct Traits<String> : public ValueTraitsBase<String> {
85  static bool get_is_null_value(const Type& t) { return t.empty(); }
86  static ReturnType get_null_value() {
87  static Type r;
88  return r;
89  }
90 #if RMF_HAS_DEPRECATED_BACKENDS
91  typedef HDF5::StringTraits HDF5Traits;
92  typedef Type AvroType;
93 #endif
94  static std::string get_tag() { return "ks"; }
95  static std::string get_name() { return "string"; }
96 };
97 
98 template <class T>
99 struct SequenceTraitsBase {
100  typedef std::vector<T> Type;
101  typedef std::vector<Type> Types;
102  typedef Type ReturnType;
103  typedef const Type& ArgumentType;
104  static bool get_is_null_value(const Type& t) { return t.empty(); }
105  static ReturnType get_null_value() {
106  static Type r;
107  return r;
108  }
109  static bool get_are_equal(ArgumentType a, ArgumentType b) {
110  if (a.size() != b.size()) return false;
111  for (unsigned int i = 0; i < a.size(); ++i) {
112  if (!Traits<T>::get_are_equal(a[i], b[i])) return false;
113  }
114  return true;
115  }
116  static std::string get_tag() { return Traits<T>::get_tag() + "s"; }
117  static std::string get_name() { return Traits<T>::get_name() + "s"; }
118 };
119 
120 template <>
121 struct Traits<Ints> : public SequenceTraitsBase<Int> {
122 #if RMF_HAS_DEPRECATED_BACKENDS
123  typedef HDF5::IntsTraits HDF5Traits;
124  typedef std::vector<Traits<Int>::AvroType> AvroType;
125 #endif
126 };
127 
128 template <>
129 struct Traits<Floats> : public SequenceTraitsBase<Float> {
130 #if RMF_HAS_DEPRECATED_BACKENDS
131  typedef HDF5::FloatsTraits HDF5Traits;
132  typedef std::vector<double> AvroType;
133 #endif
134 };
135 
136 template <>
137 struct Traits<Strings> : public SequenceTraitsBase<String> {
138 #if RMF_HAS_DEPRECATED_BACKENDS
139  typedef HDF5::StringsTraits HDF5Traits;
140  typedef Type AvroType;
141 #endif
142 };
143 
144 template <unsigned int D>
145 class Traits<Vector<D> > : public ReferenceTraitsBase<Vector<D> > {
146  static std::string make_tag() {
147  std::ostringstream oss;
148  oss << "v" << D;
149  return oss.str();
150  }
151  static std::string make_name() {
152  std::ostringstream oss;
153  oss << "vector" << D;
154  return oss.str();
155  }
156 
157  public:
158  static bool get_is_null_value(const RMF_VECTOR<D>& t) {
159  return t[0] > std::numeric_limits<double>::max();
160  }
161  static const RMF_VECTOR<D>& get_null_value() {
162  static const RMF_VECTOR<D> null(Floats(
163  D, std::numeric_limits<typename Traits<Float>::Type>::infinity()));
164  return null;
165  }
166  static bool get_are_equal(const RMF_VECTOR<D>& a, const RMF_VECTOR<D>& b) {
167  for (unsigned int i = 0; i < D; ++i) {
168  if (!Traits<Float>::get_are_equal(a[i], b[i])) return false;
169  }
170  return true;
171  }
172  static std::string get_tag() {
173  static std::string tag = make_tag();
174  return tag;
175  }
176  static std::string get_name() {
177  static std::string name = make_name();
178  return name;
179  }
180 };
181 
182 template <unsigned int D>
183 struct Traits<std::vector<Vector<D> > > : public SequenceTraitsBase<
184  Vector<D> > {};
185 
186 typedef Traits<Int> IntTraits;
187 typedef Traits<Float> FloatTraits;
188 typedef Traits<String> StringTraits;
189 typedef Traits<Ints> IntsTraits;
190 typedef Traits<Floats> FloatsTraits;
191 typedef Traits<Strings> StringsTraits;
192 typedef Traits<Vector<3> > Vector3Traits;
193 typedef Traits<Vector<4> > Vector4Traits;
194 typedef Traits<std::vector<Vector<3> > > Vector3sTraits;
195 typedef Traits<std::vector<Vector<4> > > Vector4sTraits;
196 
197 /*struct IntTag : public IntTraits {};
198 struct FloatTag : public FloatTraits {};
199 struct StringTag : public StringTraits {};
200 struct Vector3Tag : public Vector3Traits {};
201 struct Vector4Tag : public Vector4Traits {};
202 struct IntsTag : public IntsTraits {};
203 struct FloatsTag : public FloatsTraits {};
204 struct StringsTag : public StringsTraits {};
205 struct Vector3sTag : public Vector3sTag {};
206 struct Vector4sTag : public Vector4sTraits {};*/
207 typedef IntTraits IntTag;
208 typedef FloatTraits FloatTag;
209 typedef StringTraits StringTag;
210 typedef Vector3Traits Vector3Tag;
211 typedef Vector4Traits Vector4Tag;
212 typedef IntsTraits IntsTag;
213 typedef FloatsTraits FloatsTag;
214 typedef StringsTraits StringsTag;
215 typedef Vector3sTraits Vector3sTag;
216 typedef Vector4sTraits Vector4sTag;
217 
218 #else
219 struct IntTag {};
220 struct FloatTag {};
221 struct StringTag {};
222 struct Vector3Tag {};
223 struct Vector4Tag {};
224 struct IntsTag {};
225 struct FloatsTag {};
226 struct StringsTag {};
227 struct Vector3sTag {};
228 struct Vector4sTag {};
229 #endif
230 
231 } /* namespace RMF */
232 
233 RMF_DISABLE_WARNINGS
234 
235 #endif /* RMF_TRAITS_H */
Default implementation for types.h.
float Float
Definition: types.h:33
std::vector< String > Strings
Definition: types.h:41
std::vector< Int > Ints
Definition: types.h:31
std::string String
Definition: types.h:39
std::vector< Float > Floats
Definition: types.h:35
Handle read/write of Model data from/to files.
int Int
Definition: types.h:29
Traits classes describing how RMF uses types for storing data.
Definition: traits.h:31