The Battle for Wesnoth  1.15.0-dev
component.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
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  * @file
17  * A component of the AI framework
18  */
19 
20 #pragma once
21 
22 class config;
23 
24 #include <iostream>
25 #include <map>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 //============================================================================
31 namespace ai {
32 
33 //TODO: find a good place for this
34 struct path_element {
36  : property()
37  , id()
38  , position(0)
39  {
40  }
41 
42  std::string property;
43  std::string id;
44  int position;
45 };
46 
48 typedef std::shared_ptr<base_property_handler> property_handler_ptr;
49 typedef std::map<std::string,property_handler_ptr> property_handler_map;
50 
51 class component {
52 public:
54  : property_handlers_()
55  {
56  }
57 
58  virtual std::string get_id() const = 0;
59  virtual std::string get_name() const = 0;
60  virtual std::string get_engine() const = 0;
61  virtual ~component() {}
62  virtual component* get_child(const path_element &child);
63  virtual std::vector<component*> get_children(const std::string &type);
64  virtual std::vector<std::string> get_children_types();
65  virtual bool change_child(const path_element &child, const config &cfg);
66  virtual bool add_child(const path_element &child, const config &cfg);
67  virtual bool delete_child(const path_element &child);
68 
69  property_handler_map& property_handlers();
70 
71 private:
72  property_handler_map property_handlers_;
73 };
74 
76 public:
77  static bool add_component(component *root, const std::string &path, const config &cfg);
78  static bool change_component(component *root, const std::string &path, const config &cfg);
79  static bool delete_component(component *root, const std::string &path);
80  static std::string print_component_tree(component *root, const std::string &path);
81 
82  static component* get_component(component *root, const std::string &path);
83 };
84 
85 
86 } //end of namespace ai
87 
88 std::ostream &operator<<(std::ostream &o, const ai::path_element &e);
property_handler_map property_handlers_
Definition: component.hpp:72
std::ostream & operator<<(std::ostream &s, const ai::candidate_action &ca)
Definition: rca.cpp:123
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
std::string property
Definition: component.hpp:42
std::string id
Definition: component.hpp:43
const config & get_child(const std::string &key)
Definition: general.cpp:217
std::map< std::string, property_handler_ptr > property_handler_map
Definition: component.hpp:49
#define e
std::shared_ptr< base_property_handler > property_handler_ptr
Definition: component.hpp:47
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
virtual ~component()
Definition: component.hpp:61
std::string path
File path.
HOTKEY_COMMAND get_id(const std::string &command)
returns get_hotkey_command(command).id
static ai::engine_lua & get_engine(lua_State *L)
Definition: core.cpp:119