SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
ITreeLoader.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "common/Thread.h"
7 #include <memory>
8 #include <string>
9 #include <vector>
10 #include <map>
11 #include <cstdarg>
12 #include <cstdio>
13 
14 namespace ai {
15 
16 class IAIFactory;
17 class TreeNode;
18 typedef std::shared_ptr<TreeNode> TreeNodePtr;
19 
24 class ITreeLoader {
25 protected:
26  const IAIFactory& _aiFactory;
27  typedef std::map<std::string, TreeNodePtr> TreeMap;
28  TreeMap _treeMap;
29  ReadWriteLock _lock = {"treeloader"};
30 
31  inline void resetError() {
32  ScopedWriteLock scopedLock(_lock);
33  _error = "";
34  }
35 private:
36  std::string _error;
37 public:
38  explicit ITreeLoader(const IAIFactory& aiFactory) :
39  _aiFactory(aiFactory) {
40  }
41 
42  virtual ~ITreeLoader() {
43  _error = "";
44  _treeMap.clear();
45  }
46 
47  void shutdown() {
48  ScopedWriteLock scopedLock(_lock);
49  _error = "";
50  _treeMap.clear();
51  }
52 
53  inline const IAIFactory& getAIFactory() const {
54  return _aiFactory;
55  }
56 
60  void getTrees(std::vector<std::string>& trees) const {
61  ScopedReadLock scopedLock(_lock);
62  trees.reserve(_treeMap.size());
63  for (TreeMap::const_iterator it = _treeMap.begin(); it != _treeMap.end(); ++it) {
64  trees.push_back(it->first);
65  }
66  }
67 
77  bool addTree(const std::string& name, const TreeNodePtr& root) {
78  if (!root) {
79  return false;
80  }
81  {
82  ScopedReadLock scopedLock(_lock);
83  TreeMap::const_iterator i = _treeMap.find(name);
84  if (i != _treeMap.end()) {
85  return false;
86  }
87  }
88  {
89  ScopedWriteLock scopedLock(_lock);
90  _treeMap.insert(std::make_pair(name, root));
91  }
92  return true;
93  }
94 
98  TreeNodePtr load(const std::string &name) {
99  ScopedReadLock scopedLock(_lock);
100  TreeMap::const_iterator i = _treeMap.find(name);
101  if (i != _treeMap.end())
102  return i->second;
103  return TreeNodePtr();
104  }
105 
106  void setError(const char* msg, ...) __attribute__((format(printf, 2, 3)));
107 
111  inline std::string getError() const {
112  return _error;
113  }
114 };
115 
116 inline void ITreeLoader::setError(const char* msg, ...) {
117  va_list args;
118  va_start(args, msg);
119  char buf[1024];
120  std::vsnprintf(buf, sizeof(buf), msg, args);
121  va_end(args);
122  if (buf[0] != '\0') {
123  ai_log_debug("%s", buf);
124  }
125  ScopedWriteLock scopedLock(_lock);
126  _error = buf;
127 }
128 
129 }
void std::string getError() const
Gives access to the last error state of the ITreeLoader.
Definition: ITreeLoader.h:111
bool addTree(const std::string &name, const TreeNodePtr &root)
Register a new TreeNode as behaviour tree with the specified name.
Definition: ITreeLoader.h:77
#define ai_log_debug(...)
Logging macro to provide your own loggers.
Definition: Types.h:37
Definition: IAIFactory.h:39
Definition: Thread.h:44
This class must be extended to load behaviour trees. The contract here is that the parsing only happe...
Definition: ITreeLoader.h:24
void getTrees(std::vector< std::string > &trees) const
Fill the given vector with the loaded behaviour tree names.
Definition: ITreeLoader.h:60
Definition: Thread.h:56
Definition: Thread.h:14
TreeNodePtr load(const std::string &name)
Loads on particular behaviour tree.
Definition: ITreeLoader.h:98