Qore Programming Language 2.1.1
Loading...
Searching...
No Matches
QoreSerializable.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreSerializable.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2024 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_CLASS_INTERN_QORESERIALIZABLE_H
33
34#define _QORE_CLASS_INTERN_QORESERIALIZABLE_H
35
36#include <qore/Qore.h>
37#include "qore/intern/StreamReader.h"
38#include "qore/intern/StreamWriter.h"
39
40#include <map>
41#include <string>
42#include <set>
43
44// maps from index strings to containers
45typedef std::map<std::string, QoreValue> oimap_t;
46
47// maps from object hashes to index strings
48typedef std::map<std::string, std::string> imap_t;
49
50// set of modules to load
51typedef std::set<std::string> mset_t;
52
53namespace {
54// qore serialization stream data type constants
55enum qore_stream_type : unsigned char {
56 HASH = 0,
57 HASHDECL = 1,
58 STRING = 2,
59 UTF8_STRING = 3,
60 LIST = 4,
61 BOOLEAN_TRUE = 5,
62 BOOLEAN_FALSE = 6,
63 INT1 = 7,
64 INT2 = 8,
65 INT4 = 9,
66 INT8 = 10,
67 FLOAT = 11,
68 QORENUMBER = 12,
69 QOREBINARY = 13,
70 ABSDATE = 14,
71 RELDATE = 15,
72 SQLNULL = 16,
73 NOTHING = 17,
74};
75}
76
77class QoreInternalSerializationContext {
78public:
81 imap_t imap;
82 mset_t mset;
83
84 // set of classes to serialize static vars
85 typedef std::set<const QoreClass*> cset_t;
86 cset_t cset;
87
88 int64 flags;
89
90 DLLLOCAL QoreInternalSerializationContext(ExceptionSink* xsink, int64 flags) : flags(flags), index(xsink),
91 svars(xsink) {
92 }
93
94 DLLLOCAL int serializeObject(const QoreObject& obj, std::string& index_str, ExceptionSink* xsink);
95
96 DLLLOCAL int serializeHash(const QoreHashNode& h, std::string& index_str, ExceptionSink* xsink);
97
98 DLLLOCAL int serializeList(const QoreListNode& l, std::string& index_str, ExceptionSink* xsink);
99
100 DLLLOCAL QoreValue serializeValue(const QoreValue val, ExceptionSink* xsink);
101
102 DLLLOCAL void addModule(const char* module) {
103 mset.insert(module);
104 }
105
106 DLLLOCAL void setIndex(const char* str, QoreValue v, ExceptionSink* xsink) {
107 if (!index) {
108 index = new QoreHashNode(autoTypeInfo);
109 }
110 index->setKeyValue(str, v, xsink);
111 }
112
113 DLLLOCAL void setSVars(const char* str, QoreValue v, ExceptionSink* xsink) {
114 if (!svars) {
115 svars = new QoreHashNode(autoTypeInfo);
116 }
117 svars->setKeyValue(str, v, xsink);
118 }
119
120 DLLLOCAL int serializeStaticVars(const QoreClass& cls, ExceptionSink* xsink);
121};
122
123class ObjectIndexMap : public oimap_t {
124public:
125 DLLLOCAL ObjectIndexMap(ExceptionSink* xs) : xs(xs) {
126 }
127
128 DLLLOCAL ~ObjectIndexMap();
129
130protected:
131 ExceptionSink* xs;
132};
133
134class QoreInternalDeserializationContext {
135public:
136 ObjectIndexMap oimap;
137 int64 flags;
138
139 DLLLOCAL QoreInternalDeserializationContext(ExceptionSink* xsink, int64 flags) : oimap(xsink), flags(flags) {
140 }
141
142 DLLLOCAL QoreValue deserializeContainer(const char* index_str, ExceptionSink* xsink);
143
144 DLLLOCAL QoreValue deserializeValue(const QoreValue val, ExceptionSink* xsink);
145};
146
147class QoreSerializable : public AbstractPrivateData {
148friend class QoreInternalSerializationContext;
149friend class QoreInternalDeserializationContext;
150
151public:
152 DLLLOCAL static QoreHashNode* serializeToData(QoreValue val, int64 flags, ExceptionSink* xsink);
153
154 DLLLOCAL static void serialize(const QoreObject& self, OutputStream& stream, int64 flags, ExceptionSink* xsink);
155
156 DLLLOCAL static void serialize(const QoreValue val, OutputStream& stream, int64 flags, ExceptionSink* xsink);
157
158 DLLLOCAL static QoreHashNode* deserializeToData(ExceptionSink* xsink, InputStream& stream, int64 flags = 0);
159
160 DLLLOCAL static QoreValue deserialize(ExceptionSink* xsink, InputStream& stream, int64 flags = 0);
161
162 DLLLOCAL static QoreValue deserialize(ExceptionSink* xsink, const QoreHashNode& h, int64 flags = 0);
163
164 DLLLOCAL static int serializeValueToStream(const QoreValue val, OutputStream& stream, ExceptionSink* xsink);
165
166 // note: does not serialiaze the stream header; just the value
167 DLLLOCAL static int serializeValueToStream(const QoreValue val, StreamWriter& writer, ExceptionSink* xsink);
168
169 // note: reads a raw value; assumes the stream header has already been read
170 DLLLOCAL static QoreValue deserializeValueFromStream(ExceptionSink* xsink, StreamReader& reader, int64 flags = 0);
171
172protected:
173 DLLLOCAL virtual ~QoreSerializable() {}
174
175 DLLLOCAL static QoreValue serializeObjectToData(const QoreObject& obj, bool weak,
176 QoreInternalSerializationContext& context, ExceptionSink* xsink);
177
178 DLLLOCAL static imap_t::iterator serializeObjectToIndex(const QoreObject& obj,
179 QoreInternalSerializationContext& context, ExceptionSink* xsink);
180
181 DLLLOCAL static imap_t::iterator serializeObjectToIndexIntern(const QoreObject& self,
182 QoreInternalSerializationContext& context, const QoreString& str,
183 imap_t::iterator hint, ExceptionSink* xsink);
184
185 DLLLOCAL static QoreValue serializeValue(const QoreValue val, QoreInternalSerializationContext& context,
186 ExceptionSink* xsink);
187
188 DLLLOCAL static QoreValue serializeHashToData(const QoreHashNode& h, bool weak,
189 QoreInternalSerializationContext& context, ExceptionSink* xsink);
190
191 DLLLOCAL static imap_t::iterator serializeHashToIndex(const QoreHashNode& h,
192 QoreInternalSerializationContext& context, ExceptionSink* xsink);
193
194 DLLLOCAL static imap_t::iterator serializeHashToIndexIntern(const QoreHashNode& h,
195 QoreInternalSerializationContext& context, const QoreString& str,
196 imap_t::iterator hint, ExceptionSink* xsink);
197
198 DLLLOCAL static QoreValue serializeListToData(const QoreListNode& l, bool weak,
199 QoreInternalSerializationContext& context, ExceptionSink* xsink);
200
201 DLLLOCAL static imap_t::iterator serializeListToIndex(const QoreListNode& l,
202 QoreInternalSerializationContext& context, ExceptionSink* xsink);
203
204 DLLLOCAL static imap_t::iterator serializeListToIndexIntern(const QoreListNode& h,
205 QoreInternalSerializationContext& context, const QoreString& str,
206 imap_t::iterator hint, ExceptionSink* xsink);
207
208 DLLLOCAL static int readStringFromStream(StreamReader& reader, QoreString& str, const char* type,
209 ExceptionSink* xsink);
210 DLLLOCAL static int64 readIntFromStream(ExceptionSink* xsink, StreamReader& reader, const char* type,
211 bool can_be_negative = false);
212
213 DLLLOCAL static int serializeHashToStream(const QoreHashNode& h, StreamWriter& writer, ExceptionSink* xsink);
214 DLLLOCAL static int serializeStringToStream(const QoreStringNode& str, StreamWriter& writer,
215 ExceptionSink* xsink);
216 DLLLOCAL static int serializeStringToStream(StreamWriter& writer, const char* key, size_t len,
217 const QoreEncoding* enc, ExceptionSink* xsink);
218 DLLLOCAL static int serializeIntToStream(int64 i, StreamWriter& writer, ExceptionSink* xsink);
219 DLLLOCAL static int serializeBoolToStream(bool b, StreamWriter& writer, ExceptionSink* xsink);
220 DLLLOCAL static int serializeListToStream(const QoreListNode& l, StreamWriter& writer, ExceptionSink* xsink);
221 DLLLOCAL static int serializeFloatToStream(double f, StreamWriter& writer, ExceptionSink* xsink);
222 DLLLOCAL static int serializeNumberToStream(const QoreNumberNode& n, StreamWriter& writer, ExceptionSink* xsink);
223 DLLLOCAL static int serializeDateToStream(const DateTimeNode& n, StreamWriter& writer, ExceptionSink* xsink);
224 DLLLOCAL static int serializeBinaryToStream(const BinaryNode& n, StreamWriter& writer, ExceptionSink* xsink);
225
226 DLLLOCAL static void serializeToStream(const QoreHashNode& h, OutputStream& stream, ExceptionSink* xsink);
227
228 DLLLOCAL static QoreValue deserializeData(const QoreValue val, QoreInternalDeserializationContext& context,
229 ExceptionSink* xsink);
230
231 DLLLOCAL static int deserializeStaticClassVars(ExceptionSink* xsink, const QoreHashNode& svars,
232 QoreInternalDeserializationContext& context);
233
234 DLLLOCAL static QoreValue deserializeHashData(const QoreStringNode& type, const QoreHashNode& h,
235 QoreInternalDeserializationContext& context, ExceptionSink* xsink, QoreHashNode* rv = nullptr);
236
237 DLLLOCAL static QoreValue deserializeListData(QoreValue v, const QoreHashNode& h,
238 QoreInternalDeserializationContext& context, ExceptionSink* xsink, QoreListNode* rv = nullptr);
239
240 DLLLOCAL static QoreValue deserializeListData(const QoreListNode& l, QoreInternalDeserializationContext& context,
241 ExceptionSink* xsink, QoreListNode* rv = nullptr);
242
243 DLLLOCAL static QoreValue deserializeIndexedContainer(const char* key,
244 QoreInternalDeserializationContext& context, ExceptionSink* xsink);
245 DLLLOCAL static QoreValue deserializeIndexedWeakReference(const char* key,
246 QoreInternalDeserializationContext& context, ExceptionSink* xsink);
247
248 DLLLOCAL static QoreHashNode* deserializeHashFromStream(ExceptionSink* xsink, StreamReader& reader,
249 qore_stream_type code, int64 flags);
250
251 DLLLOCAL static QoreStringNode* deserializeStringFromStream(StreamReader& reader, qore_stream_type code,
252 ExceptionSink* xsink);
253
254 DLLLOCAL static int64 deserializeIntFromStream(StreamReader& reader, qore_stream_type code, ExceptionSink* xsink);
255
256 DLLLOCAL static QoreListNode* deserializeListFromStream(ExceptionSink* xsink, StreamReader& reader, int64 flags);
257
258 DLLLOCAL static double deserializeFloatFromStream(StreamReader& reader, ExceptionSink* xsink);
259
260 DLLLOCAL static QoreNumberNode* deserializeNumberFromStream(StreamReader& reader, ExceptionSink* xsink);
261
262 DLLLOCAL static DateTimeNode* deserializeAbsDateFromStream(StreamReader& reader, ExceptionSink* xsink);
263
264 DLLLOCAL static DateTimeNode* deserializeRelDateFromStream(StreamReader& reader, ExceptionSink* xsink);
265
266 DLLLOCAL static BinaryNode* deserializeBinaryFromStream(StreamReader& reader, ExceptionSink* xsink);
267};
268
269#endif // _QORE_CLASS_INTERN_QORESERIALIZABLE_H
the base class for all data to be used as private data of Qore objects
Definition AbstractPrivateData.h:44
holds arbitrary binary data
Definition BinaryNode.h:41
Qore's parse tree/value type for date-time values, reference-counted, dynamically-allocated only.
Definition DateTimeNode.h:47
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition ExceptionSink.h:50
Interface for private data of input streams.
Definition InputStream.h:44
Interface for private data of output streams.
Definition OutputStream.h:44
defines a Qore-language class
Definition QoreClass.h:310
defines string encoding functions in Qore
Definition QoreEncoding.h:83
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition QoreHashNode.h:51
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition QoreListNode.h:52
Qore's arbitrary-precision number value type, dynamically-allocated only, reference counted.
Definition QoreNumberNode.h:51
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition QoreObject.h:61
Qore's string type supported by the QoreEncoding class.
Definition QoreString.h:93
Qore's string value type, reference counted, dynamically-allocated only.
Definition QoreStringNode.h:50
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition ReferenceHolder.h:52
Private data for the Qore::StreamReader class.
Definition StreamReader.h:45
Private data for the Qore::StreamWriter class.
Definition StreamWriter.h:43
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition common.h:266
The main value class in Qore, designed to be passed by value.
Definition QoreValue.h:279