]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/quickbook/src/files.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / tools / quickbook / src / files.hpp
1 /*=============================================================================
2 Copyright (c) 2002 2004 2006 Joel de Guzman
3 Copyright (c) 2004 Eric Niebler
4 Copyright (c) 2011 Daniel James
5
6 Use, modification and distribution is subject to the Boost Software
7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10
11 #if !defined(BOOST_QUICKBOOK_FILES_HPP)
12 #define BOOST_QUICKBOOK_FILES_HPP
13
14 #include <string>
15 #include <boost/filesystem/path.hpp>
16 #include <boost/intrusive_ptr.hpp>
17 #include <boost/utility/string_ref.hpp>
18 #include <stdexcept>
19 #include <cassert>
20 #include <iosfwd>
21
22 namespace quickbook {
23
24 namespace fs = boost::filesystem;
25
26 struct file;
27 typedef boost::intrusive_ptr<file> file_ptr;
28
29 struct file_position
30 {
31 file_position() : line(1), column(1) {}
32 file_position(int l, int c) : line(l), column(c) {}
33
34 int line;
35 int column;
36
37 bool operator==(file_position const& other) const
38 {
39 return line == other.line && column == other.column;
40 }
41
42 friend std::ostream& operator<<(std::ostream&, file_position const&);
43 };
44
45 struct file
46 {
47 private:
48 // Non copyable
49 file& operator=(file const&);
50 file(file const&);
51 public:
52 fs::path const path;
53 std::string source_;
54 bool is_code_snippets;
55 private:
56 unsigned qbk_version;
57 unsigned ref_count;
58 public:
59 boost::string_ref source() const { return source_; }
60
61 file(fs::path const& path, boost::string_ref source,
62 unsigned qbk_version) :
63 path(path), source_(source.begin(), source.end()), is_code_snippets(false),
64 qbk_version(qbk_version), ref_count(0)
65 {}
66
67 file(file const& f, boost::string_ref source) :
68 path(f.path), source_(source.begin(), source.end()),
69 is_code_snippets(f.is_code_snippets),
70 qbk_version(f.qbk_version), ref_count(0)
71 {}
72
73 virtual ~file() {
74 assert(!ref_count);
75 }
76
77 unsigned version() const {
78 assert(qbk_version);
79 return qbk_version;
80 }
81
82 void version(unsigned v) {
83 // Check that either version hasn't been set, or it was
84 // previously set to the same version (because the same
85 // file has been loaded twice).
86 assert(!qbk_version || qbk_version == v);
87 qbk_version = v;
88 }
89
90 virtual file_position position_of(boost::string_ref::const_iterator) const;
91
92 friend void intrusive_ptr_add_ref(file* ptr) { ++ptr->ref_count; }
93
94 friend void intrusive_ptr_release(file* ptr)
95 { if(--ptr->ref_count == 0) delete ptr; }
96 };
97
98 // If version isn't supplied then it must be set later.
99 file_ptr load(fs::path const& filename,
100 unsigned qbk_version = 0);
101
102 struct load_error : std::runtime_error
103 {
104 explicit load_error(std::string const& arg)
105 : std::runtime_error(arg) {}
106 };
107
108 // Interface for creating fake files which are mapped to
109 // real files, so that the position can be found later.
110
111 struct mapped_file_builder_data;
112
113 struct mapped_file_builder
114 {
115 typedef boost::string_ref::const_iterator iterator;
116 typedef boost::string_ref::size_type pos;
117
118 mapped_file_builder();
119 ~mapped_file_builder();
120
121 void start(file_ptr);
122 file_ptr release();
123 void clear();
124
125 bool empty() const;
126 pos get_pos() const;
127
128 void add_at_pos(boost::string_ref, iterator);
129 void add(boost::string_ref);
130 void add(mapped_file_builder const&);
131 void add(mapped_file_builder const&, pos, pos);
132 void unindent_and_add(boost::string_ref);
133 private:
134 mapped_file_builder_data* data;
135
136 mapped_file_builder(mapped_file_builder const&);
137 mapped_file_builder& operator=(mapped_file_builder const&);
138 };
139 }
140
141 #endif // BOOST_QUICKBOOK_FILES_HPP