The Battle for Wesnoth  1.17.0-dev
context.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * Manages the availability of wesnoth callbacks to plug-ins while the
18  * application is context switching.
19  */
20 
21 #pragma once
22 
23 #include <functional>
24 
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 class config;
30 
32 
33 public:
34  typedef std::function<bool(config)> callback_function;
35  struct Reg { char const * name; callback_function func; };
36 
37  typedef std::function<config(config)> accessor_function;
38  struct aReg { char const * name; accessor_function func; };
39 
40  using reg_vec = std::vector<Reg>;
41  using areg_vec = std::vector<aReg>;
42 
43  plugins_context( const std::string & name );
44  plugins_context( const std::string & name, const reg_vec& callbacks, const areg_vec& accessors);
45 
46  void play_slice();
47 
48  void set_callback(const std::string & name, callback_function);
49  void set_callback(const std::string & name, std::function<void(config)> function, bool preserves_context);
50  std::size_t erase_callback(const std::string & name);
51  std::size_t clear_callbacks();
52 
53  void set_accessor(const std::string & name, accessor_function);
54  void set_accessor_string(const std::string & name, std::function<std::string(config)>); //helpers which create a config from a simple type
55  void set_accessor_int(const std::string & name, std::function<int(config)>);
56  std::size_t erase_accessor(const std::string & name);
57  std::size_t clear_accessors();
58 
59  friend class application_lua_kernel;
60 
61 private:
62  typedef std::map<std::string, callback_function > callback_list;
63  typedef std::map<std::string, accessor_function > accessor_list;
64 
65  void initialize(const reg_vec& callbacks, const areg_vec& accessors);
66 
67  callback_list callbacks_;
68  accessor_list accessors_;
69  std::string name_;
70 };
std::vector< Reg > reg_vec
Definition: context.hpp:40
void set_callback(const std::string &name, callback_function)
Definition: context.cpp:52
callback_list callbacks_
Definition: context.hpp:67
char const * name
Definition: context.hpp:38
std::size_t erase_accessor(const std::string &name)
Definition: context.cpp:85
std::size_t clear_accessors()
Definition: context.cpp:90
void set_accessor_int(const std::string &name, std::function< int(config)>)
Definition: context.cpp:79
std::size_t clear_callbacks()
Definition: context.cpp:62
std::map< std::string, accessor_function > accessor_list
Definition: context.hpp:63
accessor_list accessors_
Definition: context.hpp:68
std::function< config(config)> accessor_function
Definition: context.hpp:37
std::vector< aReg > areg_vec
Definition: context.hpp:41
std::size_t erase_callback(const std::string &name)
Definition: context.cpp:57
void initialize(const reg_vec &callbacks, const areg_vec &accessors)
Definition: context.cpp:38
void set_accessor(const std::string &name, accessor_function)
Definition: context.cpp:69
plugins_context(const std::string &name)
Definition: context.cpp:24
std::map< std::string, callback_function > callback_list
Definition: context.hpp:62
char const * name
Definition: context.hpp:35
callback_function func
Definition: context.hpp:35
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::string name_
Definition: context.hpp:69
void set_accessor_string(const std::string &name, std::function< std::string(config)>)
Definition: context.cpp:74
std::function< bool(config)> callback_function
Definition: context.hpp:34
void play_slice()
Definition: context.cpp:97