gtsam  4.0.0
gtsam
DerivedValue.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 DerivedValue.h
14  * @date Jan 26, 2012
15  * @author Duy Nguyen Ta
16  */
17 
18 #pragma once
19 
20 #include <gtsam/base/Value.h>
21 #include <boost/make_shared.hpp>
22 
24 // The following includes windows.h in some MSVC versions, so we undef min, max, and ERROR
25 #include <boost/pool/singleton_pool.hpp>
26 
27 #ifdef min
28 #undef min
29 #endif
30 
31 #ifdef max
32 #undef max
33 #endif
34 
35 #ifdef ERROR
36 #undef ERROR
37 #endif
38 
40 
41 namespace gtsam {
42 
43 template<class DERIVED>
44 class DerivedValue : public Value {
45 
46 protected:
47  DerivedValue() {}
48 
49 public:
50 
51  virtual ~DerivedValue() {}
52 
58  virtual Value* clone_() const {
59  void *place = boost::singleton_pool<PoolTag, sizeof(DERIVED)>::malloc();
60  DERIVED* ptr = new(place) DERIVED(static_cast<const DERIVED&>(*this));
61  return ptr;
62  }
63 
67  virtual void deallocate_() const {
68  this->~DerivedValue(); // Virtual destructor cleans up the derived object
69  boost::singleton_pool<PoolTag, sizeof(DERIVED)>::free((void*)this); // Release memory from pool
70  }
71 
75  virtual boost::shared_ptr<Value> clone() const {
76  return boost::make_shared<DERIVED>(static_cast<const DERIVED&>(*this));
77  }
78 
80  virtual bool equals_(const Value& p, double tol = 1e-9) const {
81  // Cast the base class Value pointer to a derived class pointer
82  const DERIVED& derivedValue2 = dynamic_cast<const DERIVED&>(p);
83 
84  // Return the result of calling equals on the derived class
85  return (static_cast<const DERIVED*>(this))->equals(derivedValue2, tol);
86  }
87 
89  virtual Value* retract_(const Vector& delta) const {
90  // Call retract on the derived class
91  const DERIVED retractResult = (static_cast<const DERIVED*>(this))->retract(delta);
92 
93  // Create a Value pointer copy of the result
94  void* resultAsValuePlace = boost::singleton_pool<PoolTag, sizeof(DERIVED)>::malloc();
95  Value* resultAsValue = new(resultAsValuePlace) DERIVED(retractResult);
96 
97  // Return the pointer to the Value base class
98  return resultAsValue;
99  }
100 
102  virtual Vector localCoordinates_(const Value& value2) const {
103  // Cast the base class Value pointer to a derived class pointer
104  const DERIVED& derivedValue2 = dynamic_cast<const DERIVED&>(value2);
105 
106  // Return the result of calling localCoordinates on the derived class
107  return (static_cast<const DERIVED*>(this))->localCoordinates(derivedValue2);
108  }
109 
111  virtual Value& operator=(const Value& rhs) {
112  // Cast the base class Value pointer to a derived class pointer
113  const DERIVED& derivedRhs = dynamic_cast<const DERIVED&>(rhs);
114 
115  // Do the assignment and return the result
116  return (static_cast<DERIVED*>(this))->operator=(derivedRhs);
117  }
118 
120  operator const DERIVED& () const {
121  return static_cast<const DERIVED&>(*this);
122  }
123 
125  operator DERIVED& () {
126  return static_cast<DERIVED&>(*this);
127  }
128 
129 protected:
133  // Nothing to do, do not call base class assignment operator
134  return *this;
135  }
136 
137 private:
139  struct PoolTag { };
140 
141 };
142 
143 } /* namespace gtsam */
DerivedValue< DERIVED > & operator=(const DerivedValue< DERIVED > &)
Assignment operator, protected because only the Value or DERIVED assignment operators should be used...
Definition: DerivedValue.h:132
virtual bool equals_(const Value &p, double tol=1e-9) const
equals implementing generic Value interface
Definition: DerivedValue.h:80
virtual Value * retract_(const Vector &delta) const
Generic Value interface version of retract.
Definition: DerivedValue.h:89
virtual boost::shared_ptr< Value > clone() const
Clone this value (normal clone on the heap, delete with &#39;delete&#39; operator)
Definition: DerivedValue.h:75
Template to create a binary predicate.
Definition: Testable.h:110
virtual void deallocate_() const
Destroy and deallocate this object, only if it was originally allocated using clone_().
Definition: DerivedValue.h:67
virtual Value & operator=(const Value &rhs)
Assignment operator.
Definition: DerivedValue.h:111
virtual Vector localCoordinates_(const Value &value2) const
Generic Value interface version of localCoordinates.
Definition: DerivedValue.h:102
virtual Value * clone_() const
Create a duplicate object returned as a pointer to the generic Value interface.
Definition: DerivedValue.h:58
Definition: DerivedValue.h:44
The interface class for any variable that can be optimized or used in a factor.
This is the interface class for any value that may be used as a variable assignment in a factor graph...
Definition: Value.h:83
Global functions in a separate testing namespace.
Definition: chartTesting.h:28