File: script_set.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (159 lines) | stat: -rw-r--r-- 5,270 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

//=============================================================================
//
// Managed script object wrapping std::set<String> and unordered_set<String>.
//
// TODO: support wrapping non-owned Set, passed by the reference, -
// that would let expose internal engine's sets using same interface.
// TODO: maybe optimize key lookup operations further by not creating a String
// object from const char*. It seems, C++14 standard allows to use convertible
// types as keys; need to research what performance impact that would make.
//
//=============================================================================

#ifndef AGS_ENGINE_AC_DYNOBJ_SCRIPTSET_H
#define AGS_ENGINE_AC_DYNOBJ_SCRIPTSET_H

#include "common/std/set.h"
#include "common/std/unordered_set.h"
#include "ags/engine/ac/dynobj/cc_ags_dynamic_object.h"
#include "ags/shared/util/stream.h"
#include "ags/shared/util/string.h"
#include "ags/shared/util/string_types.h"

namespace AGS3 {

using namespace AGS::Shared;

class ScriptSetBase : public AGSCCDynamicObject {
public:
	int Dispose(void *address, bool force) override;
	const char *GetType() override;
	void Unserialize(int index, AGS::Shared::Stream *in, size_t data_sz) override;

	virtual bool IsCaseSensitive() const = 0;
	virtual bool IsSorted() const = 0;

	virtual bool Add(const char *item) = 0;
	virtual void Clear() = 0;
	virtual bool Contains(const char *item) const = 0;
	virtual bool Remove(const char *item) = 0;
	virtual int GetItemCount() const = 0;
	virtual void GetItems(std::vector<const char *> &buf) const = 0;

protected:
	// Calculate and return required space for serialization, in bytes
	virtual size_t CalcSerializeSize(const void *address) override;
	// Write object data into the provided stream
	void Serialize(const void *address, AGS::Shared::Stream *out) override;

private:
	virtual size_t CalcContainerSize() = 0;
	virtual void SerializeContainer(AGS::Shared::Stream *out) = 0;
	virtual void UnserializeContainer(AGS::Shared::Stream *in) = 0;
};

template <typename TSet, bool is_sorted, bool is_casesensitive>
class ScriptSetImpl final : public ScriptSetBase {
public:
	typedef typename TSet::const_iterator ConstIterator;

	ScriptSetImpl() {}

	bool IsCaseSensitive() const override {
		return is_casesensitive;
	}
	bool IsSorted() const override {
		return is_sorted;
	}

	bool Add(const char *item) override {
		if (!item) return false;
		return TryAddItem(String(item));
	}
	void Clear() override {
		for (auto it = _set.begin(); it != _set.end(); ++it)
			DeleteItem(it);
		_set.clear();
	}
	bool Contains(const char *item) const override {
		return _set.count(String::Wrapper(item)) != 0;
	}
	bool Remove(const char *item) override {
		auto it = _set.find(String::Wrapper(item));
		if (it == _set.end()) return false;
		DeleteItem(it);
		_set.erase(it);
		return true;
	}
	int GetItemCount() const override {
		return _set.size();
	}
	void GetItems(std::vector<const char *> &buf) const override {
		for (auto it = _set.begin(); it != _set.end(); ++it)
			buf.push_back(it->GetCStr());
	}

private:
	bool TryAddItem(const String &s) {
		return _set.insert(s)._value;
	}
	void DeleteItem(ConstIterator /*it*/) { /* do nothing */ }

	size_t CalcContainerSize() override {
		// 2 class properties + item count
		size_t total_sz = sizeof(int32_t) * 3;
		// (int32 + string buffer) per item
		for (auto it = _set.begin(); it != _set.end(); ++it)
			total_sz += sizeof(int32_t) + it->GetLength();
		return total_sz;
	}

	void SerializeContainer(AGS::Shared::Stream *out) override {
		out->WriteInt32((int)_set.size());
		for (auto it = _set.begin(); it != _set.end(); ++it) {
			out->WriteInt32((int)it->GetLength());
			out->Write(it->GetCStr(), it->GetLength());
		}
	}

	void UnserializeContainer(AGS::Shared::Stream *in) override {
		size_t item_count = in->ReadInt32();
		for (size_t i = 0; i < item_count; ++i) {
			size_t len = in->ReadInt32();
			String item = String::FromStreamCount(in, len);
			TryAddItem(item);
		}
	}

	TSet _set;
};

typedef ScriptSetImpl< std::set<String>, true, true > ScriptSet;
typedef ScriptSetImpl< std::set<String, IgnoreCase_LessThan>, true, false > ScriptSetCI;
typedef ScriptSetImpl< std::unordered_set<String>, false, true > ScriptHashSet;
typedef ScriptSetImpl< std::unordered_set<String, IgnoreCase_Hash, IgnoreCase_EqualTo>, false, false > ScriptHashSetCI;

} // namespace AGS3

#endif