Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r68549 - in trunk: boost/spirit/home/support/utree boost/spirit/home/support/utree/detail libs/spirit/test/support
From: blelbach_at_[hidden]
Date: 2011-01-29 13:58:28


Author: wash
Date: 2011-01-29 13:58:27 EST (Sat, 29 Jan 2011)
New Revision: 68549
URL: http://svn.boost.org/trac/boost/changeset/68549

Log:
Added support for storing function references in utree.

Text files modified:
   trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp | 47 +++++++++++++++++++++++++++++++++++++--
   trunk/boost/spirit/home/support/utree/utree.hpp | 17 ++++++++++++++
   trunk/libs/spirit/test/support/utree.cpp | 19 ++++++++++++++++
   3 files changed, 80 insertions(+), 3 deletions(-)

Modified: trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp
==============================================================================
--- trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp (original)
+++ trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp 2011-01-29 13:58:27 EST (Sat, 29 Jan 2011)
@@ -617,7 +617,31 @@
     function_base*
     stored_function<F>::clone() const
     {
- return new stored_function<F>(*this);
+ return new stored_function<F>(f);
+ }
+
+ template <typename F>
+ referenced_function<F>::referenced_function(F& f)
+ : f(f)
+ {
+ }
+
+ template <typename F>
+ referenced_function<F>::~referenced_function()
+ {
+ };
+
+ template <typename F>
+ utree referenced_function<F>::operator()(scope const& env) const
+ {
+ return f(env);
+ }
+
+ template <typename F>
+ function_base*
+ referenced_function<F>::clone() const
+ {
+ return new referenced_function<F>(f);
     }
 
     inline utree::utree(utree::invalid_type)
@@ -719,6 +743,14 @@
         pf = new stored_function<F>(pf_);
         set_type(type::function_type);
     }
+
+ template <typename F>
+ inline utree::utree(referenced_function<F> const& pf_)
+ {
+ s.initialize();
+ pf = new referenced_function<F>(pf_);
+ set_type(type::function_type);
+ }
 
     template <typename Iter>
     inline utree::utree(boost::iterator_range<Iter> r)
@@ -864,10 +896,19 @@
     }
 
     template <typename F>
- utree& utree::operator=(stored_function<F> const& pf)
+ utree& utree::operator=(stored_function<F> const& pf_)
+ {
+ free();
+ pf = new stored_function<F>(pf_);
+ set_type(type::function_type);
+ return *this;
+ }
+
+ template <typename F>
+ utree& utree::operator=(referenced_function<F> const& pf_)
     {
         free();
- pf = new stored_function<F>(pf);
+ pf = new referenced_function<F>(pf_);
         set_type(type::function_type);
         return *this;
     }

Modified: trunk/boost/spirit/home/support/utree/utree.hpp
==============================================================================
--- trunk/boost/spirit/home/support/utree/utree.hpp (original)
+++ trunk/boost/spirit/home/support/utree/utree.hpp 2011-01-29 13:58:27 EST (Sat, 29 Jan 2011)
@@ -192,6 +192,16 @@
         virtual utree operator()(scope const& env) const;
         virtual function_base* clone() const;
     };
+
+ template <typename F>
+ struct referenced_function : function_base
+ {
+ F& f;
+ referenced_function(F& f);
+ virtual ~referenced_function();
+ virtual utree operator()(scope const& env) const;
+ virtual function_base* clone() const;
+ };
     //]
 
     ///////////////////////////////////////////////////////////////////////////
@@ -361,6 +371,13 @@
         utree(stored_function<F> const&);
         template <class F>
         reference operator=(stored_function<F> const&);
+
+ // This initializes a `function_type` node, storing by reference
+ // instead of copying the function object.
+ template <class F>
+ utree(referenced_function<F> const&);
+ template <class F>
+ reference operator=(referenced_function<F> const&);
 
         // This initializes either a `string_type`, a `symbol_type`, or a
         // `binary_type` node (depending on the template parameter `type_`),

Modified: trunk/libs/spirit/test/support/utree.cpp
==============================================================================
--- trunk/libs/spirit/test/support/utree.cpp (original)
+++ trunk/libs/spirit/test/support/utree.cpp 2011-01-29 13:58:27 EST (Sat, 29 Jan 2011)
@@ -10,6 +10,7 @@
 #include <boost/config/warning_disable.hpp>
 #include <boost/detail/lightweight_test.hpp>
 
+#include <boost/functional/hash.hpp>
 #include <boost/spirit/include/support_utree.hpp>
 
 #include <iostream>
@@ -36,6 +37,14 @@
     }
 };
 
+struct this_
+{
+ boost::spirit::utree operator()(boost::spirit::scope) const
+ {
+ return boost::spirit::utree(static_cast<int>(boost::hash_value(this)));
+ }
+};
+
 int main()
 {
     using boost::spirit::utree;
@@ -358,6 +367,16 @@
         utree f = stored_function<one_two_three>();
         f.eval(scope());
     }
+
+ {
+ // test referenced functions
+ using boost::spirit::referenced_function;
+ using boost::spirit::scope;
+
+ one_two_three f;
+ utree ff = referenced_function<one_two_three>(f);
+ BOOST_TEST_EQ(ff.eval(scope()), f(scope()));
+ }
 
     {
         // shallow ranges


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk