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(const char *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  // Write object data into the provided stream
66  void Serialize(const char *address, AGS::Shared::Stream *out) override;
67 
68 private:
69  virtual void SerializeContainer(AGS::Shared::Stream *out) = 0;
70  virtual void UnserializeContainer(AGS::Shared::Stream *in) = 0;
71 };
72 
73 template <typename TSet, bool is_sorted, bool is_casesensitive>
74 class ScriptSetImpl final : public ScriptSetBase {
75 public:
76  typedef typename TSet::const_iterator ConstIterator;
77 
78  ScriptSetImpl() {}
79 
80  bool IsCaseSensitive() const override {
81  return is_casesensitive;
82  }
83  bool IsSorted() const override {
84  return is_sorted;
85  }
86 
87  bool Add(const char *item) override {
88  if (!item) return false;
89  return TryAddItem(String(item));
90  }
91  void Clear() override {
92  for (auto it = _set.begin(); it != _set.end(); ++it)
93  DeleteItem(it);
94  _set.clear();
95  }
96  bool Contains(const char *item) const override {
97  return _set.count(String::Wrapper(item)) != 0;
98  }
99  bool Remove(const char *item) override {
100  auto it = _set.find(String::Wrapper(item));
101  if (it == _set.end()) return false;
102  DeleteItem(it);
103  _set.erase(it);
104  return true;
105  }
106  int GetItemCount() const override {
107  return _set.size();
108  }
109  void GetItems(std::vector<const char *> &buf) const override {
110  for (auto it = _set.begin(); it != _set.end(); ++it)
111  buf.push_back(it->GetCStr());
112  }
113 
114 private:
115  bool TryAddItem(const String &s) {
116  return _set.insert(s)._value;
117  }
118  void DeleteItem(ConstIterator /*it*/) { /* do nothing */ }
119 
120  size_t CalcSerializeSize() override {
121  // 2 class properties + item count
122  size_t total_sz = sizeof(int32_t) * 3;
123  // (int32 + string buffer) per item
124  for (auto it = _set.begin(); it != _set.end(); ++it)
125  total_sz += sizeof(int32_t) + it->GetLength();
126  return total_sz;
127  }
128 
129  void SerializeContainer(AGS::Shared::Stream *out) override {
130  out->WriteInt32((int)_set.size());
131  for (auto it = _set.begin(); it != _set.end(); ++it) {
132  out->WriteInt32((int)it->GetLength());
133  out->Write(it->GetCStr(), it->GetLength());
134  }
135  }
136 
137  void UnserializeContainer(AGS::Shared::Stream *in) override {
138  size_t item_count = in->ReadInt32();
139  for (size_t i = 0; i < item_count; ++i) {
140  size_t len = in->ReadInt32();
141  String item = String::FromStreamCount(in, len);
142  TryAddItem(item);
143  }
144  }
145 
146  TSet _set;
147 };
148 
149 typedef ScriptSetImpl< std::set<String>, true, true > ScriptSet;
153 
154 } // namespace AGS3
155 
156 #endif
Definition: vector.h:39
void push_back(const T &element)
Definition: array.h:180
Definition: script_set.h:74
Definition: cc_ags_dynamic_object.h:31
Definition: string.h:62
Definition: script_set.h:48
Definition: stream.h:52
Definition: ags.h:40