The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
config_cache.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Pauli Nieminen <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * this file implements game config caching
17  * to speed up startup
18  ***/
19 
20 #ifndef CONFIG_CACHE_HPP_INCLUDED
21 #define CONFIG_CACHE_HPP_INCLUDED
22 
23 #include <list>
24 #include <boost/utility.hpp>
25 #include <boost/scoped_ptr.hpp>
26 #include <boost/shared_ptr.hpp>
27 
29 
30 class config;
31 
32 namespace game_config
33 {
34 
35 /**
36  * Used to set and unset scoped defines to preproc_map
37  *
38  * This is preferred form to set defines that aren't global
39  **/
40 template<typename T>
41 class scoped_preproc_define_internal : private boost::noncopyable
42 {
43 public:
44  /**
45  * Adds normal preproc define.
46  *
47  * @param name name of preproc define to add.
48  * @param add true if we should add this.
49  */
50  scoped_preproc_define_internal(const std::string& name, bool add = true) : name_(name), add_(add)
51  {
52  if(add_) {
53  T::instance().add_define(name_);
54  }
55  }
56 
57  /**
58  * This removes preproc define from cacher
59  */
61  {
62  if(add_) {
63  T::instance().remove_define(name_);
64  }
65  }
66 
67 protected:
68  //
69  // Protected to let test code access
70  //
71 
73  bool add_;
74 
75 };
76 
78 
80 
81 /**
82  * Singleton class to manage game config file caching.
83  * It uses paths to config files as key to find correct cache
84  * @todo Make smarter filetree checksum caching so only required parts
85  * of tree are checked at startup. Trees are overlapping so have
86  * to split trees to subtrees to only do check once per file.
87  * @todo Make cache system easily allow validation of in memory cache objects
88  * using hash checksum of preproc_map.
89  **/
90 class config_cache : private boost::noncopyable
91 {
92 public:
93  /**
94  * Get reference to the singleton object
95  */
96  static config_cache& instance();
97 
98  const preproc_map& get_preproc_map() const;
99 
100  /**
101  * Gets a config object from given @a path.
102  * @param path file to load. Should be _main.cfg.
103  * @param cfg config object that is written to. Should be empty on entry.
104  */
105  void get_config(const std::string& path, config& cfg);
106 
107  /**
108  * Clear stored defines map to default values
109  */
110  void clear_defines();
111 
112  /**
113  * Add a entry to preproc defines map
114  */
115  void add_define(const std::string& define);
116 
117  /**
118  * Remove a entry to preproc defines map
119  */
120  void remove_define(const std::string& define);
121 
122  /**
123  * Enable/disable caching
124  */
125  void set_use_cache(bool use);
126 
127  /**
128  * Enable/disable cache validation
129  */
130  void set_force_valid_cache(bool force);
131 
132  /**
133  * Force cache checksum validation.
134  */
136 
137  /**
138  * Deletes stale cache files not in use by the game.
139  */
140  bool clean_cache();
141 
142  /**
143  * Deletes all cache files.
144  */
145  bool purge_cache();
146 
147 private:
151 
153 
155 
156  void read_file(const std::string& file, config& cfg);
157  void write_file(std::string file, const config& cfg);
158  void write_file(std::string file, const preproc_map& defines);
159 
160  void read_cache(const std::string& path, config& cfg);
161 
162  void read_configs(const std::string& path, config& cfg, preproc_map& defines);
163  void load_configs(const std::string& path, config& cfg);
164  void read_defines_queue();
165  void read_defines_file(const std::string& path);
166 
169 
170  bool delete_cache_files(const std::vector<std::string>& paths,
171  const std::string& exclude_pattern = "");
172 
173 protected:
174  //
175  // Protected to let test code access
176  //
177 
178  config_cache();
179 
180  void set_force_invalid_cache(bool);
181 };
182 
183 class fake_transaction;
184 
185 /**
186  * Used to share macros between cache objects
187  * You have to create transaction object to load all
188  * macros to memory and share them subsequent cache loads.
189  * If transaction is locked all stored macros are still
190  * available but new macros aren't added.
191  **/
192 class config_cache_transaction : private boost::noncopyable
193 {
194 public:
197 
198  /**
199  * Lock the transaction so no more macros are added
200  */
201  void lock();
202 
203  /**
204  * Used to let std::for_each insert new defines to active_map
205  * map to active
206  */
207  void insert_to_active(const preproc_map::value_type& def);
208 
209  enum state
210  {
215  };
216 
217 private:
218  friend class config_cache;
219  friend class fake_transaction;
220 
221  static state state_;
223 
224  std::vector<std::string> define_filenames_;
226 
227  static state get_state()
228  {
229  return state_;
230  }
231 
232  static bool is_active()
233  {
234  return active_ != 0;
235  }
236 
238  {
239  assert(active_);
240  return *active_;
241  }
242 
243  const std::vector<std::string>& get_define_files() const;
244 
245  void add_define_file(const std::string& file);
246 
247  preproc_map& get_active_map(const preproc_map& defines_map);
248 
249  void add_defines_map_diff(preproc_map& defines_map);
250 };
251 
252 /**
253  * Holds a fake cache transaction if no real one is used
254  **/
255 class fake_transaction : private boost::noncopyable
256 {
257  friend class config_cache;
258 
259  typedef boost::scoped_ptr<config_cache_transaction> value_type;
260  value_type trans_;
261 
262  fake_transaction() : trans_()
263  {
265  trans_.reset(new config_cache_transaction());
266  }
267  }
268 };
269 
270 }
271 #endif
static config_cache & instance()
Get reference to the singleton object.
void write_file(std::string file, const config &cfg)
void read_configs(const std::string &path, config &cfg, preproc_map &defines)
void read_file(const std::string &file, config &cfg)
void lock()
Lock the transaction so no more macros are added.
static config_cache_transaction * active_
void remove_define(const std::string &define)
Remove a entry to preproc defines map.
Holds a fake cache transaction if no real one is used.
std::vector< std::string > define_filenames_
static config_cache_transaction & instance()
const std::vector< std::string > & get_define_files() const
std::map< std::string, preproc_define > preproc_map
Used to set and unset scoped defines to preproc_map.
GLsizei const char ** path
Definition: glew.h:4654
bool delete_cache_files(const std::vector< std::string > &paths, const std::string &exclude_pattern="")
bool clean_cache()
Deletes stale cache files not in use by the game.
void read_defines_file(const std::string &path)
const preproc_map & get_preproc_map() const
void get_config(const std::string &path, config &cfg)
Gets a config object from given path.
void add_define_file(const std::string &file)
void set_force_valid_cache(bool force)
Enable/disable cache validation.
void set_use_cache(bool use)
Enable/disable caching.
void load_configs(const std::string &path, config &cfg)
void clear_defines()
Clear stored defines map to default values.
void add_defines_map_diff(preproc_map &)
Game configuration data as global variables.
Definition: build_info.cpp:38
preproc_map & get_active_map(const preproc_map &defines_map)
scoped_preproc_define_internal< config_cache > scoped_preproc_define
void read_cache(const std::string &path, config &cfg)
boost::scoped_ptr< config_cache_transaction > value_type
GLuint const GLchar * name
Definition: glew.h:1782
Used to share macros between cache objects You have to create transaction object to load all macros t...
scoped_preproc_define_internal(const std::string &name, bool add=true)
Adds normal preproc define.
void add_defines_map_diff(preproc_map &defines_map)
void insert_to_active(const preproc_map::value_type &def)
Used to let std::for_each insert new defines to active_map map to active.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
preproc_map & make_copy_map()
~scoped_preproc_define_internal()
This removes preproc define from cacher.
void add_define(const std::string &define)
Add a entry to preproc defines map.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void recheck_filetree_checksum()
Force cache checksum validation.
bool purge_cache()
Deletes all cache files.
Singleton class to manage game config file caching.