OpenClonk
C4PropertyPath.h
Go to the documentation of this file.
1 /*
2 * OpenClonk, http://www.openclonk.org
3 *
4 * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5 * Copyright (c) 2013, The OpenClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16 
17 /* Generation of scripts to access values in objects and effects */
18 
19 #ifndef INC_C4PropertyPath
20 #define INC_C4PropertyPath
21 
22 #include "C4Include.h" // needed for automoc
23 #include "script/C4Value.h"
24 
25 // Path to a property, like e.g. Object(123).foo.bar[456].baz
26 // Used to allow proper synchronization of property setting
28 {
29  // TODO: For now just storing the path. May want to keep the path info later to allow validation/updating of values
30  StdCopyStrBuf get_path, argument, set_path;
31  StdCopyStrBuf root;
32 
33 public:
34  enum PathType
35  {
36  PPT_Root = 0,
38  PPT_Index = 2,
43 
44 public:
46  C4PropertyPath(C4PropList *target);
47  C4PropertyPath(C4Effect *fx, C4Object *target_obj);
48  C4PropertyPath(const char *path) : get_path(path), root(path), get_path_type(PPT_Root), set_path_type(PPT_Root) {}
49  C4PropertyPath(const C4PropertyPath &parent, int32_t elem_index);
50  C4PropertyPath(const C4PropertyPath &parent, const char *child_property);
51  void SetSetPath(const C4PropertyPath &parent, const char *child_property, PathType path_type);
52  void Clear() { get_path.Clear(); set_path.Clear(); }
53  const char *GetGetPath() const { return get_path.getData(); }
54  const char *GetSetPath() const { return set_path ? set_path.getData() : get_path.getData(); }
55  const char *GetRoot() const { return root.getData(); } // Parent-most path (usually the object)
56  bool IsEmpty() const { return get_path.getLength() <= 0; }
57 
58  C4Value ResolveValue() const;
59  C4Value ResolveRoot() const;
60  void SetProperty(const char *set_string) const;
61  void SetProperty(const C4Value &to_val, const C4PropListStatic *ignore_reference_parent = nullptr) const;
62  void DoCall(const char *call_string) const; // Perform a script call where %s is replaced by the current path
63 
64  bool operator ==(const C4PropertyPath &v) const { return get_path == v.get_path; }
65 };
66 
67 // Collection of paths and their last values
69 {
70 public:
71  struct Entry
72  {
76 
77  Entry(const C4PropertyPath &path, const C4Value &value, const char *name)
78  : path(path), value(value), name(name) {}
79  };
80 
81 private:
82  std::vector<Entry> entries;
83  std::unordered_set<void *> checked_values;
84 
85  bool CollectPropList(C4PropList *p, const C4PropertyPath &path, C4PropertyName prop, const C4Value &val, const char *base_name);
86  void CollectPropLists(C4ValueArray *target, const C4PropertyPath &target_path, C4PropertyName prop, const C4Value &val, const char *base_name);
87  void CollectPropLists(C4PropList *target, const C4PropertyPath &target_path, C4PropertyName prop, const C4Value &val, const char *base_name);
88 
89 public:
90  // Reset
91  void Clear();
92 
93  // Iterates over all game objects and effects to collect proplists that have the given property set to the given value
94  void CollectPropLists(C4PropertyName prop, const C4Value &val);
95 
96  const std::vector<Entry> &GetEntries() const { return entries; }
97 };
98 
99 #endif // INC_C4PropertyPath
C4PropertyName
const std::vector< Entry > & GetEntries() const
void SetProperty(const char *set_string) const
C4Value ResolveValue() const
bool operator==(const C4PropertyPath &v) const
C4Value ResolveRoot() const
enum C4PropertyPath::PathType set_path_type
C4PropertyPath(const char *path)
const char * GetSetPath() const
enum C4PropertyPath::PathType get_path_type
const char * GetRoot() const
bool IsEmpty() const
const char * GetGetPath() const
void SetSetPath(const C4PropertyPath &parent, const char *child_property, PathType path_type)
void DoCall(const char *call_string) const
const char * getData() const
Definition: StdBuf.h:442
void Clear()
Definition: StdBuf.h:466
size_t getLength() const
Definition: StdBuf.h:445
C4PropertyPath path
C4Value value
StdCopyStrBuf name
Entry(const C4PropertyPath &path, const C4Value &value, const char *name)