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 
15 #include "scripting/plugins/context.hpp"
16 
17 #include "scripting/plugins/manager.hpp"
18 
19 #include <cassert>
20 #include <utility>
21 #include "utils/functional.hpp"
22 
plugins_context(const std::string & name)23 plugins_context::plugins_context(const std::string & name)
24 	: callbacks_()
25 	, accessors_()
26 	, name_(name)
27 {}
28 
plugins_context(const std::string & name,const std::vector<Reg> & l,const std::vector<aReg> & r)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 
initialize(const std::vector<Reg> & callbacks,const std::vector<aReg> & accessors)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 
set_callback(const std::string & name,callback_function func)51 void plugins_context::set_callback(const std::string & name, callback_function func)
52 {
53 	callbacks_[name] = func;
54 }
55 
erase_callback(const std::string & name)56 size_t plugins_context::erase_callback(const std::string & name)
57 {
58 	return callbacks_.erase(name);
59 }
60 
clear_callbacks()61 size_t plugins_context::clear_callbacks()
62 {
63 	size_t ret = callbacks_.size();
64 	callbacks_ = callback_list();
65 	return ret;
66 }
67 
set_accessor(const std::string & name,accessor_function func)68 void plugins_context::set_accessor(const std::string & name, accessor_function func)
69 {
70 	accessors_[name] = func;
71 }
72 
set_accessor_string(const std::string & name,std::function<std::string (config)> func)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 
set_accessor_int(const std::string & name,std::function<int (config)> func)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 
erase_accessor(const std::string & name)84 size_t plugins_context::erase_accessor(const std::string & name)
85 {
86 	return accessors_.erase(name);
87 }
88 
clear_accessors()89 size_t plugins_context::clear_accessors()
90 {
91 	size_t ret = accessors_.size();
92 	accessors_ = accessor_list();
93 	return ret;
94 }
95 
play_slice()96 void plugins_context::play_slice()
97 {
98 	assert(plugins_manager::get());
99 	plugins_manager::get()->play_slice(*this);
100 }
101 
set_callback(const std::string & name,std::function<void (config)> func,bool preserves_context)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 }
106