The Battle for Wesnoth  1.19.8+dev
context.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
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, const std::function<void(config)>& function, bool preserves_context);
50  void set_callback_execute(class lua_kernel_base& kernel);
51  std::size_t erase_callback(const std::string & name);
52  std::size_t clear_callbacks();
53 
54  void set_accessor(const std::string & name, accessor_function);
55  void set_accessor_string(const std::string & name, const std::function<std::string(config)>&); //helpers which create a config from a simple type
56  void set_accessor_int(const std::string & name, const std::function<int(config)>&);
57  void set_accessor_bool(const std::string & name, const std::function<bool(config)>&);
58  std::size_t erase_accessor(const std::string & name);
59  std::size_t clear_accessors();
60 
61  friend class application_lua_kernel;
62 
63 private:
64  typedef std::map<std::string, callback_function > callback_list;
65  typedef std::map<std::string, accessor_function > accessor_list;
66 
67  void initialize(const reg_vec& callbacks, const areg_vec& accessors);
68 
71  std::string name_;
73 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
void play_slice()
Definition: context.cpp:103
std::vector< Reg > reg_vec
Definition: context.hpp:40
void set_accessor_int(const std::string &name, const std::function< int(config)> &)
Definition: context.cpp:80
std::function< bool(config)> callback_function
Definition: context.hpp:34
std::vector< aReg > areg_vec
Definition: context.hpp:41
std::map< std::string, accessor_function > accessor_list
Definition: context.hpp:65
void set_callback(const std::string &name, callback_function)
Definition: context.cpp:53
std::string name_
Definition: context.hpp:71
std::size_t erase_callback(const std::string &name)
Definition: context.cpp:58
std::size_t erase_accessor(const std::string &name)
Definition: context.cpp:91
void set_accessor(const std::string &name, accessor_function)
Definition: context.cpp:70
void set_accessor_string(const std::string &name, const std::function< std::string(config)> &)
Definition: context.cpp:75
void set_callback_execute(class lua_kernel_base &kernel)
Definition: context.cpp:114
std::size_t clear_callbacks()
Definition: context.cpp:63
plugins_context(const std::string &name)
Definition: context.cpp:25
void initialize(const reg_vec &callbacks, const areg_vec &accessors)
Definition: context.cpp:39
std::map< std::string, callback_function > callback_list
Definition: context.hpp:64
callback_list callbacks_
Definition: context.hpp:69
std::size_t clear_accessors()
Definition: context.cpp:96
lua_kernel_base * execute_kernel_
Definition: context.hpp:72
void set_accessor_bool(const std::string &name, const std::function< bool(config)> &)
Definition: context.cpp:85
accessor_list accessors_
Definition: context.hpp:70
std::function< config(config)> accessor_function
Definition: context.hpp:37
char const * name
Definition: context.hpp:35
callback_function func
Definition: context.hpp:35
char const * name
Definition: context.hpp:38
accessor_function func
Definition: context.hpp:38