diff options
Diffstat (limited to 'include/gras')
-rw-r--r-- | include/gras/detail/element.hpp | 27 | ||||
-rw-r--r-- | include/gras/element.hpp | 32 |
2 files changed, 45 insertions, 14 deletions
diff --git a/include/gras/detail/element.hpp b/include/gras/detail/element.hpp index 2b97c8f..f8a8904 100644 --- a/include/gras/detail/element.hpp +++ b/include/gras/detail/element.hpp @@ -3,12 +3,37 @@ #ifndef INCLUDED_GRAS_DETAIL_ELEMENT_HPP #define INCLUDED_GRAS_DETAIL_ELEMENT_HPP +#include <boost/weak_ptr.hpp> + namespace gras { + + //! Weak element overload for the case of shared_ptr container + struct WeakElementSharedPtr : WeakElement + { + WeakElementSharedPtr(boost::weak_ptr<const void> weak_self) + { + _weak_self = weak_self; + } + boost::shared_ptr<const void> lock(void) + { + return _weak_self.lock(); + } + boost::weak_ptr<const void> _weak_self; + }; + template <typename T> inline Element::Element(const boost::shared_ptr<T> &elem) { - *this = elem->shared_to_element(); + //the container is a shared pointer, so save the reference, + //unless the reference was already set to something + if (not elem->weak_self) + { + elem->weak_self.reset(new WeakElementSharedPtr(elem)); + } + + //initialize this new Element from the argument passed + *this = *elem; } } //namespace gras diff --git a/include/gras/element.hpp b/include/gras/element.hpp index cfaa5e4..98286e2 100644 --- a/include/gras/element.hpp +++ b/include/gras/element.hpp @@ -10,7 +10,6 @@ #include <gras/gras.hpp> #include <boost/shared_ptr.hpp> -#include <boost/enable_shared_from_this.hpp> namespace gras { @@ -29,13 +28,13 @@ struct Block; struct WeakElement { //! Lock creates a shared pointer holding a container reference - virtual boost::shared_ptr<void> lock(void) = 0; + virtual boost::shared_ptr<const void> lock(void) = 0; }; /*! * Element is a base class for all topological elements. */ -struct GRAS_API Element : ElementBase, boost::enable_shared_from_this<Element> +struct GRAS_API Element : ElementBase { //! Create an empty element Element(void); @@ -43,8 +42,21 @@ struct GRAS_API Element : ElementBase, boost::enable_shared_from_this<Element> //! Creates a new element given the name Element(const std::string &name); + /*! + * Create an element from a shared pointer to an element. + * Good for that factory function/shared ptr paradigm. + */ + template <typename T> + Element(const boost::shared_ptr<T> &elem); + /*virtual*/ ~Element(void); + //! Convert this object to the element base class + const Element &to_element(void) const; + + //! Convert this object to the element base class + Element &to_element(void); + /*! * Check if another element is the same as this one. * \return true if the rhs is the same as *this. @@ -94,19 +106,13 @@ struct GRAS_API Element : ElementBase, boost::enable_shared_from_this<Element> Block *locate_block(const std::string &path); /******************************************************************* - * Compatibility for dealing with shared ptrs of Elements + * API for dealing with parent containers - for internal use ******************************************************************/ + /*! - * Create an element from a shared pointer to an element. - * Good for that factory function/shared ptr paradigm. + * Allows internals to get a reference to the container holding an element. + * This container could be a shared_ptr or perhaps a Python object. */ - template <typename T> - Element(const boost::shared_ptr<T> &elem); - - //! Convert a shared ptr of a derived class to an Element - Element &shared_to_element(void); - - //! for internal use only boost::shared_ptr<WeakElement> weak_self; }; |