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
21RMF_ENABLE_WARNINGS
22
23namespace 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*/
30template <class T>
31struct Traits {};
32
33template <class T>
34struct 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
42template <class T>
43struct ReferenceTraitsBase {
44 typedef T Type;
45 typedef std::vector<T> Types;
46 typedef const T& ArgumentType;
47 typedef const T& ReturnType;
48};
49
50template <>
51struct 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
64template <>
65struct 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
83template <>
84struct 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
98template <class T>
99struct 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
120template <>
121struct 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
128template <>
129struct 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
136template <>
137struct Traits<Strings> : public SequenceTraitsBase<String> {
138#if RMF_HAS_DEPRECATED_BACKENDS
139 typedef HDF5::StringsTraits HDF5Traits;
140 typedef Type AvroType;
141#endif
142};
143
144template <unsigned int D>
145class 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
182template <unsigned int D>
183struct Traits<std::vector<Vector<D> > > : public SequenceTraitsBase<
184 Vector<D> > {};
185
186typedef Traits<Int> IntTraits;
187typedef Traits<Float> FloatTraits;
188typedef Traits<String> StringTraits;
189typedef Traits<Ints> IntsTraits;
190typedef Traits<Floats> FloatsTraits;
191typedef Traits<Strings> StringsTraits;
192typedef Traits<Vector<3> > Vector3Traits;
193typedef Traits<Vector<4> > Vector4Traits;
194typedef Traits<std::vector<Vector<3> > > Vector3sTraits;
195typedef Traits<std::vector<Vector<4> > > Vector4sTraits;
196
197/*struct IntTag : public IntTraits {};
198struct FloatTag : public FloatTraits {};
199struct StringTag : public StringTraits {};
200struct Vector3Tag : public Vector3Traits {};
201struct Vector4Tag : public Vector4Traits {};
202struct IntsTag : public IntsTraits {};
203struct FloatsTag : public FloatsTraits {};
204struct StringsTag : public StringsTraits {};
205struct Vector3sTag : public Vector3sTag {};
206struct Vector4sTag : public Vector4sTraits {};*/
207typedef IntTraits IntTag;
208typedef FloatTraits FloatTag;
209typedef StringTraits StringTag;
210typedef Vector3Traits Vector3Tag;
211typedef Vector4Traits Vector4Tag;
212typedef IntsTraits IntsTag;
213typedef FloatsTraits FloatsTag;
214typedef StringsTraits StringsTag;
215typedef Vector3sTraits Vector3sTag;
216typedef Vector4sTraits Vector4sTag;
217
218#else
219struct IntTag {};
220struct FloatTag {};
221struct StringTag {};
222struct Vector3Tag {};
223struct Vector4Tag {};
224struct IntsTag {};
225struct FloatsTag {};
226struct StringsTag {};
227struct Vector3sTag {};
228struct Vector4sTag {};
229#endif
230
231} /* namespace RMF */
232
233RMF_DISABLE_WARNINGS
234
235#endif /* RMF_TRAITS_H */
Handle read/write of Model data from/to files.
Represent a point in some dimension.
Definition Vector.h:33
The namespace for all functionality in the library.
std::string String
Definition types.h:39
int Int
Definition types.h:29
std::vector< Int > Ints
Definition types.h:31
float Float
Definition types.h:33
std::vector< String > Strings
Definition types.h:41
std::vector< Float > Floats
Definition types.h:35
Traits classes describing how RMF uses types for storing data.
Definition traits.h:31
Default implementation for types.h.
#define RMF_VECTOR
For classes that are templated on the dimension.
Definition types.h:23