Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72067 - in trunk: boost/spirit/home/support/utree boost/spirit/home/support/utree/detail libs/spirit/test/support
From: blelbach_at_[hidden]
Date: 2011-05-21 19:58:20


Author: wash
Date: 2011-05-21 19:58:19 EDT (Sat, 21 May 2011)
New Revision: 72067
URL: http://svn.boost.org/trac/boost/changeset/72067

Log:
Change utree functions to take a utree as a parameter, instead of a scope.
Remove scope from Spirit, as it is application specific. Change utree's function
API to take function_base references and pointers by ctor/assignment operator,
instead of just stored_function<> instances, allowing users to derive classes
from function_base and use them with utree. Add an operator() to utree that
aliases utree.eval(), so that utree can be used as a function object. Allow both
a utree& and a utree const& to be passed as a parameter to a utree function object.

Text files modified:
   trunk/boost/spirit/home/support/utree/detail/utree_detail1.hpp | 2
   trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp | 66 ++++++++++++++++++++++++---------
   trunk/boost/spirit/home/support/utree/operators.hpp | 15 +++++--
   trunk/boost/spirit/home/support/utree/utree.hpp | 77 ++++++++++-----------------------------
   trunk/libs/spirit/test/support/utree.cpp | 10 ++---
   5 files changed, 83 insertions(+), 87 deletions(-)

