diff --git a/dispatchkit/boxed_value.hpp b/dispatchkit/boxed_value.hpp index 84f08c7..5b6d882 100644 --- a/dispatchkit/boxed_value.hpp +++ b/dispatchkit/boxed_value.hpp @@ -4,8 +4,9 @@ #include "type_info.hpp" #include #include - +#include #include +#include class Boxed_Value { @@ -14,8 +15,10 @@ class Boxed_Value { Data(const Type_Info &ti, const boost::any &to, - bool tr) - : m_type_info(ti), m_obj(to), m_is_ref(tr) + bool tr, + const boost::function &t_unique = &Boxed_Value::Data::get_false) + : m_type_info(ti), m_obj(to), + m_is_ref(tr) { } @@ -28,6 +31,11 @@ class Boxed_Value return *this; } + static bool get_false() + { + return false; + } + Type_Info m_type_info; boost::any m_obj; bool m_is_ref; @@ -46,16 +54,16 @@ class Boxed_Value false) ) { - void *ptr = boost::any_cast >(m_data->m_obj).get(); + boost::shared_ptr *ptr = boost::any_cast >(&m_data->m_obj); std::map >::iterator itr - = m_ptrs.find(ptr); + = m_ptrs.find(ptr->get()); if (itr != m_ptrs.end()) { m_data = (itr->second); } else { - m_ptrs[ptr] = m_data; + m_ptrs[ptr->get()] = m_data; } } @@ -74,11 +82,9 @@ class Boxed_Value if (itr != m_ptrs.end()) { - std::cout << "Reference wrapper ptr found, using it" << std::endl; +// std::cout << "Reference wrapper ptr found, using it" << std::endl; m_data = (itr->second); - } else { - m_ptrs[ptr] = m_data; - } + } } template @@ -89,7 +95,9 @@ class Boxed_Value false) ) { - m_ptrs[boost::any_cast >(m_data->m_obj).get()] + boost::shared_ptr *ptr = boost::any_cast >(&m_data->m_obj); + + m_ptrs[ptr->get()] = m_data; } @@ -116,6 +124,11 @@ class Boxed_Value { } + ~Boxed_Value() + { + cleanup_ptrs(m_ptrs); + } + Boxed_Value assign(const Boxed_Value &rhs) { (*m_data) = (*rhs.m_data); @@ -151,6 +164,26 @@ class Boxed_Value private: boost::shared_ptr m_data; static std::map > m_ptrs; + + void cleanup_ptrs(std::map > &m_ptrs) + { + std::map >::iterator itr = m_ptrs.begin(); + + while (itr != m_ptrs.end()) + { + if (itr->second.unique()) + { + std::map >::iterator todel = itr; +// std::cout << "Releasing unique ptr " << std::endl; + ++itr; + m_ptrs.erase(todel); + } else { + ++itr; + } + } + +// std::cout << "References held: " << m_ptrs.size() << std::endl; + } }; std::map > Boxed_Value::m_ptrs; diff --git a/samples/lambda.wes b/samples/lambda.wes index dcd61f0..81aafe6 100644 --- a/samples/lambda.wes +++ b/samples/lambda.wes @@ -1,2 +1,5 @@ var add2 = function(x, y) { x + y } +var add1 = function(x, f) { f(3,x) } print(add2(3, 4)) + +print(add1(2, add2))