The Battle for Wesnoth  1.19.8+dev
context.cpp
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 
17 
20 
21 #include <cassert>
22 #include <functional>
23 #include <utility>
24 
25 plugins_context::plugins_context(const std::string & name)
26  : callbacks_()
27  , accessors_()
28  , name_(name)
29 {}
30 
31 plugins_context::plugins_context(const std::string& name, const reg_vec& l, const areg_vec& r)
32  : callbacks_()
33  , accessors_()
34  , name_(name)
35 {
36  initialize(l, r);
37 }
38 
39 void plugins_context::initialize(const reg_vec& callbacks, const areg_vec& accessors)
40 {
41  for (const Reg& l : callbacks) { /* fill the table with given functions */
42  if (l.name != nullptr) {
43  callbacks_.emplace(l.name, l.func);
44  }
45  }
46  for (const aReg& r : accessors) { /* fill the table with given functions */
47  if (r.name != nullptr) {
48  accessors_.emplace(r.name, r.func);
49  }
50  }
51 }
52 
53 void plugins_context::set_callback(const std::string & name, callback_function func)
54 {
55  callbacks_[name] = std::move(func);
56 }
57 
58 std::size_t plugins_context::erase_callback(const std::string & name)
59 {
60  return callbacks_.erase(name);
61 }
62 
64 {
65  std::size_t ret = callbacks_.size();
67  return ret;
68 }
69 
70 void plugins_context::set_accessor(const std::string & name, accessor_function func)
71 {
72  accessors_[name] = std::move(func);
73 }
74 
75 void plugins_context::set_accessor_string(const std::string & name, const std::function<std::string(config)>& func)
76 {
77  set_accessor(name, [func, name](const config& cfg) { return config {name, func(cfg)}; });
78 }
79 
80 void plugins_context::set_accessor_int(const std::string & name, const std::function<int(config)>& func)
81 {
82  set_accessor(name, [func, name](const config& cfg) { return config {name, func(cfg)}; });
83 }
84 
85 void plugins_context::set_accessor_bool(const std::string & name, const std::function<bool(config)>& func)
86 {
87  set_accessor(name, [func, name](const config& cfg) { return config {name, func(cfg)}; });
88 }
89 
90 
91 std::size_t plugins_context::erase_accessor(const std::string & name)
92 {
93  return accessors_.erase(name);
94 }
95 
97 {
98  std::size_t ret = accessors_.size();
100  return ret;
101 }
102 
104 {
105  assert(plugins_manager::get());
107 }
108 
109 void plugins_context::set_callback(const std::string & name, const std::function<void(config)>& func, bool preserves_context)
110 {
111  set_callback(name, [func, preserves_context](config cfg) { func(std::move(cfg)); return preserves_context; });
112 }
113 
115  execute_kernel_ = &kernel;
116 }
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::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
static plugins_manager * get()
Definition: manager.cpp:58
void play_slice(const plugins_context &)
Definition: manager.cpp:158