1 /*=============================================================================
2 Copyright (c) 2010-2011 Daniel James
4 Use, modification and distribution is subject to the Boost Software
5 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
9 // An easy way to store data parsed for quickbook.
11 #if !defined(BOOST_SPIRIT_QUICKBOOK_VALUES_HPP)
12 #define BOOST_SPIRIT_QUICKBOOK_VALUES_HPP
18 #include <boost/iterator/iterator_traits.hpp>
19 #include <boost/operators.hpp>
20 #include <boost/scoped_ptr.hpp>
23 #include "string_view.hpp"
33 ////////////////////////////////////////////////////////////////////////
39 value_node(value_node const&);
40 value_node& operator=(value_node const&);
46 explicit value_node(tag_type);
47 virtual ~value_node();
50 virtual char const* type_name() const = 0;
51 virtual value_node* clone() const = 0;
53 virtual file_ptr get_file() const;
54 virtual string_iterator get_position() const;
55 virtual quickbook::string_view get_quickbook() const;
56 virtual std::string get_encoded() const;
57 virtual int get_int() const;
59 virtual bool check() const;
60 virtual bool empty() const;
61 virtual bool is_encoded() const;
62 virtual bool is_list() const;
63 virtual bool equals(value_node*) const;
65 virtual value_node* get_list() const;
71 friend void intrusive_ptr_add_ref(value_node* ptr)
75 friend void intrusive_ptr_release(value_node* ptr)
77 if (--ptr->ref_count_ == 0) delete ptr;
81 ////////////////////////////////////////////////////////////////////////
84 // This defines most of the public methods for value.
85 // 'begin' and 'end' are defined with the iterators later.
92 typedef iterator const_iterator;
93 typedef value_node::tag_type tag_type;
100 explicit value_base(value_node* base) : value_(base)
107 void swap(value_base& x) { std::swap(value_, x.value_); }
110 bool check() const { return value_->check(); }
111 bool empty() const { return value_->empty(); }
112 bool is_encoded() const { return value_->is_encoded(); }
113 bool is_list() const { return value_->is_list(); }
115 iterator begin() const;
116 iterator end() const;
119 tag_type get_tag() const { return value_->tag_; }
120 file_ptr get_file() const { return value_->get_file(); }
121 string_iterator get_position() const
123 return value_->get_position();
125 quickbook::string_view get_quickbook() const
127 return value_->get_quickbook();
129 std::string get_encoded() const { return value_->get_encoded(); }
130 int get_int() const { return value_->get_int(); }
132 // Equality is pretty inefficient. Not really designed for anything
133 // more than testing purposes.
134 friend bool operator==(value_base const& x, value_base const& y)
136 return x.value_->equals(y.value_);
142 // value_builder needs to access 'value_' to get the node
144 friend struct quickbook::value_builder;
147 ////////////////////////////////////////////////////////////////////////
148 // Reference and proxy values for use in iterators
150 struct value_ref : public value_base
153 explicit value_ref(value_node* base) : value_base(base) {}
156 struct value_proxy : public value_base
159 explicit value_proxy(value_node* base) : value_base(base) {}
160 value_proxy* operator->() { return this; }
161 value_ref operator*() const { return value_ref(value_); }
164 ////////////////////////////////////////////////////////////////////////
167 struct value_base::iterator : public boost::forward_iterator_helper<
176 explicit iterator(value_node* p) : ptr_(p) {}
177 friend bool operator==(iterator x, iterator y)
179 return x.ptr_ == y.ptr_;
181 iterator& operator++()
186 value_ref operator*() const { return value_ref(ptr_); }
187 value_proxy operator->() const { return value_proxy(ptr_); }
193 inline value_base::iterator value_base::begin() const
195 return iterator(value_->get_list());
198 inline value_base::iterator value_base::end() const
203 ////////////////////////////////////////////////////////////////////////
204 // Reference counting for values
206 struct value_counted : public value_base
208 value_counted& operator=(value_counted const&);
212 value_counted(value_counted const&);
213 value_counted(value_base const&);
214 value_counted(value_node*);
218 ////////////////////////////////////////////////////////////////////////
221 // Values are immutable, so this class is used to build a list of
222 // value nodes before constructing the value.
224 struct value_list_builder
226 value_list_builder(value_list_builder const&);
227 value_list_builder& operator=(value_list_builder const&);
230 value_list_builder();
231 value_list_builder(value_node*);
232 ~value_list_builder();
233 void swap(value_list_builder& b);
234 value_node* release();
236 void append(value_node*);
247 ////////////////////////////////////////////////////////////////////////////
250 // Most of the methods are in value_base.
252 struct value : public detail::value_counted
257 value(detail::value_base const&);
258 explicit value(detail::value_node*);
259 value& operator=(value);
260 void swap(value& x) { detail::value_counted::swap(x); }
264 value empty_value(value::tag_type = value::default_tag);
267 value int_value(int, value::tag_type = value::default_tag);
271 // Quickbook strings contain a reference to the original quickbook source.
276 value::tag_type = value::default_tag);
278 // Encoded strings are either plain text or boostbook.
280 std::string const&, value::tag_type = value::default_tag);
282 // An encoded quickbook string is an encoded string that contains a
283 // reference to the quickbook source it was generated from.
284 value encoded_qbk_value(
289 value::tag_type = value::default_tag);
291 ////////////////////////////////////////////////////////////////////////////
294 // Used to incrementally build a valueeter tree.
300 void swap(value_builder& b);
307 void insert(value const&);
308 void extend(value const&);
310 void start_list(value::tag_type = value::default_tag);
318 detail::value_list_builder current;
319 value::tag_type list_tag;
320 boost::scoped_ptr<value_builder> saved;
323 ////////////////////////////////////////////////////////////////////////////
327 struct value_error : public std::logic_error
330 explicit value_error(std::string const&);
333 ////////////////////////////////////////////////////////////////////////////
336 // Convenience class for unpacking value values.
338 struct value_consumer
341 struct iterator : public boost::input_iterator_helper<
343 boost::iterator_value<value::iterator>::type,
344 boost::iterator_difference<value::iterator>::type,
345 boost::iterator_pointer<value::iterator>::type,
346 boost::iterator_reference<value::iterator>::type>
350 explicit iterator(value::iterator* p) : ptr_(p) {}
351 friend bool operator==(iterator x, iterator y)
353 return *x.ptr_ == *y.ptr_;
355 iterator& operator++()
360 reference operator*() const { return **ptr_; }
361 pointer operator->() const { return ptr_->operator->(); }
364 value::iterator* ptr_;
367 typedef iterator const_iterator;
368 typedef iterator::reference reference;
370 value_consumer(value const& x)
371 : list_(x), pos_(x.begin()), end_(x.end())
375 value_consumer(reference x) : list_(x), pos_(x.begin()), end_(x.end())
385 reference consume(value::tag_type t)
391 value optional_consume()
401 value optional_consume(value::tag_type t)
411 bool check() const { return pos_ != end_; }
413 bool check(value::tag_type t) const
415 return pos_ != end_ && t == pos_->get_tag();
420 if (pos_ != end_) throw value_error("Not all values handled.");
423 iterator begin() { return iterator(&pos_); }
424 iterator end() { return iterator(&end_); }
427 void assert_check() const
430 throw value_error("Attempt to read past end of value list.");
433 void assert_check(value::tag_type t) const
436 if (t != pos_->get_tag()) throw value_error("Incorrect value tag.");
440 value::iterator pos_, end_;