forpy  2
storage.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef FORPY_UTIL_STORAGE_H_
3 #define FORPY_UTIL_STORAGE_H_
4 
5 #ifdef __GNUC__
6 #ifndef __clang__
7 #pragma GCC diagnostic push
8 #pragma GCC diagnostic ignored "-Wreturn-local-addr"
9 #endif
10 #endif
11 #include <mapbox/variant_cast.hpp>
12 #ifdef __GNUC__
13 #ifndef __clang__
14 #pragma GCC diagnostic pop
15 #endif
16 #endif
17 #include "./serialization/variant.h"
18 
19 namespace forpy {
20 
53 
54 template <typename... Ts>
55 struct ptr_variant : mu::variant<Ts...> {
56  using Base = mu::variant<Ts...>;
57  using Base::Base;
58 };
59 
67 struct Empty {
68  template <typename Archive>
69  void serialize(Archive &, const uint &){};
70  bool operator==(const Empty &) const { return true; };
71  float *data() const {
72  throw ForpyException("Trying to access an empty data storage.");
73  return nullptr; };
74  size_t &innerStride() const {
75  throw ForpyException("Trying to access an empty data storage.");
76  };
77  size_t &outerStride() const {
78  throw ForpyException("Trying to access an empty data storage.");
79  };
80  friend std::ostream &operator<<(std::ostream &stream, const Empty &) {
81  stream << "forpy::Empty";
82  return stream;
83  };
84 };
85 
91 struct MatEqVis {
92  template <typename T, typename U>
93  bool operator()(const T &, const U &) {
94  return false;
95  };
96  template <typename T>
97  bool operator()(const T &lhs, const T &rhs) {
98  return lhs.isApprox(rhs);
99  };
100  inline bool operator()(const Empty &, const Empty &) { return true; };
101 };
102 
106 struct VReset {
107  template <class T>
108  void operator()(T &pointer) const {
109  pointer.reset();
110  }
111 };
112 
116 template <template <typename> class STOT>
117 using DataStore = typename mu::variant<
118  std::shared_ptr<const STOT<float>>, std::shared_ptr<const STOT<double>>,
119  std::shared_ptr<const STOT<uint>>, std::shared_ptr<const STOT<uint8_t>>>;
120 
124 template <template <typename> class STOT>
125 using Data = typename mu::variant<Empty, STOT<float>, STOT<double>, STOT<uint>,
126  STOT<uint8_t>>;
127 
128 using DataV = typename mu::variant<std::vector<float>, std::vector<double>,
129  std::vector<uint>, std::vector<uint8_t>>;
130 
135 template <typename T>
136 struct get_core {
137  typedef typename std::remove_pointer<T>::type _tmp;
138  typedef typename std::remove_reference<_tmp>::type __tmp;
139  typedef typename std::remove_cv<__tmp>::type type;
140 };
141 
142 template <typename V, class... VarArgs>
144  const std::unordered_map<std::string, mu::variant<VarArgs...>> &m,
145  std::string const &key, const V &defval) {
146  typename std::unordered_map<std::string,
147  mu::variant<VarArgs...>>::const_iterator it =
148  m.find(key);
149  if (it == m.end()) return defval;
150  return mu::static_variant_cast<V>(it->second);
151 }
152 
153 } // namespace forpy
154 #endif // FORPY_UTIL_STORAGE_H_
V GetWithDefVar(const std::unordered_map< std::string, mu::variant< VarArgs... >> &m, std::string const &key, const V &defval)
Definition: storage.h:143
size_t & innerStride() const
Definition: storage.h:74
bool operator()(const Empty &, const Empty &)
Definition: storage.h:100
std::remove_cv< __tmp >::type type
Definition: storage.h:139
std::remove_reference< _tmp >::type __tmp
Definition: storage.h:138
bool operator==(const Empty &) const
Definition: storage.h:70
bool operator()(const T &, const U &)
Definition: storage.h:93
void serialize(Archive &, const uint &)
Definition: storage.h:69
typename mu::variant< Empty, STOT< float >, STOT< double >, STOT< uint >, STOT< uint8_t > > Data
Storing a variant of the provided data container type.
Definition: storage.h:126
typename mu::variant< std::shared_ptr< const STOT< float > >, std::shared_ptr< const STOT< double > >, std::shared_ptr< const STOT< uint > >, std::shared_ptr< const STOT< uint8_t > >> DataStore
Variant for storing shared_ptrs to the stored data matrix type.
Definition: storage.h:119
float * data() const
Definition: storage.h:71
friend std::ostream & operator<<(std::ostream &stream, const Empty &)
Definition: storage.h:80
typename mu::variant< std::vector< float >, std::vector< double >, std::vector< uint >, std::vector< uint8_t > > DataV
Definition: storage.h:129
void operator()(T &pointer) const
Definition: storage.h:108
std::remove_pointer< T >::type _tmp
Definition: storage.h:137
A struct to represent an empty variant.
Definition: storage.h:67
Call the reset operation on a pointer variant.
Definition: storage.h:106
mu::variant< Ts... > Base
Definition: storage.h:56
Comparison visitor.
Definition: storage.h:91
bool operator()(const T &lhs, const T &rhs)
Definition: storage.h:97
size_t & outerStride() const
Definition: storage.h:77
Get the core datatype with removed pointer, reference and const modifiers.
Definition: storage.h:136
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:113