Atoms Crowd  7.0.0
Blackboard.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <Atoms/Globals.h>
11 #include <AtomsCore/Metadata/Metadata.h>
12 #include <AtomsCore/Metadata/MapMetadata.h>
13 #include <AtomsCore/Metadata/StringMetadata.h>
14 #include <Atoms/BehaviourTree/BlackboardValue.h>
15 #include <map>
16 
17 namespace Atoms
18 {
20  {
21  enum Flags {
22  None = 0,
23  LinkMetadata = 1,
24  CacheMetadata = 2
25  };
26 
27  std::string name;
28  AtomsPtr<AtomsCore::Metadata> value;
29  short flags;
30  };
31 
32  class BlackboardInstance;
33 
34  class ATOMS_EXPORT Blackboard
35  {
36  public:
37 
38  Blackboard();
39 
40  ~Blackboard();
41 
42  BlackboardEntry& entry(size_t index);
43 
44  size_t addEntry(const std::string& name, AtomsPtr<AtomsCore::Metadata>& metadata, short flags = 0);
45 
46  void updateEntry(const std::string& name, AtomsPtr<AtomsCore::Metadata>& metadata);
47 
48  size_t addEntry(const std::string& name, AtomsCore::Metadata* metadata, short flags = 0);
49 
50  inline size_t size() const { return m_entries.size(); }
51 
52  inline void setParent(Blackboard* parent) { m_parent = parent; }
53 
54  Blackboard* parent() { return m_parent; }
55  const Blackboard* parent() const { return m_parent; }
56 
57  BlackboardEntryPosition entryPosition(const std::string& name) const;
58 
59  unsigned int entryType(const std::string& name) const;
60 
61  std::string entryTypeStr(const std::string& name) const;
62 
63  void initializeInstance(BlackboardInstance& instance, bool clone=true);
64 
65  void clear();
66 
67  private:
68 
69  std::vector<BlackboardEntry> m_entries;
70 
71  Blackboard* m_parent;
72 
73  };
74 
75  class ATOMS_EXPORT BlackboardInstance
76  {
77  public:
79 
81 
82  inline size_t size() const { return values.size(); }
83 
84  AtomsPtr<AtomsCore::Metadata>& entry(size_t index) { assert(values.size() > index); return values[index]; }
85  const AtomsPtr<AtomsCore::Metadata>& entry(size_t index) const { assert(values.size() > index); return values[index]; }
86 
87  public:
88  std::vector<AtomsPtr<AtomsCore::Metadata>> values;
89 
90  BlackboardInstance* parent;
91 
92  Blackboard* blackboard;
93  };
94 
95 
96  template <typename T, typename M>
97  inline void getBlackboardValueFromAttributes(const AtomsCore::MapMetadata* attributes, Blackboard* blackboard, const std::string& attributeName, BlackboardValue<M>& blackboardValue)
98  {
99  blackboardValue.position.valueIndex = -1;
100  blackboardValue.position.parentIndex = 0;
101  auto attrPtr = attributes->getTypedEntry<T>(attributeName);
102  if (attrPtr)
103  {
104  blackboardValue.value = attrPtr->value();
105  }
106 
107  auto properties = attributes->getTypedEntry<AtomsCore::MapMetadata>(attributeName + "#properties");
108  if (properties && blackboard)
109  {
110  auto blackboardKey = properties->getTypedEntry<AtomsCore::StringMetadata>("blackboard_key");
111  if (blackboardKey && !blackboardKey->get().empty())
112  {
113  auto bbPosition = blackboard->entryPosition(blackboardKey->get());
114  blackboardValue.position = bbPosition;
115  }
116  }
117  }
118 
119  inline void getBlackboardKeyPositionFromAttributes(const AtomsCore::MapMetadata* attributes, Blackboard* blackboard, const std::string& attributeName, BlackboardValue<std::string>& blackboardValue)
120  {
121  blackboardValue.position.valueIndex = -1;
122  blackboardValue.position.parentIndex = 0;
123  auto attrPtr = attributes->getTypedEntry<AtomsCore::StringMetadata>(attributeName);
124  if (attrPtr)
125  {
126  auto bbPosition = blackboard->entryPosition(attrPtr->get());
127  blackboardValue.position = bbPosition;
128  }
129 
130  auto properties = attributes->getTypedEntry<AtomsCore::MapMetadata>(attributeName + "#properties");
131  if (properties && blackboard)
132  {
133  auto blackboardKey = properties->getTypedEntry<AtomsCore::StringMetadata>("blackboard_key");
134  if (blackboardKey && !blackboardKey->get().empty())
135  {
136  auto bbPosition = blackboard->entryPosition(blackboardKey->get());
137  blackboardValue.position = bbPosition;
138  }
139  }
140  }
141 
142  template <>
143  inline void getBlackboardValueFromAttributes<AtomsCore::Metadata, AtomsPtr<AtomsCore::Metadata>>(const AtomsCore::MapMetadata* attributes, Blackboard* blackboard, const std::string& attributeName, BlackboardMetadataValue& blackboardValue)
144  {
145  blackboardValue.position.valueIndex = -1;
146  blackboardValue.position.parentIndex = 0;
147  auto attrPtr = attributes->getEntry(attributeName);
148  if (attrPtr)
149  {
150  blackboardValue.value = attrPtr->clone();
151  }
152 
153  auto properties = attributes->getTypedEntry<AtomsCore::MapMetadata>(attributeName + "#properties");
154  if (properties && blackboard)
155  {
156  auto blackboardKey = properties->getTypedEntry<AtomsCore::StringMetadata>("blackboard_key");
157  if (blackboardKey && !blackboardKey->get().empty())
158  {
159  auto bbPosition = blackboard->entryPosition(blackboardKey->get());
160  blackboardValue.position = bbPosition;
161  }
162  }
163  }
164 }
Definition: Blackboard.h:35
Definition: Blackboard.h:76
Definition: BlackboardValue.h:28
MapMetadata class.
Definition: MapMetadata.h:24
AtomsPtr< T > getTypedEntry(const Key &key)
Get a typed entry.
Definition: MapMetadata.impl.h:4
Base Metadata class.
Definition: Metadata.h:24
Atoms namespace.
Definition: Agent.h:29
Definition: Blackboard.h:20
Definition: BlackboardValue.h:21