PageRenderTime 56ms CodeModel.GetById 15ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/property_tree/string_path.hpp

http://hadesmem.googlecode.com/
C++ Header | 275 lines | 193 code | 30 blank | 52 comment | 11 complexity | 2125134d600b52308d2ac52584fdf4b5 MD5 | raw file
  1// ----------------------------------------------------------------------------
  2// Copyright (C) 2009 Sebastian Redl
  3//
  4// Distributed under the Boost Software License, Version 1.0. 
  5// (See accompanying file LICENSE_1_0.txt or copy at 
  6// http://www.boost.org/LICENSE_1_0.txt)
  7//
  8// For more information, see www.boost.org
  9// ----------------------------------------------------------------------------
 10
 11#ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
 12#define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
 13
 14#include <boost/property_tree/ptree_fwd.hpp>
 15#include <boost/property_tree/id_translator.hpp>
 16#include <boost/property_tree/exceptions.hpp>
 17#include <boost/property_tree/detail/ptree_utils.hpp>
 18
 19#include <boost/static_assert.hpp>
 20#include <boost/assert.hpp>
 21#include <boost/type_traits/is_same.hpp>
 22#include <boost/optional.hpp>
 23#include <boost/throw_exception.hpp>
 24#include <algorithm>
 25#include <string>
 26#include <iterator>
 27
 28namespace boost { namespace property_tree
 29{
 30    namespace detail
 31    {
 32        template <typename Sequence, typename Iterator>
 33        void append_and_preserve_iter(Sequence &s, const Sequence &r,
 34                                      Iterator &, std::forward_iterator_tag)
 35        {
 36            // Here we boldly assume that anything that is not random-access
 37            // preserves validity. This is valid for the STL sequences.
 38            s.insert(s.end(), r.begin(), r.end());
 39        }
 40        template <typename Sequence, typename Iterator>
 41        void append_and_preserve_iter(Sequence &s, const Sequence &r,
 42                                      Iterator &it,
 43                                      std::random_access_iterator_tag)
 44        {
 45            // Convert the iterator to an index, and later back.
 46            typename std::iterator_traits<Iterator>::difference_type idx =
 47                it - s.begin();
 48            s.insert(s.end(), r.begin(), r.end());
 49            it = s.begin() + idx;
 50        }
 51
 52        template <typename Sequence>
 53        inline std::string dump_sequence(const Sequence &)
 54        {
 55            return "<undumpable sequence>";
 56        }
 57        inline std::string dump_sequence(const std::string &s)
 58        {
 59            return s;
 60        }
 61#ifndef BOOST_NO_STD_WSTRING
 62        inline std::string dump_sequence(const std::wstring &s)
 63        {
 64            return narrow(s.c_str());
 65        }
 66#endif
 67    }
 68
 69    /// Default path class. A path is a sequence of values. Groups of values
 70    /// are separated by the separator value, which defaults to '.' cast to
 71    /// the sequence's value type. The group of values is then passed to the
 72    /// translator to get a key.
 73    ///
 74    /// If instantiated with std::string and id_translator\<std::string\>,
 75    /// it accepts paths of the form "one.two.three.four".
 76    ///
 77    /// @tparam String Any Sequence. If the sequence does not support random-
 78    ///                access iteration, concatenation of paths assumes that
 79    ///                insertions at the end preserve iterator validity.
 80    /// @tparam Translator A translator with internal_type == String.
 81    template <typename String, typename Translator>
 82    class string_path
 83    {
 84        BOOST_STATIC_ASSERT((is_same<String,
 85                                   typename Translator::internal_type>::value));
 86    public:
 87        typedef typename Translator::external_type key_type;
 88        typedef typename String::value_type char_type;
 89
 90        /// Create an empty path.
 91        explicit string_path(char_type separator = char_type('.'));
 92        /// Create a path by parsing the given string.
 93        /// @param value A sequence, possibly with separators, that describes
 94        ///              the path, e.g. "one.two.three".
 95        /// @param separator The separator used in parsing. Defaults to '.'.
 96        /// @param tr The translator used by this path to convert the individual
 97        ///           parts to keys.
 98        string_path(const String &value, char_type separator = char_type('.'),
 99                    Translator tr = Translator());
100        /// Create a path by parsing the given string.
101        /// @param value A zero-terminated array of values. Only use if zero-
102        ///              termination makes sense for your type, and your
103        ///              sequence supports construction from it. Intended for
104        ///              string literals.
105        /// @param separator The separator used in parsing. Defaults to '.'.
106        /// @param tr The translator used by this path to convert the individual
107        ///           parts to keys.
108        string_path(const char_type *value,
109                    char_type separator = char_type('.'),
110                    Translator tr = Translator());
111
112        // Default copying doesn't do the right thing with the iterator
113        string_path(const string_path &o);
114        string_path& operator =(const string_path &o);
115
116        /// Take a single element off the path at the front and return it.
117        key_type reduce();
118
119        /// Test if the path is empty.
120        bool empty() const;
121
122        /// Test if the path contains a single element, i.e. no separators.
123        bool single() const;
124
125        std::string dump() const {
126            return detail::dump_sequence(m_value);
127        }
128
129        /// Append a second path to this one.
130        /// @pre o's separator is the same as this one's, or o has no separators
131        string_path& operator /=(const string_path &o) {
132            // If it's single, there's no separator. This allows to do
133            // p /= "piece";
134            // even for non-default separators.
135            BOOST_ASSERT((m_separator == o.m_separator
136                          || o.empty()
137                          || o.single())
138                         && "Incompatible paths.");
139            if(!o.empty()) {
140                String sub;
141                if(!this->empty()) {
142                    sub.push_back(m_separator);
143                }
144                sub.insert(sub.end(), o.cstart(), o.m_value.end());
145                detail::append_and_preserve_iter(m_value, sub, m_start,
146                    typename std::iterator_traits<s_iter>::iterator_category());
147            }
148            return *this;
149        }
150
151    private:
152        typedef typename String::iterator s_iter;
153        typedef typename String::const_iterator s_c_iter;
154        String m_value;
155        char_type m_separator;
156        Translator m_tr;
157        s_iter m_start;
158        s_c_iter cstart() const { return m_start; }
159    };
160
161    template <typename String, typename Translator> inline
162    string_path<String, Translator>::string_path(char_type separator)
163        : m_separator(separator), m_start(m_value.begin())
164    {}
165
166    template <typename String, typename Translator> inline
167    string_path<String, Translator>::string_path(const String &value,
168                                                 char_type separator,
169                                                 Translator tr)
170        : m_value(value), m_separator(separator),
171          m_tr(tr), m_start(m_value.begin())
172    {}
173
174    template <typename String, typename Translator> inline
175    string_path<String, Translator>::string_path(const char_type *value,
176                                                 char_type separator,
177                                                 Translator tr)
178        : m_value(value), m_separator(separator),
179          m_tr(tr), m_start(m_value.begin())
180    {}
181
182    template <typename String, typename Translator> inline
183    string_path<String, Translator>::string_path(const string_path &o)
184        : m_value(o.m_value), m_separator(o.m_separator),
185          m_tr(o.m_tr), m_start(m_value.begin())
186    {
187        std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
188    }
189
190    template <typename String, typename Translator> inline
191    string_path<String, Translator>&
192    string_path<String, Translator>::operator =(const string_path &o)
193    {
194        m_value = o.m_value;
195        m_separator = o.m_separator;
196        m_tr = o.m_tr;
197        m_start = m_value.begin();
198        std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
199        return *this;
200    }
201
202    template <typename String, typename Translator>
203    typename Translator::external_type string_path<String, Translator>::reduce()
204    {
205        BOOST_ASSERT(!empty() && "Reducing empty path");
206
207        s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
208        String part(m_start, next_sep);
209        m_start = next_sep;
210        if(!empty()) {
211          // Unless we're at the end, skip the separator we found.
212          ++m_start;
213        }
214
215        if(optional<key_type> key = m_tr.get_value(part)) {
216            return *key;
217        }
218        BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
219    }
220
221    template <typename String, typename Translator> inline
222    bool string_path<String, Translator>::empty() const
223    {
224        return m_start == m_value.end();
225    }
226
227    template <typename String, typename Translator> inline
228    bool string_path<String, Translator>::single() const
229    {
230        return std::find(static_cast<s_c_iter>(m_start),
231                         m_value.end(), m_separator)
232            == m_value.end();
233    }
234
235    // By default, this is the path for strings. You can override this by
236    // specializing path_of for a more specific form of std::basic_string.
237    template <typename Ch, typename Traits, typename Alloc>
238    struct path_of< std::basic_string<Ch, Traits, Alloc> >
239    {
240        typedef std::basic_string<Ch, Traits, Alloc> _string;
241        typedef string_path< _string, id_translator<_string> > type;
242    };
243
244    template <typename String, typename Translator> inline
245    string_path<String, Translator> operator /(
246                                  string_path<String, Translator> p1,
247                                  const string_path<String, Translator> &p2)
248    {
249        p1 /= p2;
250        return p1;
251    }
252
253    // These shouldn't be necessary, but GCC won't find the one above.
254    template <typename String, typename Translator> inline
255    string_path<String, Translator> operator /(
256                                  string_path<String, Translator> p1,
257                                  const typename String::value_type *p2)
258    {
259        p1 /= p2;
260        return p1;
261    }
262
263    template <typename String, typename Translator> inline
264    string_path<String, Translator> operator /(
265                                  const typename String::value_type *p1,
266                                  const string_path<String, Translator> &p2)
267    {
268        string_path<String, Translator> t(p1);
269        t /= p2;
270        return t;
271    }
272
273}}
274
275#endif