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
17 #include <boost/scoped_ptr.hpp>
18 #include <boost/iterator/iterator_traits.hpp>
19 #include <boost/utility/string_ref.hpp>
32 ////////////////////////////////////////////////////////////////////////
38 value_node(value_node const&);
39 value_node& operator=(value_node const&);
45 explicit value_node(tag_type);
46 virtual ~value_node();
49 virtual char const* type_name() const = 0;
50 virtual value_node* clone() const = 0;
52 virtual file_ptr get_file() const;
53 virtual string_iterator get_position() const;
54 virtual boost::string_ref get_quickbook() const;
55 virtual std::string get_encoded() const;
56 virtual int get_int() const;
58 virtual bool check() const;
59 virtual bool empty() const;
60 virtual bool is_encoded() const;
61 virtual bool is_list() const;
62 virtual bool equals(value_node*) const;
64 virtual value_node* get_list() const;
70 friend void intrusive_ptr_add_ref(value_node* ptr)
71 { ++ptr->ref_count_; }
72 friend void intrusive_ptr_release(value_node* ptr)
73 { if(--ptr->ref_count_ == 0) delete ptr; }
76 ////////////////////////////////////////////////////////////////////////
79 // This defines most of the public methods for value.
80 // 'begin' and 'end' are defined with the iterators later.
87 typedef iterator const_iterator;
88 typedef value_node::tag_type tag_type;
89 enum { default_tag = 0 };
92 explicit value_base(value_node* base)
100 void swap(value_base& x) { std::swap(value_, x.value_); }
102 bool check() const { return value_->check(); }
103 bool empty() const { return value_->empty(); }
104 bool is_encoded() const { return value_->is_encoded(); }
105 bool is_list() const { return value_->is_list(); }
107 iterator begin() const;
108 iterator end() const;
111 int get_tag() const { return value_->tag_; }
112 file_ptr get_file() const
113 { return value_->get_file(); }
114 string_iterator get_position() const
115 { return value_->get_position(); }
116 boost::string_ref get_quickbook() const
117 { return value_->get_quickbook(); }
118 std::string get_encoded() const
119 { return value_->get_encoded(); }
121 { return value_->get_int(); }
123 // Equality is pretty inefficient. Not really designed for anything
124 // more than testing purposes.
125 friend bool operator==(value_base const& x, value_base const& y)
126 { return x.value_->equals(y.value_); }
131 // value_builder needs to access 'value_' to get the node
133 friend struct quickbook::value_builder;
136 ////////////////////////////////////////////////////////////////////////
137 // Reference and proxy values for use in iterators
139 struct value_ref : public value_base
142 explicit value_ref(value_node* base) : value_base(base) {}
145 struct value_proxy : public value_base
148 explicit value_proxy(value_node* base) : value_base(base) {}
149 value_proxy* operator->() { return this; }
150 value_ref operator*() const { return value_ref(value_); }
153 ////////////////////////////////////////////////////////////////////////
156 struct value_base::iterator
157 : public boost::forward_iterator_helper<
158 iterator, value, int, value_proxy, value_ref>
162 explicit iterator(value_node* p) : ptr_(p) {}
163 friend bool operator==(iterator x, iterator y)
164 { return x.ptr_ == y.ptr_; }
165 iterator& operator++() { ptr_ = ptr_->next_; return *this; }
166 value_ref operator*() const { return value_ref(ptr_); }
167 value_proxy operator->() const { return value_proxy(ptr_); }
172 inline value_base::iterator value_base::begin() const
174 return iterator(value_->get_list());
177 inline value_base::iterator value_base::end() const
182 ////////////////////////////////////////////////////////////////////////
183 // Reference counting for values
185 struct value_counted : public value_base
187 value_counted& operator=(value_counted const&);
190 value_counted(value_counted const&);
191 value_counted(value_base const&);
192 value_counted(value_node*);
196 ////////////////////////////////////////////////////////////////////////
199 // Values are immutable, so this class is used to build a list of
200 // value nodes before constructing the value.
202 struct value_list_builder {
203 value_list_builder(value_list_builder const&);
204 value_list_builder& operator=(value_list_builder const&);
206 value_list_builder();
207 value_list_builder(value_node*);
208 ~value_list_builder();
209 void swap(value_list_builder& b);
210 value_node* release();
212 void append(value_node*);
222 ////////////////////////////////////////////////////////////////////////////
225 // Most of the methods are in value_base.
227 struct value : public detail::value_counted
232 value(detail::value_base const&);
233 explicit value(detail::value_node*);
234 value& operator=(value);
235 void swap(value& x) { detail::value_counted::swap(x); }
239 value empty_value(value::tag_type = value::default_tag);
242 value int_value(int, value::tag_type = value::default_tag);
246 // Quickbook strings contain a reference to the original quickbook source.
247 value qbk_value(file_ptr const&, string_iterator, string_iterator,
248 value::tag_type = value::default_tag);
250 // Encoded strings are either plain text or boostbook.
251 value encoded_value(std::string const&,
252 value::tag_type = value::default_tag);
254 // An encoded quickbook string is an encoded string that contains a
255 // reference to the quickbook source it was generated from.
256 value encoded_qbk_value(file_ptr const&, string_iterator, string_iterator,
257 std::string const&, value::tag_type = value::default_tag);
259 ////////////////////////////////////////////////////////////////////////////
262 // Used to incrementally build a valueeter tree.
264 struct value_builder {
267 void swap(value_builder& b);
274 void insert(value const&);
275 void extend(value const&);
277 void start_list(value::tag_type = value::default_tag);
285 detail::value_list_builder current;
286 value::tag_type list_tag;
287 boost::scoped_ptr<value_builder> saved;
290 ////////////////////////////////////////////////////////////////////////////
294 struct value_error : public std::logic_error
297 explicit value_error(std::string const&);
300 ////////////////////////////////////////////////////////////////////////////
303 // Convenience class for unpacking value values.
305 struct value_consumer {
308 : public boost::input_iterator_helper<iterator,
309 boost::iterator_value<value::iterator>::type,
310 boost::iterator_difference<value::iterator>::type,
311 boost::iterator_pointer<value::iterator>::type,
312 boost::iterator_reference<value::iterator>::type>
316 explicit iterator(value::iterator* p) : ptr_(p) {}
317 friend bool operator==(iterator x, iterator y)
318 { return *x.ptr_ == *y.ptr_; }
319 iterator& operator++() { ++*ptr_; return *this; }
320 reference operator*() const { return **ptr_; }
321 pointer operator->() const { return ptr_->operator->(); }
323 value::iterator* ptr_;
326 typedef iterator const_iterator;
327 typedef iterator::reference reference;
329 value_consumer(value const& x)
335 value_consumer(reference x)
347 reference consume(value::tag_type t)
353 value optional_consume()
363 value optional_consume(value::tag_type t)
378 bool check(value::tag_type t) const
380 return pos_ != end_ && t == pos_->get_tag();
386 throw value_error("Not all values handled.");
389 iterator begin() { return iterator(&pos_); }
390 iterator end() { return iterator(&end_); }
393 void assert_check() const
396 throw value_error("Attempt to read past end of value list.");
399 void assert_check(value::tag_type t) const
402 if (t != pos_->get_tag())
403 throw value_error("Incorrect value tag.");
407 value::iterator pos_, end_;