SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
IFilter.h
Go to the documentation of this file.
1 
28 #pragma once
29 
30 #include <list>
31 #include <vector>
32 #include "ICharacter.h"
33 #include "AI.h"
34 #include "common/MemoryAllocator.h"
35 #include "common/Thread.h"
36 
37 namespace ai {
38 
42 #define FILTER_CLASS(FilterName) \
43  explicit FilterName(const std::string& parameters = "") : \
44  ::ai::IFilter(#FilterName, parameters) { \
45  } \
46 public: \
47  virtual ~FilterName() { \
48  }
49 
50 #define FILTER_FACTORY(FilterName) \
51 public: \
52  class Factory: public ::ai::IFilterFactory { \
53  public: \
54  ::ai::FilterPtr create (const ::ai::FilterFactoryContext *ctx) const override { \
55  return std::make_shared<FilterName>(ctx->parameters); \
56  } \
57  }; \
58  static const Factory& getFactory() { \
59  static Factory FACTORY; \
60  return FACTORY; \
61  }
62 
63 #define FILTER_ACTION_CLASS(FilterName) \
64  FilterName(const std::string& parameters, const ::ai::Filters& filters) : \
65  ::ai::IFilter(#FilterName, parameters), _filters(filters) { \
66  ai_assert(_filters.size() > 1, #FilterName " must contain at least two sub filters"); \
67  } \
68 protected: \
69  const ::ai::Filters _filters; \
70 public: \
71  virtual ~FilterName() { \
72  }
73 
74 #define FILTER_ACTION_FACTORY(FilterName) \
75 public: \
76  class Factory: public ::ai::IFilterFactory { \
77  public: \
78  ::ai::FilterPtr create (const ::ai::FilterFactoryContext *ctx) const override { \
79  return std::make_shared<FilterName>(ctx->parameters, ctx->filters); \
80  } \
81  }; \
82  static const Factory& getFactory() { \
83  static Factory FACTORY; \
84  return FACTORY; \
85  }
86 
87 #define FILTER_FACTORY_SINGLETON \
88 public: \
89  class Factory: public ::ai::IFilterFactory { \
90  ::ai::FilterPtr create (const ::ai::FilterFactoryContext */*ctx*/) const override { \
91  return get(); \
92  } \
93  }; \
94  static const Factory& getFactory() { \
95  static Factory FACTORY; \
96  return FACTORY; \
97  }
98 
102 #define FILTER_CLASS_SINGLETON(FilterName) \
103 private: \
104 FILTER_CLASS(FilterName) \
105 public: \
106  static ::ai::FilterPtr& get() { \
107  AI_THREAD_LOCAL FilterName* c = nullptr; \
108  if (c == nullptr) { c = new FilterName; } \
109  AI_THREAD_LOCAL ::ai::FilterPtr _instance(c); \
110  return _instance; \
111  } \
112  FILTER_FACTORY_SINGLETON
113 
130 class IFilter : public MemObject {
131 protected:
132  const std::string _name;
133  const std::string _parameters;
134 
141  inline FilteredEntities& getFilteredEntities(const AIPtr& ai) {
142  return ai->_filteredEntities;
143  }
144 public:
145  IFilter (const std::string& name, const std::string& parameters) :
146  _name(name), _parameters(parameters) {
147  }
148 
149  virtual ~IFilter () {
150  }
151 
152  inline const std::string& getName() const {
153  return _name;
154  }
155 
156  inline const std::string& getParameters() const {
157  return _parameters;
158  }
159 
160  virtual void filter (const AIPtr& entity) = 0;
161 };
162 
163 }
164