GenericValue.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
12 /*
13  * @file GenericValue.h
14  * @brief Wraps any type T so it can play as a Value
15  * @date October, 2014
16  * @author Michael Bosse, Abel Gawel, Renaud Dube
17  * based on DerivedValue.h by Duy Nguyen Ta
18  */
19 
20 #pragma once
21 
22 #include <gtsam/base/Manifold.h>
23 #include <gtsam/base/types.h>
24 #include <gtsam/base/Value.h>
25 
26 #include <boost/make_shared.hpp>
27 #include <boost/pool/pool_alloc.hpp>
28 
29 #include <cmath>
30 #include <iostream>
31 #include <typeinfo> // operator typeid
32 
33 #ifdef _WIN32
34 #define GENERICVALUE_VISIBILITY
35 #else
36 // This will trigger a LNKxxxx on MSVC, so disable for MSVC build
37 // Please refer to https://github.com/borglab/gtsam/blob/develop/Using-GTSAM-EXPORT.md
38 #define GENERICVALUE_VISIBILITY GTSAM_EXPORT
39 #endif
40 
41 namespace gtsam {
42 
46 template<class T>
47 class GenericValue: public Value {
48 
49 public:
50 
51  typedef T type;
52 
53 protected:
54 
56 
57 public:
58  // Only needed for serialization.
60 
62  GenericValue(const T& value) :
63  value_(value) {
64  }
65 
67  const T& value() const {
68  return value_;
69  }
70 
72  T& value() {
73  return value_;
74  }
75 
77  ~GenericValue() override {
78  }
79 
81  bool equals_(const Value& p, double tol = 1e-9) const override {
82  // Cast the base class Value pointer to a templated generic class pointer
83  const GenericValue& genericValue2 = static_cast<const GenericValue&>(p);
84  // Return the result of using the equals traits for the derived class
85  return traits<T>::Equals(this->value_, genericValue2.value_, tol);
86  }
87 
89  bool equals(const GenericValue &other, double tol = 1e-9) const {
90  return traits<T>::Equals(this->value(), other.value(), tol);
91  }
92 
94  void print(const std::string& str) const override {
95  std::cout << "(" << demangle(typeid(T).name()) << ")\n";
96  traits<T>::Print(value_, str);
97  }
98 
102  Value* clone_() const override {
103  GenericValue* ptr = new GenericValue(*this); // calls copy constructor to fill in
104  return ptr;
105  }
106 
110  void deallocate_() const override {
111  delete this;
112  }
113 
117  boost::shared_ptr<Value> clone() const override {
118  return boost::allocate_shared<GenericValue>(Eigen::aligned_allocator<GenericValue>(), *this);
119  }
120 
122  Value* retract_(const Vector& delta) const override {
123  // Call retract on the derived class using the retract trait function
124  const T retractResult = traits<T>::Retract(GenericValue<T>::value(), delta);
125 
126  Value* resultAsValue = new GenericValue(retractResult);
127 
128  // Return the pointer to the Value base class
129  return resultAsValue;
130  }
131 
133  Vector localCoordinates_(const Value& value2) const override {
134  // Cast the base class Value pointer to a templated generic class pointer
135  const GenericValue<T>& genericValue2 =
136  static_cast<const GenericValue<T>&>(value2);
137 
138  // Return the result of calling localCoordinates trait on the derived class
139  return traits<T>::Local(GenericValue<T>::value(), genericValue2.value());
140  }
141 
145  }
146 
148  Vector localCoordinates(const GenericValue& value2) const {
149  return localCoordinates_(value2);
150  }
151 
153  size_t dim() const override {
154  return traits<T>::GetDimension(value_);
155  }
156 
158  Value& operator=(const Value& rhs) override {
159  // Cast the base class Value pointer to a derived class pointer
160  const GenericValue& derivedRhs = static_cast<const GenericValue&>(rhs);
161 
162  // Do the assignment and return the result
163  *this = GenericValue(derivedRhs); // calls copy constructor
164  return *this;
165  }
166 
167  protected:
168 
172  Value::operator=(static_cast<Value const&>(rhs));
173  value_ = rhs.value_;
174  return *this;
175  }
176 
177  private:
178 
181  template<class ARCHIVE>
182  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
183  ar & boost::serialization::make_nvp("GenericValue",
184  boost::serialization::base_object<Value>(*this));
185  ar & boost::serialization::make_nvp("value", value_);
186  }
187 
188 
189  // Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
190  enum { NeedsToAlign = (sizeof(T) % 16) == 0 };
191 public:
193 };
194 
196 #define GTSAM_VALUE_EXPORT(Type) BOOST_CLASS_EXPORT(gtsam::GenericValue<Type>)
197 
198 // traits
199 template <typename ValueType>
200 struct traits<GenericValue<ValueType> >
201  : public Testable<GenericValue<ValueType> > {};
202 
203 // define Value::cast here since now GenericValue has been declared
204 template<typename ValueType>
205 const ValueType& Value::cast() const {
206  return dynamic_cast<const GenericValue<ValueType>&>(*this).value();
207 }
208 
211 template<class T>
213  return GenericValue<T>(v);
214 }
215 
216 
217 } /* namespace gtsam */
Typedefs for easier changing of types.
ArrayXcf v
Definition: Cwise_arg.cpp:1
T value_
The wrapped value.
Definition: GenericValue.h:55
void print(const std::string &str) const override
Virtual print function, uses traits.
Definition: GenericValue.h:94
const T & value() const
Return a constant value.
Definition: GenericValue.h:67
~GenericValue() override
Destructor.
Definition: GenericValue.h:77
Value * clone_() const override
Definition: GenericValue.h:102
GenericValue(const T &value)
Construct from value.
Definition: GenericValue.h:62
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
Definition: types.h:278
friend class boost::serialization::access
Definition: GenericValue.h:180
void deallocate_() const override
Definition: GenericValue.h:110
Eigen::VectorXd Vector
Definition: Vector.h:38
bool equals(const GenericValue &other, double tol=1e-9) const
non virtual equals function, uses traits
Definition: GenericValue.h:89
STL compatible allocator to use with types requiring a non standrad alignment.
Definition: Memory.h:721
Definition: pytypes.h:928
float * ptr
Value & operator=(const Value &rhs) override
Assignment operator.
Definition: GenericValue.h:158
Base class and basic functions for Manifold types.
std::string demangle(const char *name)
Pretty print Value type name.
Definition: types.cpp:37
Value * retract_(const Vector &delta) const override
Generic Value interface version of retract.
Definition: GenericValue.h:122
Eigen::Triplet< double > T
virtual Value & operator=(const Value &)
Definition: Value.h:78
Array< double, 1, 3 > e(1./3., 0.5, 2.)
traits
Definition: chartTesting.h:28
GenericValue< T > genericValue(const T &v)
Definition: GenericValue.h:212
GenericValue retract(const Vector &delta) const
Non-virtual version of retract.
Definition: GenericValue.h:143
bool equals_(const Value &p, double tol=1e-9) const override
equals implementing generic Value interface
Definition: GenericValue.h:81
float * p
Vector localCoordinates_(const Value &value2) const override
Generic Value interface version of localCoordinates.
Definition: GenericValue.h:133
The base class for any variable that can be optimized or used in a factor.
const G double tol
Definition: Group.h:83
Vector localCoordinates(const GenericValue &value2) const
Non-virtual version of localCoordinates.
Definition: GenericValue.h:148
boost::shared_ptr< Value > clone() const override
Definition: GenericValue.h:117
void serialize(ARCHIVE &ar, const unsigned int)
Definition: GenericValue.h:182
const ValueType & cast() const
Definition: GenericValue.h:205
T & value()
Return the value.
Definition: GenericValue.h:72
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:59
size_t dim() const override
Return run-time dimensionality.
Definition: GenericValue.h:153
GenericValue< T > & operator=(const GenericValue< T > &rhs)
Definition: GenericValue.h:171


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:07