Boost logo

Boost :

From: Paul Hamilton (paul_at_[hidden])
Date: 2003-08-21 20:44:36


I wrote "insert" based on "append" so that you could insert the results
of a parse into a set. It works great!

I based it on "append" in "actions.hpp".

here it is (for inclusion into boost):

/
*=======================================================================
======
     Spirit v1.6.0
     Copyright (c) 1998-2003 Joel de Guzman
     http://spirit.sourceforge.net/

     Permission to copy, use, modify, sell and distribute this software
is
     granted provided this copyright notice appears in all copies. This
     software is provided "as is" without express or implied warranty,
and
     with no claim as to its suitability for any purpose.
========================================================================
=====*/
#ifndef BOOST_SPIRIT_ACTIONS_INSERT_HPP
#define BOOST_SPIRIT_ACTIONS_INSERT_HPP

///////////////////////////////////////////////////////////////////////
////////
#include "boost/spirit/core/composite/actions.hpp"

///////////////////////////////////////////////////////////////////////
////////
namespace boost { namespace spirit {

      
///////////////////////////////////////////////////////////////////////
////
     //
     // insert_actor class
     //
     // insert_actor is a predefined semantic action functor. It
can be
     // used to extract the result of a successful parse and
inserting it to
     // a variable. The functor overloads two function call
operators:
     // operator(), one that takes in a single value argument and
another
     // that accepts two iterators (first and last).
     //
     // The constructor expects a reference to a variable. The
functor is
     // polymorphic and should work with any variable type as long
as it
     // is compatible with the requirements outlined below.
     //
     // 1 The variable is assumed to be a container of some
sort. An
     // STL container is a perfectly valid candidate.
     //
     // 2 The single argument function call operator inserts the
     // extracted parser result and inserts it to the
container.
     // The container is required to accept the statement:
     //
     // c.insert(value)
     //
     // where c is the container and value is the extracted
result
     // of the parser.
     //
     // 2.a The container is required to have a member
function
     // end() that returns an iterator to its 'end'
element.
     //
     // 2.b The container is required to have a member
function
     // insert that takes in the 'end' iterator and a
value
     // compatible with the container's element type.
     //
     // 3 The function call operator that takes in the
first/last
     // iterator first constructs a value from the iterator
pair
     // before inserting the value to the container. The
container
     // is required to accept the statement:
     //
     // c.insert(T::value_type(first, last));
     //
     // where c is the container and T is the container type.
In
     // addition to the requirements 1 and 2 above,
     //
     // 3.a The container is also required to have a typedef
     // value_type (the container's value type) that can
be
     // constructed given a first/last iterator pair.
     //
     // Requirement 2 is exclusive of requirement 3.
Requirement 3
     // only applies if the corresponding double argument
operator
     // is actually called.
     //
     // Instances of insert_actor are not created directly. Instead
a
     // generator function:
     //
     // insert(T& ref)
     //
     // taking in a reference to a variable of arbitrary type is
used to
     // instantiate an insert_actor object of the proper type.
     //
      
///////////////////////////////////////////////////////////////////////
////
        template <typename T>
        class insert_actor
        {
        public:
        
                explicit
                insert_actor(T& ref_)
                : ref(ref_) {}
        
                template <typename T2>
                void operator()(T2 const& val) const
                { ref.insert(val); }
        
                template <typename IteratorT>
                void operator()(IteratorT const& first, IteratorT const& last) const
                {
                        typedef typename T::value_type value_type;
                        ref.insert(value_type(first, last));
                }
        
        private:
        
                T& ref;
        };

     //////////////////////////////////
        template <typename T>
        inline insert_actor<T> const
        insert(T& ref)
        {
                return insert_actor<T>(ref);
        }

}} // namespace boost::spirit

#endif

---------------------
Paul Hamilton
pHamtec P/L - Software Makers
http://www.phamtec.com/
mailto:paul_at_[hidden]

The information transmitted is intended only for the person or entity
to which it is addressed and may contain confidential and/or privileged
material. Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by persons or
entities other than the intended recipient is prohibited. If you
received this in error, please contact the sender and delete the
material from any computer.
-----------------------------------------------------


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk