gtsam
Loading...
Searching...
No Matches
GenericValue.h
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 <iostream>
27#include <typeinfo> // operator typeid
28
29// GenericValue's RTTI must remain visible so the C++ ABI can identify a
30// specialization consistently when a Value crosses a shared-library boundary.
31#if defined(_WIN32)
32// Exporting this header-only template triggers linker errors on MSVC.
33// Please refer to https://github.com/borglab/gtsam/blob/develop/Using-GTSAM-EXPORT.md
34#define GENERICVALUE_VISIBILITY
35#else
36#define GENERICVALUE_VISIBILITY GTSAM_EXPORT
37#endif
38
39namespace gtsam {
40
47template<class T>
48class GENERICVALUE_VISIBILITY GenericValue: public Value {
49
50public:
51
52 typedef T type;
53
54protected:
55
57
58public:
59 // Only needed for serialization.
60 GenericValue(){}
61
63 GenericValue(const T& value) : Value(),
64 value_(value) {}
65
66 GenericValue(const GenericValue& other) = default;
67
69 const T& value() const {
70 return value_;
71 }
72
74 T& value() {
75 return value_;
76 }
77
79 ~GenericValue() override {
80 }
81
83 bool equals_(const Value& p, double tol = 1e-9) const override {
84 // Cast the base class Value pointer to a templated generic class pointer
85 const GenericValue& genericValue2 = static_cast<const GenericValue&>(p);
86 // Return the result of using the equals traits for the derived class
87 return traits<T>::Equals(this->value_, genericValue2.value_, tol);
88 }
89
91 bool equals(const GenericValue &other, double tol = 1e-9) const {
92 return traits<T>::Equals(this->value(), other.value(), tol);
93 }
94
96 void print(const std::string& str) const override {
97 std::cout << "(" << demangle(typeid(T).name()) << ")\n";
99 }
100
104 Value* clone_() const override {
105 GenericValue* ptr = new GenericValue(*this); // calls copy constructor to fill in
106 return ptr;
107 }
108
112 void deallocate_() const override {
113 delete this;
114 }
115
119 std::shared_ptr<Value> clone() const override {
120 return std::allocate_shared<GenericValue>(Eigen::aligned_allocator<GenericValue>(), *this);
121 }
122
124 Value* retract_(const Vector& delta) const override {
125 // Call retract on the derived class using the retract trait function
126 const T retractResult = traits<T>::Retract(GenericValue<T>::value(), delta);
127
128 Value* resultAsValue = new GenericValue(retractResult);
129
130 // Return the pointer to the Value base class
131 return resultAsValue;
132 }
133
135 Vector localCoordinates_(const Value& value2) const override {
136 // Cast the base class Value pointer to a templated generic class pointer
137 const GenericValue<T>& genericValue2 =
138 static_cast<const GenericValue<T>&>(value2);
139
140 // Return the result of calling localCoordinates trait on the derived class
141 return traits<T>::Local(GenericValue<T>::value(), genericValue2.value());
142 }
143
145 GenericValue retract(const Vector& delta) const {
146 return GenericValue(traits<T>::Retract(GenericValue<T>::value(), delta));
147 }
148
150 Vector localCoordinates(const GenericValue& value2) const {
151 return localCoordinates_(value2);
152 }
153
155 size_t dim() const override {
157 }
158
160 Value& operator=(const Value& rhs) override {
161 // Cast the base class Value pointer to a derived class pointer
162 const GenericValue& derivedRhs = static_cast<const GenericValue&>(rhs);
163
164 // Do the assignment and return the result
165 *this = GenericValue(derivedRhs); // calls copy constructor
166 return *this;
167 }
168
169 protected:
170
173 GenericValue<T>& operator=(const GenericValue<T>& rhs) {
174 Value::operator=(static_cast<Value const&>(rhs));
175 value_ = rhs.value_;
176 return *this;
177 }
178
179 private:
180
181#if GTSAM_ENABLE_BOOST_SERIALIZATION
183 friend class boost::serialization::access;
184 template<class ARCHIVE>
185 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
186 ar & boost::serialization::make_nvp("GenericValue",
187 boost::serialization::base_object<Value>(*this));
188 ar & boost::serialization::make_nvp("value", value_);
189 }
190#endif
191};
192
194#define GTSAM_VALUE_EXPORT(Type) BOOST_CLASS_EXPORT(gtsam::GenericValue<Type>)
195
196// traits
197template <typename ValueType>
198struct traits<GenericValue<ValueType> >
199 : public Testable<GenericValue<ValueType> > {};
200
201// define Value::cast here since now GenericValue has been declared
202template<typename ValueType>
203const ValueType& Value::cast() const {
204 return dynamic_cast<const GenericValue<ValueType>&>(*this).value();
205}
206
209template<class T>
211 return GenericValue<T>(v);
212}
213
214
215} /* namespace gtsam */
Typedefs for easier changing of types.
The base class for any variable that can be optimized or used in a factor.
Base class and basic functions for Manifold types.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
std::string demangle(const char *name)
Pretty print Value type name.
Definition types.cpp:37
GenericValue< T > genericValue(const T &v)
Functional constructor of GenericValue<T> so T can be automatically deduced.
Definition GenericValue.h:210
Wraps any type T so it can play as a Value.
Definition GenericValue.h:48
Value * clone_() const override
Create a duplicate object returned as a pointer to the generic Value interface.
Definition GenericValue.h:104
T & value()
Return the value.
Definition GenericValue.h:74
Value * retract_(const Vector &delta) const override
Generic Value interface version of retract.
Definition GenericValue.h:124
Value & operator=(const Value &rhs) override
Assignment operator.
Definition GenericValue.h:160
void print(const std::string &str) const override
Virtual print function, uses traits.
Definition GenericValue.h:96
bool equals(const GenericValue &other, double tol=1e-9) const
non virtual equals function, uses traits
Definition GenericValue.h:91
size_t dim() const override
Return run-time dimensionality.
Definition GenericValue.h:155
GenericValue(const T &value)
Construct from value.
Definition GenericValue.h:63
void deallocate_() const override
Destroy and deallocate this object, only if it was originally allocated using clone_().
Definition GenericValue.h:112
T value_
The wrapped value.
Definition GenericValue.h:56
GenericValue< T > & operator=(const GenericValue< T > &rhs)
Assignment operator, protected because only the Value or DERIVED assignment operators should be used.
Definition GenericValue.h:173
Vector localCoordinates(const GenericValue &value2) const
Non-virtual version of localCoordinates.
Definition GenericValue.h:150
bool equals_(const Value &p, double tol=1e-9) const override
equals implementing generic Value interface
Definition GenericValue.h:83
std::shared_ptr< Value > clone() const override
Clone this value (normal clone on the heap, delete with 'delete' operator).
Definition GenericValue.h:119
const T & value() const
Return a constant value.
Definition GenericValue.h:69
~GenericValue() override
Destructor.
Definition GenericValue.h:79
GenericValue retract(const Vector &delta) const
Non-virtual version of retract.
Definition GenericValue.h:145
Vector localCoordinates_(const Value &value2) const override
Generic Value interface version of localCoordinates.
Definition GenericValue.h:135
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
const ValueType & cast() const
Cast to known ValueType.
Definition GenericValue.h:203
virtual Value & operator=(const Value &)
Assignment operator.
Definition Value.h:84