parser_utils.hpp

Go to the documentation of this file.
00001 /*
00002 IoBind Library License:
00003 --------------------------
00004 
00005 The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux
00006 
00007 This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00012 
00013 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00014 
00015 3. This notice may not be removed or altered from any source distribution
00016 */
00017 
00018 
00019 #ifndef IOBIND_PARSER_UTILS_HPP
00020 #define IOBIND_PARSER_UTILS_HPP
00021 
00022 #include <boost/spirit.hpp>
00023 #include <boost/call_traits.hpp>
00024 #include <string>
00025 
00026 namespace iobind{
00027 namespace parser{
00028 namespace detail{
00029 
00030 template<
00031         typename Container,
00032         typename Policy
00033 > 
00034 class append_with_policy
00035 {
00036 public:
00037     append_with_policy( Container& container_, Policy const& policy_)
00038         : m_container(container_), m_policy(policy_)
00039     {};
00040 
00041     // the method called by the parser    
00042     template <typename IteratorT>
00043     void operator()(IteratorT const& first, IteratorT const& last) const
00044     {
00045                 m_container.insert(m_container.end(), m_policy.encode( std::string(first, last) ) );
00046     }
00047 
00048 private:
00049     Container& m_container;
00050         Policy const& m_policy;
00051 };
00052 
00053 template<
00054         typename Container,
00055         typename Policy
00056 > 
00057 class insert_with_policy
00058 {
00059 public:
00060     insert_with_policy( size_t& index_, Container& container_, Policy const& policy_)
00061         : m_index(index_), m_container(container_), m_policy(policy_)
00062     {};
00063 
00064     // the method called by the parser    
00065     template <typename IteratorT>
00066     void operator()(IteratorT const& first, IteratorT const& last) const
00067     {
00068                 if (m_index < m_container.size())
00069                         m_container[m_index++]=m_policy.encode( std::string(first, last) );
00070 #ifdef _DEBUG
00071                 else
00072                         std::cerr<<"insert_with_policy: could not add data"<<std::endl;
00073 #endif
00074         }
00075 
00076 private:
00077         size_t& m_index;
00078     Container& m_container;
00079         Policy const& m_policy;
00080 };
00081 
00082 template<
00083         typename Pair,
00084         typename FirstPolicy,
00085         typename SecondPolicy
00086 > 
00087 class assign_pair_with_policy
00088 {
00089 public:
00090  
00091         explicit assign_pair_with_policy( 
00092                 Pair& pair_, 
00093                 FirstPolicy const& first_policy_,
00094                 SecondPolicy const& second_policy_,
00095                 std::string const& first_,
00096                 std::string const& second_
00097                 )
00098         : 
00099                 m_pair(pair_), 
00100                 m_first_policy(first_policy_),
00101                 m_second_policy(second_policy_),
00102                 m_first(first_),
00103                 m_second(second_)
00104     {};
00105 
00106     // the method called by the parser    
00107     template <typename IteratorT>
00108     void operator()(IteratorT first, IteratorT last) const
00109     {
00110                 m_pair=Pair(
00111                         m_first_policy.encode(m_first.c_str()),
00112                         m_second_policy.encode(m_second.c_str())
00113                         );
00114     }
00115 
00116 private:
00117     Pair& m_pair;
00118         FirstPolicy const& m_first_policy;
00119         SecondPolicy const& m_second_policy;
00120         std::string const& m_first;
00121         std::string const& m_second;
00122 };
00123 
00124 class concat_string
00125 {
00126 public:
00127     // key_ and val_ should point to the string modified in keyvalue_grammar
00128     // kvc_ is the map of key - values
00129     concat_string( std::ostream& out_)
00130         : out(out_)
00131     {  };
00132 
00133     // the method called by the parser    
00134     template <typename IteratorT>
00135     void operator()(IteratorT first, IteratorT last) const
00136     {
00137                 out<<std::string(first,last);
00138     }
00139 
00140         template <typename IteratorT>
00141         void operator()(IteratorT single) const
00142         {
00143                 out<<single;
00144         }
00145 private:
00146     std::ostream& out;
00147 };
00148 
00149 class concat_symbol
00150 {
00151 public:
00152     // key_ and val_ should point to the string modified in keyvalue_grammar
00153     // kvc_ is the map of key - values
00154     concat_symbol( std::ostream& out_)
00155         : out(out_)
00156     {  };
00157 
00158     // the method called by the parser    
00159         void operator()(std::string const& str) const
00160         {
00161                 out<<str;
00162         }
00163 private:
00164     std::ostream& out;
00165 };
00166 
00167 class concat_pre_post_symbol
00168 {
00169 public:
00170     // key_ and val_ should point to the string modified in keyvalue_grammar
00171     // kvc_ is the map of key - values
00172     concat_pre_post_symbol( std::ostream& out_, std::string const& pre_, std::string const& post_)
00173         : m_out(out_),m_pre(pre_), m_post(post_)
00174     {  };
00175 
00176     // the method called by the parser    
00177         void operator()(std::string const& str_) const
00178         {
00179                 m_out<<m_pre<<str_<<m_post;
00180         }
00181 private:
00182     std::ostream& m_out;
00183         std::string m_pre;
00184         std::string m_post;
00185 };
00186 
00187 };//details
00188 };//parser
00189 };//iobind
00190 
00191 #endif
00192 
00193 

Generated on Thu Jul 6 00:07:26 2006 for zypp by  doxygen 1.4.6