The Battle for Wesnoth  1.15.12+dev
context.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://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  * Manages the availability of wesnoth callbacks to plug-ins while the
17  * application is context switching.
18  */
19 
20 #pragma once
21 
22 #include <functional>
23 
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 class config;
29 
31 
32 public:
33  typedef std::function<bool(config)> callback_function;
34  struct Reg { char const * name; callback_function func; };
35 
36  typedef std::function<config(config)> accessor_function;
37  struct aReg { char const * name; accessor_function func; };
38 
39  using reg_vec = std::vector<Reg>;
40  using areg_vec = std::vector<aReg>;
41 
42  plugins_context( const std::string & name );
43  plugins_context( const std::string & name, const reg_vec& callbacks, const areg_vec& accessors);
44 
45  void play_slice();
46 
47  void set_callback(const std::string & name, callback_function);
48  void set_callback(const std::string & name, std::function<void(config)> function, bool preserves_context);
49  std::size_t erase_callback(const std::string & name);
50  std::size_t clear_callbacks();
51 
52  void set_accessor(const std::string & name, accessor_function);
53  void set_accessor_string(const std::string & name, std::function<std::string(config)>); //helpers which create a config from a simple type
54  void set_accessor_int(const std::string & name, std::function<int(config)>);
55  std::size_t erase_accessor(const std::string & name);
56  std::size_t clear_accessors();
57 
58  friend class application_lua_kernel;
59 
60 private:
61  typedef std::map<std::string, callback_function > callback_list;
62  typedef std::map<std::string, accessor_function > accessor_list;
63 
64  void initialize(const reg_vec& callbacks, const areg_vec& accessors);
65 
66  callback_list callbacks_;
67  accessor_list accessors_;
68  std::string name_;
69 };
std::vector< Reg > reg_vec
Definition: context.hpp:39
void set_callback(const std::string &name, callback_function)
Definition: context.cpp:51
callback_list callbacks_
Definition: context.hpp:66
char const * name
Definition: context.hpp:37
std::size_t erase_accessor(const std::string &name)
Definition: context.cpp:84
std::size_t clear_accessors()
Definition: context.cpp:89
void set_accessor_int(const std::string &name, std::function< int(config)>)
Definition: context.cpp:78
std::size_t clear_callbacks()
Definition: context.cpp:61
std::map< std::string, accessor_function > accessor_list
Definition: context.hpp:62
accessor_list accessors_
Definition: context.hpp:67
std::function< config(config)> accessor_function
Definition: context.hpp:36
std::vector< aReg > areg_vec
Definition: context.hpp:40
std::size_t erase_callback(const std::string &name)
Definition: context.cpp:56
void initialize(const reg_vec &callbacks, const areg_vec &accessors)
Definition: context.cpp:37
void set_accessor(const std::string &name, accessor_function)
Definition: context.cpp:68
plugins_context(const std::string &name)
Definition: context.cpp:23
std::map< std::string, callback_function > callback_list
Definition: context.hpp:61
char const * name
Definition: context.hpp:34
callback_function func
Definition: context.hpp:34
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
std::string name_
Definition: context.hpp:68
void set_accessor_string(const std::string &name, std::function< std::string(config)>)
Definition: context.cpp:73
std::function< bool(config)> callback_function
Definition: context.hpp:33
void play_slice()
Definition: context.cpp:96