diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index a4e60e7..9c50c6b 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -56,7 +56,7 @@ namespace chaiscript if (rhs.m_attrs) { - m_attrs = std::unique_ptr>(new std::map(*rhs.m_attrs)); + m_attrs = std::unique_ptr>>(new std::map>(*rhs.m_attrs)); } return *this; @@ -74,7 +74,7 @@ namespace chaiscript chaiscript::detail::Any m_obj; void *m_data_ptr; const void *m_const_data_ptr; - std::unique_ptr> m_attrs; + std::unique_ptr>> m_attrs; bool m_is_ref; bool m_return_value; }; @@ -277,17 +277,24 @@ namespace chaiscript { if (!m_data->m_attrs) { - m_data->m_attrs = std::unique_ptr>(new std::map()); + m_data->m_attrs = std::unique_ptr>>(new std::map>()); } - return (*m_data->m_attrs)[t_name]; + auto &attr = (*m_data->m_attrs)[t_name]; + if (attr) { + return Boxed_Value(attr, Internal_Construction()); + } else { + Boxed_Value bv; //default construct a new one + attr = bv.m_data; + return bv; + } } Boxed_Value ©_attrs(const Boxed_Value &t_obj) { if (t_obj.m_data->m_attrs) { - m_data->m_attrs = std::unique_ptr>(new std::map(*t_obj.m_data->m_attrs)); + m_data->m_attrs = std::unique_ptr>>(new std::map>(*t_obj.m_data->m_attrs)); } return *this; } @@ -300,6 +307,13 @@ namespace chaiscript } private: + // necessary to avoid hitting the templated && constructor of Boxed_Value + struct Internal_Construction{}; + + Boxed_Value(const std::shared_ptr &t_data, Internal_Construction) + : m_data(t_data) { + } + std::shared_ptr m_data; };