The Battle for Wesnoth  1.15.2+dev
context.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 
18 
19 #include <cassert>
20 #include <utility>
21 #include "utils/functional.hpp"
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 std::vector<Reg>& l, const std::vector<aReg>& r)
30  : callbacks_()
31  , accessors_()
32  , name_(name)
33 {
34  initialize(l, r);
35 }
36 
37 void plugins_context::initialize(const std::vector<Reg>& callbacks, const std::vector<aReg>& 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 }
void set_callback(const std::string &name, callback_function)
Definition: context.cpp:51
callback_list callbacks_
Definition: context.hpp:66
std::size_t erase_accessor(const std::string &name)
Definition: context.cpp:84
std::size_t clear_accessors()
Definition: context.cpp:89
void set_accessor_int(const std::string &name, std::function< int(config)>)
Definition: context.cpp:78
std::size_t clear_callbacks()
Definition: context.cpp:61
std::map< std::string, accessor_function > accessor_list
Definition: context.hpp:62
accessor_list accessors_
Definition: context.hpp:67
void play_slice(const plugins_context &)
Definition: manager.cpp:158
std::function< config(config)> accessor_function
Definition: context.hpp:36
std::size_t erase_callback(const std::string &name)
Definition: context.cpp:56
void initialize(const reg_vec &callbacks, const areg_vec &accessors)
Definition: context.cpp:37
void set_accessor(const std::string &name, accessor_function)
Definition: context.cpp:68
plugins_context(const std::string &name)
Definition: context.cpp:23
std::map< std::string, callback_function > callback_list
Definition: context.hpp:61
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
std::string name_
Definition: context.hpp:68
void set_accessor_string(const std::string &name, std::function< std::string(config)>)
Definition: context.cpp:73
std::function< bool(config)> callback_function
Definition: context.hpp:33
static plugins_manager * get()
Definition: manager.cpp:58
void play_slice()
Definition: context.cpp:96