Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:19

0001 // ----------------------------------------------------------------------------
0002 // Copyright (C) 2009 Sebastian Redl
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. 
0005 // (See accompanying file LICENSE_1_0.txt or copy at 
0006 // http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // For more information, see www.boost.org
0009 // ----------------------------------------------------------------------------
0010 
0011 #ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
0012 #define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
0013 
0014 #include <boost/property_tree/ptree_fwd.hpp>
0015 #include <boost/property_tree/id_translator.hpp>
0016 #include <boost/property_tree/exceptions.hpp>
0017 #include <boost/property_tree/detail/ptree_utils.hpp>
0018 
0019 #include <boost/static_assert.hpp>
0020 #include <boost/assert.hpp>
0021 #include <boost/type_traits/is_same.hpp>
0022 #include <boost/optional/optional.hpp>
0023 #include <boost/throw_exception.hpp>
0024 #include <algorithm>
0025 #include <string>
0026 #include <iterator>
0027 
0028 namespace boost { namespace property_tree
0029 {
0030     namespace detail
0031     {
0032         template <typename Sequence, typename Iterator>
0033         void append_and_preserve_iter(Sequence &s, const Sequence &r,
0034                                       Iterator &, std::forward_iterator_tag)
0035         {
0036             // Here we boldly assume that anything that is not random-access
0037             // preserves validity. This is valid for the STL sequences.
0038             s.insert(s.end(), r.begin(), r.end());
0039         }
0040         template <typename Sequence, typename Iterator>
0041         void append_and_preserve_iter(Sequence &s, const Sequence &r,
0042                                       Iterator &it,
0043                                       std::random_access_iterator_tag)
0044         {
0045             // Convert the iterator to an index, and later back.
0046             typename std::iterator_traits<Iterator>::difference_type idx =
0047                 it - s.begin();
0048             s.insert(s.end(), r.begin(), r.end());
0049             it = s.begin() + idx;
0050         }
0051 
0052         template <typename Sequence>
0053         inline std::string dump_sequence(const Sequence &)
0054         {
0055             return "<undumpable sequence>";
0056         }
0057         inline std::string dump_sequence(const std::string &s)
0058         {
0059             return s;
0060         }
0061 #ifndef BOOST_NO_STD_WSTRING
0062         inline std::string dump_sequence(const std::wstring &s)
0063         {
0064             return narrow<std::string>(s.c_str());
0065         }
0066 #endif
0067     }
0068 
0069     /// Default path class. A path is a sequence of values. Groups of values
0070     /// are separated by the separator value, which defaults to '.' cast to
0071     /// the sequence's value type. The group of values is then passed to the
0072     /// translator to get a key.
0073     ///
0074     /// If instantiated with std::string and id_translator\<std::string\>,
0075     /// it accepts paths of the form "one.two.three.four".
0076     ///
0077     /// @tparam String Any Sequence. If the sequence does not support random-
0078     ///                access iteration, concatenation of paths assumes that
0079     ///                insertions at the end preserve iterator validity.
0080     /// @tparam Translator A translator with internal_type == String.
0081     template <typename String, typename Translator>
0082     class string_path
0083     {
0084         BOOST_STATIC_ASSERT((is_same<String,
0085                                    typename Translator::internal_type>::value));
0086     public:
0087         typedef typename Translator::external_type key_type;
0088         typedef typename String::value_type char_type;
0089 
0090         /// Create an empty path.
0091         explicit string_path(char_type separator = char_type('.'));
0092         /// Create a path by parsing the given string.
0093         /// @param value A sequence, possibly with separators, that describes
0094         ///              the path, e.g. "one.two.three".
0095         /// @param separator The separator used in parsing. Defaults to '.'.
0096         /// @param tr The translator used by this path to convert the individual
0097         ///           parts to keys.
0098         string_path(const String &value, char_type separator = char_type('.'),
0099                     Translator tr = Translator());
0100         /// Create a path by parsing the given string.
0101         /// @param value A zero-terminated array of values. Only use if zero-
0102         ///              termination makes sense for your type, and your
0103         ///              sequence supports construction from it. Intended for
0104         ///              string literals.
0105         /// @param separator The separator used in parsing. Defaults to '.'.
0106         /// @param tr The translator used by this path to convert the individual
0107         ///           parts to keys.
0108         string_path(const char_type *value,
0109                     char_type separator = char_type('.'),
0110                     Translator tr = Translator());
0111 
0112         // Default copying doesn't do the right thing with the iterator
0113         string_path(const string_path &o);
0114         string_path& operator =(const string_path &o);
0115 
0116         /// Take a single element off the path at the front and return it.
0117         key_type reduce();
0118 
0119         /// Test if the path is empty.
0120         bool empty() const;
0121 
0122         /// Test if the path contains a single element, i.e. no separators.
0123         bool single() const;
0124 
0125         /// Get the separator used by this path.
0126         char_type separator() const { return m_separator; }
0127 
0128         std::string dump() const {
0129             return detail::dump_sequence(m_value);
0130         }
0131 
0132         /// Append a second path to this one.
0133         /// @pre o's separator is the same as this one's, or o has no separators
0134         string_path& operator /=(const string_path &o) {
0135             // If it's single, there's no separator. This allows to do
0136             // p /= "piece";
0137             // even for non-default separators.
0138             BOOST_ASSERT((m_separator == o.m_separator
0139                           || o.empty()
0140                           || o.single())
0141                          && "Incompatible paths.");
0142             if(!o.empty()) {
0143                 String sub;
0144                 if(!this->empty()) {
0145                     sub.push_back(m_separator);
0146                 }
0147                 sub.insert(sub.end(), o.cstart(), o.m_value.end());
0148                 detail::append_and_preserve_iter(m_value, sub, m_start,
0149                     typename std::iterator_traits<s_iter>::iterator_category());
0150             }
0151             return *this;
0152         }
0153 
0154     private:
0155         typedef typename String::iterator s_iter;
0156         typedef typename String::const_iterator s_c_iter;
0157         String m_value;
0158         char_type m_separator;
0159         Translator m_tr;
0160         s_iter m_start;
0161         s_c_iter cstart() const { return m_start; }
0162     };
0163 
0164     template <typename String, typename Translator> inline
0165     string_path<String, Translator>::string_path(char_type separator)
0166         : m_separator(separator), m_start(m_value.begin())
0167     {}
0168 
0169     template <typename String, typename Translator> inline
0170     string_path<String, Translator>::string_path(const String &value,
0171                                                  char_type separator,
0172                                                  Translator tr)
0173         : m_value(value), m_separator(separator),
0174           m_tr(tr), m_start(m_value.begin())
0175     {}
0176 
0177     template <typename String, typename Translator> inline
0178     string_path<String, Translator>::string_path(const char_type *value,
0179                                                  char_type separator,
0180                                                  Translator tr)
0181         : m_value(value), m_separator(separator),
0182           m_tr(tr), m_start(m_value.begin())
0183     {}
0184 
0185     template <typename String, typename Translator> inline
0186     string_path<String, Translator>::string_path(const string_path &o)
0187         : m_value(o.m_value), m_separator(o.m_separator),
0188           m_tr(o.m_tr), m_start(m_value.begin())
0189     {
0190         std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
0191     }
0192 
0193     template <typename String, typename Translator> inline
0194     string_path<String, Translator>&
0195     string_path<String, Translator>::operator =(const string_path &o)
0196     {
0197         m_value = o.m_value;
0198         m_separator = o.m_separator;
0199         m_tr = o.m_tr;
0200         m_start = m_value.begin();
0201         std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
0202         return *this;
0203     }
0204 
0205     template <typename String, typename Translator>
0206     typename Translator::external_type string_path<String, Translator>::reduce()
0207     {
0208         BOOST_ASSERT(!empty() && "Reducing empty path");
0209 
0210         s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
0211         String part(m_start, next_sep);
0212         m_start = next_sep;
0213         if(!empty()) {
0214           // Unless we're at the end, skip the separator we found.
0215           ++m_start;
0216         }
0217 
0218         if(optional<key_type> key = m_tr.get_value(part)) {
0219             return *key;
0220         }
0221         BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
0222     }
0223 
0224     template <typename String, typename Translator> inline
0225     bool string_path<String, Translator>::empty() const
0226     {
0227         return m_start == m_value.end();
0228     }
0229 
0230     template <typename String, typename Translator> inline
0231     bool string_path<String, Translator>::single() const
0232     {
0233         return std::find(static_cast<s_c_iter>(m_start),
0234                          m_value.end(), m_separator)
0235             == m_value.end();
0236     }
0237 
0238     // By default, this is the path for strings. You can override this by
0239     // specializing path_of for a more specific form of std::basic_string.
0240     template <typename Ch, typename Traits, typename Alloc>
0241     struct path_of< std::basic_string<Ch, Traits, Alloc> >
0242     {
0243         typedef std::basic_string<Ch, Traits, Alloc> _string;
0244         typedef string_path< _string, id_translator<_string> > type;
0245     };
0246 
0247     template <typename String, typename Translator> inline
0248     string_path<String, Translator> operator /(
0249                                   string_path<String, Translator> p1,
0250                                   const string_path<String, Translator> &p2)
0251     {
0252         p1 /= p2;
0253         return p1;
0254     }
0255 
0256     // These shouldn't be necessary, but GCC won't find the one above.
0257     template <typename String, typename Translator> inline
0258     string_path<String, Translator> operator /(
0259                                   string_path<String, Translator> p1,
0260                                   const typename String::value_type *p2)
0261     {
0262         p1 /= p2;
0263         return p1;
0264     }
0265 
0266     template <typename String, typename Translator> inline
0267     string_path<String, Translator> operator /(
0268                                   const typename String::value_type *p1,
0269                                   const string_path<String, Translator> &p2)
0270     {
0271         string_path<String, Translator> t(p1);
0272         t /= p2;
0273         return t;
0274     }
0275 
0276 }}
0277 
0278 #endif