/* Copyright (C) 2014 - 2018 by Chris Beck Part of the Battle for Wesnoth Project https://www.wesnoth.org/ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. See the COPYING file for more details. */ /** * Manages the availability of wesnoth callbacks to plug-ins while the * application is context switching. */ #pragma once #include "utils/functional.hpp" #include #include #include class config; class plugins_context { public: typedef std::function callback_function; struct Reg { char const * name; callback_function func; }; typedef std::function accessor_function; struct aReg { char const * name; accessor_function func; }; plugins_context( const std::string & name ); plugins_context( const std::string & name, const std::vector& callbacks, const std::vector& accessors); template plugins_context( const std::string & name, const Reg (& callbacks)[N], const aReg (& accessors)[M]) : name_(name) { std::vector l; std::vector r; l.reserve(N); r.reserve(M); for(int i = 0; i < N; i++) { l.push_back(callbacks[i]); } for(int i = 0; i < M; i++) { r.push_back(accessors[i]); } initialize(l, r); } void play_slice(); void set_callback(const std::string & name, callback_function); void set_callback(const std::string & name, std::function function, bool preserves_context); size_t erase_callback(const std::string & name); size_t clear_callbacks(); void set_accessor(const std::string & name, accessor_function); void set_accessor_string(const std::string & name, std::function); //helpers which create a config from a simple type void set_accessor_int(const std::string & name, std::function); size_t erase_accessor(const std::string & name); size_t clear_accessors(); friend class application_lua_kernel; private: typedef std::map callback_list; typedef std::map accessor_list; void initialize(const std::vector& callbacks, const std::vector& accessors); callback_list callbacks_; accessor_list accessors_; std::string name_; };