Debian Codegraph @

plugins/dm.objectives/TargetList.h

raw
1 #pragma once
2
3 #include "ientity.h"
4
5 #include <set>
6 #include <string>
7 #include "string/predicate.h"
8
9 namespace objectives
10 {
11
12 /**
13 * A list of entities targeted from a given source entity. During construction,
14 * this class builds a set of target entities by enumerating the "target<x>"
15 * keys on the source entity. It then provides a method to check whether a given
16 * entity is targeted by the source entity.
17 */
18 class TargetList
19 {
20 // Source entity
21 const Entity* _src;
22
23 // The set of targeted entities, by name
24 typedef std::set<std::string> StringSet;
25 StringSet _set;
26
27 public:
28
29 /**
30 * Construct a TargetList with the given source entity. The source entity
31 * is immediately scanned for <b>target</b> keyvalues that refer to other
32 * entities.
33 *
34 * @param src
35 * The source Entity to query for targets.
36 */
37 TargetList(const Entity* src)
38 : _src(src)
39 {
40 assert(src);
41 _src->forEachKeyValue([&](const std::string& key, const std::string& value)
42 {
43 // If the key starts with "target", add the value to the set
44 if (string::istarts_with(key, "target"))
45 {
46 _set.insert(value);
47 }
48 });
49 }
50
51 /**
52 * Query whether the supplied entity is targeted by the source entity.
53 *
54 * @param qtarget
55 * The destination entity.
56 *
57 * @return
58 * true if the destination Entity appears in the list of targets for the
59 * source Entity, false otherwise.
60 */
61 bool isTargeted(const Entity* qtarget) const
62 {
63 assert(qtarget);
64
65 // Check if the queried entity's name is in the set
66 if (_set.find(qtarget->getKeyValue("name")) != _set.end())
67 return true;
68 else
69 return false;
70 }
71 };
72
73 } // namespace objectives