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