diff --git a/gtsam/base/DerivedValue.h b/gtsam/base/DerivedValue.h index 6c10a29f5..4f6dae9c7 100644 --- a/gtsam/base/DerivedValue.h +++ b/gtsam/base/DerivedValue.h @@ -16,7 +16,7 @@ */ #pragma once - +#include #include #include @@ -37,7 +37,8 @@ public: /** * Create a duplicate object returned as a pointer to the generic Value interface. - * For the sake of performance, this function use singleton pool allocator instead of the normal heap allocator + * For the sake of performance, this function use singleton pool allocator instead of the normal heap allocator. + * The result must be deleted with Value::deallocate_, not with the 'delete' operator. */ virtual Value* clone_() const { void *place = boost::singleton_pool::malloc(); @@ -53,6 +54,13 @@ public: boost::singleton_pool::free((void*)this); } + /** + * Clone this value (normal clone on the heap, delete with 'delete' operator) + */ + virtual boost::shared_ptr clone() const { + return boost::make_shared(static_cast(*this)); + } + /// equals implementing generic Value interface virtual bool equals_(const Value& p, double tol = 1e-9) const { // Cast the base class Value pointer to a derived class pointer diff --git a/gtsam/base/Value.h b/gtsam/base/Value.h index 394a11e72..8b19a6532 100644 --- a/gtsam/base/Value.h +++ b/gtsam/base/Value.h @@ -101,12 +101,15 @@ namespace gtsam { class Value { public: - /** Allocate and construct a clone of this value */ + /** Clone this value in a special memory pool, must be deleted with Value::deallocate_, *not* with the 'delete' operator. */ virtual Value* clone_() const = 0; /** Deallocate a raw pointer of this value */ virtual void deallocate_() const = 0; + /** Clone this value (normal clone on the heap, delete with 'delete' operator) */ + virtual boost::shared_ptr clone() const = 0; + /** Compare this Value with another for equality. */ virtual bool equals_(const Value& other, double tol = 1e-9) const = 0;