Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-12 08:28:43

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         /// Concatenates two path components
0133         friend string_path operator /(string_path p1, const string_path &p2)
0134         {
0135             p1 /= p2;
0136             return p1;
0137         }
0138 
0139         /// Append a second path to this one.
0140         /// @pre o's separator is the same as this one's, or o has no separators
0141         string_path& operator /=(const string_path &o) {
0142             // If it's single, there's no separator. This allows to do
0143             // p /= "piece";
0144             // even for non-default separators.
0145             BOOST_ASSERT((m_separator == o.m_separator
0146                           || o.empty()
0147                           || o.single())
0148                          && "Incompatible paths.");
0149             if(!o.empty()) {
0150                 String sub;
0151                 if(!this->empty()) {
0152                     sub.push_back(m_separator);
0153                 }
0154                 sub.insert(sub.end(), o.cstart(), o.m_value.end());
0155                 detail::append_and_preserve_iter(m_value, sub, m_start,
0156                     typename std::iterator_traits<s_iter>::iterator_category());
0157             }
0158             return *this;
0159         }
0160 
0161     private:
0162         typedef typename String::iterator s_iter;
0163         typedef typename String::const_iterator s_c_iter;
0164         String m_value;
0165         char_type m_separator;
0166         Translator m_tr;
0167         s_iter m_start;
0168         s_c_iter cstart() const { return m_start; }
0169     };
0170 
0171     template <typename String, typename Translator> inline
0172     string_path<String, Translator>::string_path(char_type separator)
0173         : m_separator(separator), m_start(m_value.begin())
0174     {}
0175 
0176     template <typename String, typename Translator> inline
0177     string_path<String, Translator>::string_path(const String &value,
0178                                                  char_type separator,
0179                                                  Translator tr)
0180         : m_value(value), m_separator(separator),
0181           m_tr(tr), m_start(m_value.begin())
0182     {}
0183 
0184     template <typename String, typename Translator> inline
0185     string_path<String, Translator>::string_path(const char_type *value,
0186                                                  char_type separator,
0187                                                  Translator tr)
0188         : m_value(value), m_separator(separator),
0189           m_tr(tr), m_start(m_value.begin())
0190     {}
0191 
0192     template <typename String, typename Translator> inline
0193     string_path<String, Translator>::string_path(const string_path &o)
0194         : m_value(o.m_value), m_separator(o.m_separator),
0195           m_tr(o.m_tr), m_start(m_value.begin())
0196     {
0197         std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
0198     }
0199 
0200     template <typename String, typename Translator> inline
0201     string_path<String, Translator>&
0202     string_path<String, Translator>::operator =(const string_path &o)
0203     {
0204         m_value = o.m_value;
0205         m_separator = o.m_separator;
0206         m_tr = o.m_tr;
0207         m_start = m_value.begin();
0208         std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
0209         return *this;
0210     }
0211 
0212     template <typename String, typename Translator>
0213     typename Translator::external_type string_path<String, Translator>::reduce()
0214     {
0215         BOOST_ASSERT(!empty() && "Reducing empty path");
0216 
0217         s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
0218         String part(m_start, next_sep);
0219         m_start = next_sep;
0220         if(!empty()) {
0221           // Unless we're at the end, skip the separator we found.
0222           ++m_start;
0223         }
0224 
0225         if(optional<key_type> key = m_tr.get_value(part)) {
0226             return *key;
0227         }
0228         BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
0229     }
0230 
0231     template <typename String, typename Translator> inline
0232     bool string_path<String, Translator>::empty() const
0233     {
0234         return m_start == m_value.end();
0235     }
0236 
0237     template <typename String, typename Translator> inline
0238     bool string_path<String, Translator>::single() const
0239     {
0240         return std::find(static_cast<s_c_iter>(m_start),
0241                          m_value.end(), m_separator)
0242             == m_value.end();
0243     }
0244 
0245     // By default, this is the path for strings. You can override this by
0246     // specializing path_of for a more specific form of std::basic_string.
0247     template <typename Ch, typename Traits, typename Alloc>
0248     struct path_of< std::basic_string<Ch, Traits, Alloc> >
0249     {
0250         typedef std::basic_string<Ch, Traits, Alloc> _string;
0251         typedef string_path< _string, id_translator<_string> > type;
0252     };
0253 }}
0254 
0255 #endif