ScummVM API documentation
script_set.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 //=============================================================================
23 //
24 // Managed script object wrapping std::set<String> and unordered_set<String>.
25 //
26 // TODO: support wrapping non-owned Set, passed by the reference, -
27 // that would let expose internal engine's sets using same interface.
28 // TODO: maybe optimize key lookup operations further by not creating a String
29 // object from const char*. It seems, C++14 standard allows to use convertible
30 // types as keys; need to research what performance impact that would make.
31 //
32 //=============================================================================
33 
34 #ifndef AGS_ENGINE_AC_DYNOBJ_SCRIPTSET_H
35 #define AGS_ENGINE_AC_DYNOBJ_SCRIPTSET_H
36 
37 #include "common/std/set.h"
38 #include "common/std/unordered_set.h"
39 #include "ags/engine/ac/dynobj/cc_ags_dynamic_object.h"
40 #include "ags/shared/util/stream.h"
41 #include "ags/shared/util/string.h"
42 #include "ags/shared/util/string_types.h"
43 
44 namespace AGS3 {
45 
46 using namespace AGS::Shared;
47 
49 public:
50  int Dispose(void *address, bool force) override;
51  const char *GetType() override;
52  void Unserialize(int index, AGS::Shared::Stream *in, size_t data_sz) override;
53 
54  virtual bool IsCaseSensitive() const = 0;
55  virtual bool IsSorted() const = 0;
56 
57  virtual bool Add(const char *item) = 0;
58  virtual void Clear() = 0;
59  virtual bool Contains(const char *item) const = 0;
60  virtual bool Remove(const char *item) = 0;
61  virtual int GetItemCount() const = 0;
62  virtual void GetItems(std::vector<const char *> &buf) const = 0;
63 
64 protected:
65  // Calculate and return required space for serialization, in bytes
66  virtual size_t CalcSerializeSize(const void *address) override;
67  // Write object data into the provided stream
68  void Serialize(const void *address, AGS::Shared::Stream *out) override;
69 
70 private:
71  virtual size_t CalcContainerSize() = 0;
72  virtual void SerializeContainer(AGS::Shared::Stream *out) = 0;
73  virtual void UnserializeContainer(AGS::Shared::Stream *in) = 0;
74 };
75 
76 template <typename TSet, bool is_sorted, bool is_casesensitive>
77 class ScriptSetImpl final : public ScriptSetBase {
78 public:
79  typedef typename TSet::const_iterator ConstIterator;
80 
81  ScriptSetImpl() {}
82 
83  bool IsCaseSensitive() const override {
84  return is_casesensitive;
85  }
86  bool IsSorted() const override {
87  return is_sorted;
88  }
89 
90  bool Add(const char *item) override {
91  if (!item) return false;
92  return TryAddItem(String(item));
93  }
94  void Clear() override {
95  for (auto it = _set.begin(); it != _set.end(); ++it)
96  DeleteItem(it);
97  _set.clear();
98  }
99  bool Contains(const char *item) const override {
100  return _set.count(String::Wrapper(item)) != 0;
101  }
102  bool Remove(const char *item) override {
103  auto it = _set.find(String::Wrapper(item));
104  if (it == _set.end()) return false;
105  DeleteItem(it);
106  _set.erase(it);
107  return true;
108  }
109  int GetItemCount() const override {
110  return _set.size();
111  }
112  void GetItems(std::vector<const char *> &buf) const override {
113  for (auto it = _set.begin(); it != _set.end(); ++it)
114  buf.push_back(it->GetCStr());
115  }
116 
117 private:
118  bool TryAddItem(const String &s) {
119  return _set.insert(s)._value;
120  }
121  void DeleteItem(ConstIterator /*it*/) { /* do nothing */ }
122 
123  size_t CalcContainerSize() override {
124  // 2 class properties + item count
125  size_t total_sz = sizeof(int32_t) * 3;
126  // (int32 + string buffer) per item
127  for (auto it = _set.begin(); it != _set.end(); ++it)
128  total_sz += sizeof(int32_t) + it->GetLength();
129  return total_sz;
130  }
131 
132  void SerializeContainer(AGS::Shared::Stream *out) override {
133  out->WriteInt32((int)_set.size());
134  for (auto it = _set.begin(); it != _set.end(); ++it) {
135  out->WriteInt32((int)it->GetLength());
136  out->Write(it->GetCStr(), it->GetLength());
137  }
138  }
139 
140  void UnserializeContainer(AGS::Shared::Stream *in) override {
141  size_t item_count = in->ReadInt32();
142  for (size_t i = 0; i < item_count; ++i) {
143  size_t len = in->ReadInt32();
144  String item = String::FromStreamCount(in, len);
145  TryAddItem(item);
146  }
147  }
148 
149  TSet _set;
150 };
151 
152 typedef ScriptSetImpl< std::set<String>, true, true > ScriptSet;
156 
157 } // namespace AGS3
158 
159 #endif
Definition: vector.h:39
void push_back(const T &element)
Definition: array.h:180
Definition: script_set.h:77
Definition: cc_ags_dynamic_object.h:88
Definition: string.h:62
Definition: script_set.h:48
Definition: stream.h:52
Definition: ags.h:40