Modified: trunk/boost/spirit/home/support/utree/detail/utree_detail1.hpp
==============================================================================
--- trunk/boost/spirit/home/support/utree/detail/utree_detail1.hpp (original)
+++ trunk/boost/spirit/home/support/utree/detail/utree_detail1.hpp 2011-05-21 19:58:19 EDT (Sat, 21 May 2011)
@@ -1,6 +1,7 @@
 /*=============================================================================
     Copyright (c) 2001-2011 Joel de Guzman
     Copyright (c) 2001-2011 Hartmut Kaiser
+ Copyright (c) 2011 Bryce Lelbach
 
     Distributed under the Boost Software License, Version 1.0. (See accompanying
     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -16,7 +17,6 @@
     struct visit_impl;
 
     struct index_impl;
- struct assign_impl;
 
     ///////////////////////////////////////////////////////////////////////////
     // Our POD double linked list. Straightforward implementation.

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-05-21 19:58:19 EDT (Sat, 21 May 2011)
@@ -1,6 +1,7 @@
 /*=============================================================================
     Copyright (c) 2001-2011 Joel de Guzman
     Copyright (c) 2001-2011 Hartmut Kaiser
+ Copyright (c) 2011 Bryce Lelbach
 
     Distributed under the Boost Software License, Version 1.0. (See accompanying
     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -642,7 +643,13 @@
     }
 
     template <typename F>
- utree stored_function<F>::operator()(scope const& env) const
+ utree stored_function<F>::operator()(utree const& env) const
+ {
+ return f(env);
+ }
+
+ template <typename F>
+ utree stored_function<F>::operator()(utree& env)
     {
         return f(env);
     }
@@ -666,7 +673,13 @@
     }
 
     template <typename F>
- utree referenced_function<F>::operator()(scope const& env) const
+ utree referenced_function<F>::operator()(utree const& env) const
+ {
+ return f(env);
+ }
+
+ template <typename F>
+ utree referenced_function<F>::operator()(utree& env)
     {
         return f(env);
     }
@@ -770,22 +783,20 @@
         set_type(type::any_type);
     }
 
- template <typename F>
- inline utree::utree(stored_function<F> const& pf_)
+ inline utree::utree(function_base const& pf_)
     {
         s.initialize();
- pf = new stored_function<F>(pf_);
+ pf = pf_.clone();
         set_type(type::function_type);
     }
     
- template <typename F>
- inline utree::utree(referenced_function<F> const& pf_)
+ inline utree::utree(function_base* pf_)
     {
         s.initialize();
- pf = new referenced_function<F>(pf_);
+ pf = pf_;
         set_type(type::function_type);
     }
-
+
     template <typename Iter>
     inline utree::utree(boost::iterator_range<Iter> r)
     {
@@ -928,25 +939,23 @@
         set_type(type::any_type);
         return *this;
     }
-
- template <typename F>
- utree& utree::operator=(stored_function<F> const& pf_)
+
+ utree& utree::operator=(function_base const& pf_)
     {
         free();
- pf = new stored_function<F>(pf_);
+ pf = pf_.clone();
         set_type(type::function_type);
         return *this;
     }
-
- template <typename F>
- utree& utree::operator=(referenced_function<F> const& pf_)
+
+ utree& utree::operator=(function_base* pf_)
     {
         free();
- pf = new referenced_function<F>(pf_);
+ pf = pf_;
         set_type(type::function_type);
         return *this;
     }
-
+
     template <typename Iter>
     inline utree& utree::operator=(boost::iterator_range<Iter> r)
     {
@@ -1566,7 +1575,16 @@
         s.tag(tag);
     }
 
- inline utree utree::eval(scope const& env) const
+ inline utree utree::eval(utree const& env) const
+ {
+ if (get_type() != type::function_type)
+ BOOST_THROW_EXCEPTION(
+ bad_type_exception(
+ "eval() called on non-function utree type", get_type()));
+ return (*pf)(env);
+ }
+
+ inline utree utree::eval(utree& env) const
     {
         if (get_type() != type::function_type)
             BOOST_THROW_EXCEPTION(
@@ -1574,6 +1592,16 @@
                     "eval() called on non-function utree type", get_type()));
         return (*pf)(env);
     }
+
+ inline utree utree::operator() (utree const& env) const
+ {
+ return eval(env);
+ }
+
+ inline utree utree::operator() (utree& env) const
+ {
+ return eval(env);
+ }
 }}
 
 #if defined(BOOST_MSVC)

Modified: trunk/boost/spirit/home/support/utree/operators.hpp
==============================================================================
--- trunk/boost/spirit/home/support/utree/operators.hpp (original)
+++ trunk/boost/spirit/home/support/utree/operators.hpp 2011-05-21 19:58:19 EDT (Sat, 21 May 2011)
@@ -1,6 +1,7 @@
 /*=============================================================================
     Copyright (c) 2001-2011 Joel de Guzman
     Copyright (c) 2001-2011 Hartmut Kaiser
+ Copyright (c) 2011 Bryce Lelbach
 
     Distributed under the Boost Software License, Version 1.0. (See accompanying
     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -15,7 +16,9 @@
 #endif
 
 #include <exception>
-#include <ios>
+#if !defined(BOOST_SPIRIT_DISABLE_UTREE_IO)
+ #include <ios>
+#endif
 #include <boost/spirit/home/support/utree/utree.hpp>
 #include <boost/preprocessor/cat.hpp>
 #include <boost/throw_exception.hpp>
@@ -32,12 +35,12 @@
     bool operator<=(utree const& a, utree const& b);
     bool operator>=(utree const& a, utree const& b);
 
- // Input and output
+#if !defined(BOOST_SPIRIT_DISABLE_UTREE_IO)
+ // output
     std::ostream& operator<<(std::ostream& out, utree const& x);
- std::istream& operator>>(std::istream& in, utree& x);
-
     std::ostream& operator<<(std::ostream& out, utree::invalid_type const& x);
     std::ostream& operator<<(std::ostream& out, utree::nil_type const& x);
+#endif
 
     // Logical operators
     utree operator&&(utree const& a, utree const& b);
@@ -190,6 +193,7 @@
         }
     };
 
+#if !defined(BOOST_SPIRIT_DISABLE_UTREE_IO)
     struct utree_print
     {
         typedef void result_type;
@@ -271,6 +275,7 @@
             return (*this)("<function>");
         }
     };
+#endif
 
     template <typename Base>
     struct logical_function
@@ -494,6 +499,7 @@
         return !(a < b);
     }
 
+#if !defined(BOOST_SPIRIT_DISABLE_UTREE_IO)
     inline std::ostream& operator<<(std::ostream& out, utree const& x)
     {
         utree::visit(x, utree_print(out));
@@ -509,6 +515,7 @@
     {
         return out;
     }
+#endif
 
     BOOST_SPIRIT_UTREE_CREATE_LOGICAL_FUNCTION(and_, a&&b)
     BOOST_SPIRIT_UTREE_CREATE_LOGICAL_FUNCTION(or_, a||b)

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-05-21 19:58:19 EDT (Sat, 21 May 2011)
@@ -24,6 +24,8 @@
 #include <boost/range/iterator_range.hpp>
 #include <boost/type_traits/remove_pointer.hpp>
 #include <boost/type_traits/is_polymorphic.hpp>
+#include <boost/shared_array.hpp>
+#include <boost/enable_shared_from_this.hpp>
 #include <boost/utility/enable_if.hpp>
 #include <boost/utility/result_of.hpp>
 #include <boost/ref.hpp>
@@ -67,7 +69,7 @@
             any_type, // A pointer or reference to any C++ type.
             function_type, // A utree holding a stored_function<F> object,
                                 // where F is an unary function object taking a
- // scope as it's parameter and returning a
+ // utree as it's parameter and returning a
                                 // utree.
 
             // numeric atoms
@@ -222,13 +224,13 @@
     // Our function type
     ///////////////////////////////////////////////////////////////////////////
     class utree;
- class scope;
 
     //[utree_function_object_interface
     struct function_base
     {
         virtual ~function_base() {}
- virtual utree operator()(scope const& env) const = 0;
+ virtual utree operator()(utree const& env) const = 0;
+ virtual utree operator()(utree& env) = 0;
 
         // Calling f.clone() must return a newly allocated function_base
         // instance that is equal to f.
@@ -241,7 +243,8 @@
         F f;
         stored_function(F f = F());
         virtual ~stored_function();
- virtual utree operator()(scope const& env) const;
+ virtual utree operator()(utree const& env) const;
+ virtual utree operator()(utree& env);
         virtual function_base* clone() const;
     };
     
@@ -251,7 +254,8 @@
         F& f;
         referenced_function(F& f);
         virtual ~referenced_function();
- virtual utree operator()(scope const& env) const;
+ virtual utree operator()(utree const& env) const;
+ virtual utree operator()(utree& env);
         virtual function_base* clone() const;
     };
     //]
@@ -416,20 +420,13 @@
         utree(boost::iterator_range<Iterator>);
         template <class Iterator>
         reference operator=(boost::iterator_range<Iterator>);
-
- // This initializes a `function_type` node, which can store an
- // arbitrary function or function object.
- template <class F>
- 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 a `function_type` node from a polymorphic function
+ // object pointer (takes ownership) or reference.
+ utree(function_base const&);
+ reference operator=(function_base const&);
+ utree(function_base*);
+ reference operator=(function_base*);
 
         // This initializes either a `string_type`, a `symbol_type`, or a
         // `binary_type` node (depending on the template parameter `type_`),
@@ -554,8 +551,11 @@
         short tag() const;
         void tag(short);
 
- utree eval(scope const&) const;
+ utree eval(utree const&) const;
+ utree eval(utree&) const;
 
+ utree operator() (utree const&) const;
+ utree operator() (utree&) const;
     //<-
     protected:
         void ensure_list_type(char const* failed_in = "ensure_list_type()");
@@ -566,7 +566,6 @@
         template <class UTreeX, class UTreeY>
         friend struct detail::visit_impl;
         friend struct detail::index_impl;
- friend struct detail::assign_impl;
 
         type::info get_type() const;
         void set_type(type::info);
@@ -615,42 +614,6 @@
     utree::invalid_type const invalid = {};
     utree::nil_type const nil = {};
     utree::list_type const empty_list = utree::list_type();
-
- ///////////////////////////////////////////////////////////////////////////
- //[utree_scope
- class scope : public boost::iterator_range<utree*>
- {
- public:
- scope(utree* first = 0,
- utree* last = 0,
- scope const* parent = 0)
- //<-
- : boost::iterator_range<utree*>(first, last)
- , parent(parent)
- , depth(parent? parent->depth + 1 : 0) {}
- //->
-
- scope const* outer() const
- //<-
- {
- return parent;
- }
- //->
-
- std::size_t level() const
- //<-
- {
- return depth;
- }
- //->
-
- //<-
- private:
- scope const* parent;
- std::size_t depth;
- //->
- };
- //]
 }}
 
 #if defined(BOOST_MSVC)

Modified: trunk/libs/spirit/test/support/utree.cpp
==============================================================================
--- trunk/libs/spirit/test/support/utree.cpp (original)
+++ trunk/libs/spirit/test/support/utree.cpp 2011-05-21 19:58:19 EDT (Sat, 21 May 2011)
@@ -32,7 +32,7 @@
 
 struct one_two_three
 {
- boost::spirit::utree operator()(boost::spirit::scope) const
+ boost::spirit::utree operator()(boost::spirit::utree) const
     {
         return boost::spirit::utree(123);
     }
@@ -40,7 +40,7 @@
 
 struct this_
 {
- boost::spirit::utree operator()(boost::spirit::scope) const
+ boost::spirit::utree operator()(boost::spirit::utree) const
     {
         return boost::spirit::utree(static_cast<int>(boost::hash_value(this)));
     }
@@ -407,20 +407,18 @@
     {
         // test functions
         using boost::spirit::stored_function;
- using boost::spirit::scope;
 
         utree f = stored_function<one_two_three>();
- f.eval(scope());
+ f.eval(utree());
     }
     
     {
         // 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()));
+ BOOST_TEST_EQ(ff.eval(utree()), f(utree()));
     }
 
     {


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