1 /*=============================================================================
2 Copyright (c) 2002 2004 2006 Joel de Guzman
3 Copyright (c) 2004 Eric Niebler
4 Copyright (c) 2011 Daniel James
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 =============================================================================*/
11 #if !defined(BOOST_QUICKBOOK_FILES_HPP)
12 #define BOOST_QUICKBOOK_FILES_HPP
15 #include <boost/filesystem/path.hpp>
16 #include <boost/intrusive_ptr.hpp>
17 #include <boost/utility/string_ref.hpp>
24 namespace fs = boost::filesystem;
27 typedef boost::intrusive_ptr<file> file_ptr;
31 file_position() : line(1), column(1) {}
32 file_position(int l, int c) : line(l), column(c) {}
37 bool operator==(file_position const& other) const
39 return line == other.line && column == other.column;
42 friend std::ostream& operator<<(std::ostream&, file_position const&);
49 file& operator=(file const&);
54 bool is_code_snippets;
59 boost::string_ref source() const { return source_; }
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)
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)
77 unsigned version() const {
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);
90 virtual file_position position_of(boost::string_ref::const_iterator) const;
92 friend void intrusive_ptr_add_ref(file* ptr) { ++ptr->ref_count; }
94 friend void intrusive_ptr_release(file* ptr)
95 { if(--ptr->ref_count == 0) delete ptr; }
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);
102 struct load_error : std::runtime_error
104 explicit load_error(std::string const& arg)
105 : std::runtime_error(arg) {}
108 // Interface for creating fake files which are mapped to
109 // real files, so that the position can be found later.
111 struct mapped_file_builder_data;
113 struct mapped_file_builder
115 typedef boost::string_ref::const_iterator iterator;
116 typedef boost::string_ref::size_type pos;
118 mapped_file_builder();
119 ~mapped_file_builder();
121 void start(file_ptr);
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);
134 mapped_file_builder_data* data;
136 mapped_file_builder(mapped_file_builder const&);
137 mapped_file_builder& operator=(mapped_file_builder const&);
141 #endif // BOOST_QUICKBOOK_FILES_HPP