The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
context.hpp
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 
15 /**
16  * Manages the availability of wesnoth callbacks to plug-ins while the
17  * application is context switching.
18  */
19 
20 #ifndef INCLUDED_PLUGINS_CONTEXT_HPP_
21 #define INCLUDED_PLUGINS_CONTEXT_HPP_
22 
23 #include "utils/functional.hpp"
24 
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 class config;
30 
32 
33 public:
34  typedef std::function<bool(config)> callback_function;
35  typedef struct { char const * name; callback_function func; } Reg;
36 
37  typedef std::function<config(config)> accessor_function;
38  typedef struct { char const * name; accessor_function func; } aReg;
39 
40  plugins_context( const std::string & name );
41  plugins_context( const std::string & name, const std::vector<Reg>& callbacks, const std::vector<aReg>& accessors);
42  template<int N, int M>
43  plugins_context( const std::string & name, const Reg (& callbacks)[N], const aReg (& accessors)[M])
44  : name_(name)
45  {
46  std::vector<Reg> l;
47  std::vector<aReg> r;
48  l.reserve(N);
49  r.reserve(M);
50  for(int i = 0; i < N; i++) {
51  l.push_back(callbacks[i]);
52  }
53  for(int i = 0; i < M; i++) {
54  r.push_back(accessors[i]);
55  }
56  initialize(l, r);
57  }
58 
59  void play_slice();
60 
61  void set_callback(const std::string & name, callback_function);
62  void set_callback(const std::string & name, std::function<void(config)> function, bool preserves_context);
63  size_t erase_callback(const std::string & name);
64  size_t clear_callbacks();
65 
66  void set_accessor(const std::string & name, accessor_function);
67  void set_accessor_string(const std::string & name, std::function<std::string(config)>); //helpers which create a config from a simple type
68  void set_accessor_int(const std::string & name, std::function<int(config)>);
69  size_t erase_accessor(const std::string & name);
70  size_t clear_accessors();
71 
72  friend class application_lua_kernel;
73 
74 private:
75  typedef std::map<std::string, callback_function > callback_list;
76  typedef std::map<std::string, accessor_function > accessor_list;
77 
78  void initialize(const std::vector<Reg>& callbacks, const std::vector<aReg>& accessors);
79 
80  callback_list callbacks_;
81  accessor_list accessors_;
83 };
84 
85 //A shim to assist in retrieving config attribute values
86 extern const std::function< std::string ( const config & , const std::string & ) > get_str;
87 extern const std::function< int ( const config & , const std::string &, int ) > get_int;
88 extern const std::function< size_t ( const config & , const std::string &, size_t ) > get_size_t;
89 
90 #endif
size_t clear_accessors()
Definition: context.cpp:97
void set_callback(const std::string &name, callback_function)
Definition: context.cpp:53
callback_list callbacks_
Definition: context.hpp:80
const std::function< std::string(const config &, const std::string &) > get_str
Definition: context.cpp:121
char const * name
Definition: context.hpp:38
size_t erase_accessor(const std::string &name)
Definition: context.cpp:92
GLdouble l
Definition: glew.h:6966
size_t erase_callback(const std::string &name)
Definition: context.cpp:58
const std::function< int(const config &, const std::string &, int) > get_int
Definition: context.cpp:125
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
plugins_context(const std::string &name, const Reg(&callbacks)[N], const aReg(&accessors)[M])
Definition: context.hpp:43
std::map< std::string, accessor_function > accessor_list
Definition: context.hpp:76
const std::function< size_t(const config &, const std::string &, size_t) > get_size_t
Definition: context.cpp:130
accessor_list accessors_
Definition: context.hpp:81
size_t i
Definition: function.cpp:1057
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
plugins_context(const std::string &name)
Definition: context.cpp:25
std::map< std::string, callback_function > callback_list
Definition: context.hpp:75
char const * name
Definition: context.hpp:35
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
std::string name_
Definition: context.hpp:82
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
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::function< bool(config)> callback_function
Definition: context.hpp:34
void play_slice()
Definition: context.cpp:104