The Battle for Wesnoth  1.19.5+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 
19 
20 #include <cassert>
21 #include <functional>
22 
23 plugins_context::plugins_context(const std::string & name)
24  : callbacks_()
25  , accessors_()
26  , name_(name)
27 {}
28 
29 plugins_context::plugins_context(const std::string& name, const reg_vec& l, const areg_vec& r)
30  : callbacks_()
31  , accessors_()
32  , name_(name)
33 {
34  initialize(l, r);
35 }
36 
37 void plugins_context::initialize(const reg_vec& callbacks, const areg_vec& accessors)
38 {
39  for (const Reg& l : callbacks) { /* fill the table with given functions */
40  if (l.name != nullptr) {
41  callbacks_.emplace(l.name, l.func);
42  }
43  }
44  for (const aReg& r : accessors) { /* fill the table with given functions */
45  if (r.name != nullptr) {
46  accessors_.emplace(r.name, r.func);
47  }
48  }
49 }
50 
51 void plugins_context::set_callback(const std::string & name, callback_function func)
52 {
53  callbacks_[name] = func;
54 }
55 
56 std::size_t plugins_context::erase_callback(const std::string & name)
57 {
58  return callbacks_.erase(name);
59 }
60 
62 {
63  std::size_t ret = callbacks_.size();
65  return ret;
66 }
67 
68 void plugins_context::set_accessor(const std::string & name, accessor_function func)
69 {
70  accessors_[name] = func;
71 }
72 
73 void plugins_context::set_accessor_string(const std::string & name, std::function<std::string(config)> func)
74 {
75  set_accessor(name, [func, name](const config& cfg) { return config {name, func(cfg)}; });
76 }
77 
78 void plugins_context::set_accessor_int(const std::string & name, std::function<int(config)> func)
79 {
80  set_accessor(name, [func, name](const config& cfg) { return config {name, func(cfg)}; });
81 }
82 
83 
84 std::size_t plugins_context::erase_accessor(const std::string & name)
85 {
86  return accessors_.erase(name);
87 }
88 
90 {
91  std::size_t ret = accessors_.size();
93  return ret;
94 }
95 
97 {
98  assert(plugins_manager::get());
100 }
101 
102 void plugins_context::set_callback(const std::string & name, std::function<void(config)> func, bool preserves_context)
103 {
104  set_callback(name, [func, preserves_context](config cfg) { func(cfg); return preserves_context; });
105 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
void play_slice()
Definition: context.cpp:96
std::vector< Reg > reg_vec
Definition: context.hpp:40
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:63
void set_callback(const std::string &name, callback_function)
Definition: context.cpp:51
std::size_t erase_callback(const std::string &name)
Definition: context.cpp:56
std::size_t erase_accessor(const std::string &name)
Definition: context.cpp:84
void set_accessor_int(const std::string &name, std::function< int(config)>)
Definition: context.cpp:78
void set_accessor(const std::string &name, accessor_function)
Definition: context.cpp:68
std::size_t clear_callbacks()
Definition: context.cpp:61
plugins_context(const std::string &name)
Definition: context.cpp:23
void initialize(const reg_vec &callbacks, const areg_vec &accessors)
Definition: context.cpp:37
std::map< std::string, callback_function > callback_list
Definition: context.hpp:62
callback_list callbacks_
Definition: context.hpp:67
std::size_t clear_accessors()
Definition: context.cpp:89
accessor_list accessors_
Definition: context.hpp:68
std::function< config(config)> accessor_function
Definition: context.hpp:37
void set_accessor_string(const std::string &name, std::function< std::string(config)>)
Definition: context.cpp:73
static plugins_manager * get()
Definition: manager.cpp:58
void play_slice(const plugins_context &)
Definition: manager.cpp:158