The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
context.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 "config_assign.hpp"
20 
21 #include <assert.h>
22 #include <utility>
23 #include "utils/functional.hpp"
24 
26  : callbacks_()
27  , accessors_()
28  , name_(name)
29 {}
30 
31 plugins_context::plugins_context(const std::string& name, const std::vector<Reg>& l, const std::vector<aReg>& r)
32  : callbacks_()
33  , accessors_()
34  , name_(name)
35 {
36  initialize(l, r);
37 }
38 
39 void plugins_context::initialize(const std::vector<Reg>& callbacks, const std::vector<aReg>& accessors)
40 {
41  for (const Reg& l : callbacks) { /* fill the table with given functions */
42  if (l.name != nullptr) {
43  callbacks_.insert(std::make_pair(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_.insert(std::make_pair(r.name, r.func));
49  }
50  }
51 }
52 
54 {
55  callbacks_[name] = func;
56 }
57 
59 {
60  return callbacks_.erase(name);
61 }
62 
64 {
65  size_t ret = callbacks_.size();
67  return ret;
68 }
69 
71 {
72  accessors_[name] = func;
73 }
74 
75 template<typename T>
76 static config make_config(const std::string & name, const T & val)
77 {
78  return config_of(name, val);
79 }
80 
82 {
83  set_accessor(name, std::bind(&make_config<std::string>, name, std::bind(func, _1)));
84 }
85 
86 void plugins_context::set_accessor_int(const std::string & name, std::function<int(config)> func)
87 {
88  set_accessor(name, std::bind(&make_config<int>, name, std::bind(func, _1)));
89 }
90 
91 
93 {
94  return accessors_.erase(name);
95 }
96 
98 {
99  size_t ret = accessors_.size();
101  return ret;
102 }
103 
105 {
106  assert(plugins_manager::get());
108 }
109 
110 static bool shim(config cfg, std::function<void(config)> func, bool return_value)
111 {
112  func(cfg);
113  return return_value;
114 }
115 
116 void plugins_context::set_callback(const std::string & name, std::function<void(config)> func, bool preserves_context)
117 {
118  set_callback(name, std::bind(shim, _1, func, preserves_context));
119 }
120 
121 const std::function< std::string ( const config & , const std::string & ) > get_str =
122  std::bind(&config::attribute_value::str,
123  std::bind(static_cast<const config::attribute_value &(config::*)(const std::string &) const>(&config::operator[]) , _1, _2));
124 
125 const std::function< int ( const config & , const std::string &, int ) > get_int =
127  std::bind(static_cast<const config::attribute_value &(config::*)(const std::string &) const>(&config::operator[]) , _1, _2)
128  , _3);
129 
130 const std::function< size_t ( const config & , const std::string &, size_t ) > get_size_t =
132  std::bind(static_cast<const config::attribute_value &(config::*)(const std::string &) const>(&config::operator[]) , _1, _2)
133  , _3);
const std::function< size_t(const config &, const std::string &, size_t) > get_size_t
Definition: context.cpp:130
size_t clear_accessors()
Definition: context.cpp:97
std::string str() const
Definition: config.cpp:353
void set_callback(const std::string &name, callback_function)
Definition: context.cpp:53
callback_list callbacks_
Definition: context.hpp:80
size_t to_size_t(size_t def=0) const
Definition: config.cpp:323
static bool shim(config cfg, std::function< void(config)> func, bool return_value)
Definition: context.cpp:110
const std::function< int(const config &, const std::string &, int) > get_int
Definition: context.cpp:125
GLuint const GLfloat * val
Definition: glew.h:2614
size_t erase_accessor(const std::string &name)
Definition: context.cpp:92
Variant for storing WML attributes.
Definition: config.hpp:223
GLdouble l
Definition: glew.h:6966
size_t erase_callback(const std::string &name)
Definition: context.cpp:58
void set_accessor_int(const std::string &name, std::function< int(config)>)
Definition: context.cpp:86
void initialize(const std::vector< Reg > &callbacks, const std::vector< aReg > &accessors)
Definition: context.cpp:39
std::map< std::string, accessor_function > accessor_list
Definition: context.hpp:76
accessor_list accessors_
Definition: context.hpp:81
void play_slice(const plugins_context &)
Definition: manager.cpp:161
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
std::function< config(config)> accessor_function
Definition: context.hpp:37
GLuint const GLchar * name
Definition: glew.h:1782
void set_accessor(const std::string &name, accessor_function)
Definition: context.cpp:70
const std::function< std::string(const config &, const std::string &) > get_str
Definition: context.cpp:121
int to_int(int def=0) const
Definition: config.cpp:308
plugins_context(const std::string &name)
Definition: context.cpp:25
std::map< std::string, callback_function > callback_list
Definition: context.hpp:75
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
size_t clear_callbacks()
Definition: context.cpp:63
void set_accessor_string(const std::string &name, std::function< std::string(config)>)
Definition: context.cpp:81
static config make_config(const std::string &name, const T &val)
Definition: context.cpp:76
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::function< bool(config)> callback_function
Definition: context.hpp:34
static plugins_manager * get()
Definition: manager.cpp:61
void play_slice()
Definition: context.cpp:104