Qore Programming Language  1.12.0
QoreSerializable.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreSerializable.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 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 objects
45 typedef std::map<std::string, QoreObject*> oimap_t;
46 
47 // maps from object hashes to index strings
48 typedef std::map<std::string, std::string> imap_t;
49 
50 // set of modules to load
51 typedef std::set<std::string> mset_t;
52 
53 namespace {
54 // qore serialization stream data type constants
55 enum 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 
77 class ObjectIndexMap : public oimap_t {
78 public:
79  DLLLOCAL ObjectIndexMap(ExceptionSink* xs) : xs(xs) {
80  }
81 
82  DLLLOCAL ~ObjectIndexMap();
83 
84 protected:
85  ExceptionSink* xs;
86 };
87 
88 class QoreSerializable : public AbstractPrivateData {
89 friend class QoreInternalSerializationContext;
90 friend class QoreInternalDeserializationContext;
91 
92 public:
93  DLLLOCAL static QoreHashNode* serializeToData(QoreValue val, ExceptionSink* xsink);
94 
95  DLLLOCAL static void serialize(const QoreObject& self, OutputStream& stream, ExceptionSink* xsink);
96 
97  DLLLOCAL static void serialize(const QoreValue val, OutputStream& stream, ExceptionSink* xsink);
98 
99  DLLLOCAL static QoreHashNode* deserializeToData(InputStream& stream, ExceptionSink* xsink);
100 
101  DLLLOCAL static QoreValue deserialize(InputStream& stream, ExceptionSink* xsink);
102 
103  DLLLOCAL static QoreValue deserialize(const QoreHashNode& h, ExceptionSink* xsink);
104 
105  DLLLOCAL static int serializeValueToStream(const QoreValue val, OutputStream& stream, ExceptionSink* xsink);
106 
107 protected:
108  DLLLOCAL virtual ~QoreSerializable() {}
109 
110  DLLLOCAL static QoreValue serializeObjectToData(const QoreObject& obj, ReferenceHolder<QoreHashNode>& index, imap_t& imap, mset_t& mset, ExceptionSink* xsink);
111 
112  DLLLOCAL static imap_t::iterator serializeObjectToIndex(const QoreObject& obj, ReferenceHolder<QoreHashNode>& index, imap_t& imap, mset_t& mset, ExceptionSink* xsink);
113 
114  DLLLOCAL static QoreValue serializeValue(const QoreValue val, ReferenceHolder<QoreHashNode>& index, imap_t& imap, mset_t& mset, ExceptionSink* xsink);
115 
116  DLLLOCAL static imap_t::iterator serializeObjectToIndexIntern(const QoreObject& self, ReferenceHolder<QoreHashNode>& index, imap_t& imap, mset_t& mset, const QoreString& str, imap_t::iterator hint, ExceptionSink* xsink);
117 
118  DLLLOCAL static QoreHashNode* serializeHashToData(const QoreHashNode& h, ReferenceHolder<QoreHashNode>& index, imap_t& imap, mset_t& mset, ExceptionSink* xsink);
119 
120  DLLLOCAL static QoreHashNode* serializeListToData(const QoreListNode& l, ReferenceHolder<QoreHashNode>& index, imap_t& imap, mset_t& mset, ExceptionSink* xsink);
121 
122  DLLLOCAL static int readStringFromStream(StreamReader& reader, QoreString& str, const char* type, ExceptionSink* xsink);
123  DLLLOCAL static int64 readIntFromStream(ExceptionSink* xsink, StreamReader& reader, const char* type, bool can_be_negative = false);
124 
125  DLLLOCAL static int serializeValueToStream(const QoreValue val, StreamWriter& writer, ExceptionSink* xsink);
126  DLLLOCAL static int serializeHashToStream(const QoreHashNode& h, StreamWriter& writer, ExceptionSink* xsink);
127  DLLLOCAL static int serializeStringToStream(const QoreStringNode& str, StreamWriter& writer, ExceptionSink* xsink);
128  DLLLOCAL static int serializeStringToStream(StreamWriter& writer, const char* key, size_t len, const QoreEncoding* enc, ExceptionSink* xsink);
129  DLLLOCAL static int serializeIntToStream(int64 i, StreamWriter& writer, ExceptionSink* xsink);
130  DLLLOCAL static int serializeBoolToStream(bool b, StreamWriter& writer, ExceptionSink* xsink);
131  DLLLOCAL static int serializeListToStream(const QoreListNode& l, StreamWriter& writer, ExceptionSink* xsink);
132  DLLLOCAL static int serializeFloatToStream(double f, StreamWriter& writer, ExceptionSink* xsink);
133  DLLLOCAL static int serializeNumberToStream(const QoreNumberNode& n, StreamWriter& writer, ExceptionSink* xsink);
134  DLLLOCAL static int serializeDateToStream(const DateTimeNode& n, StreamWriter& writer, ExceptionSink* xsink);
135  DLLLOCAL static int serializeBinaryToStream(const BinaryNode& n, StreamWriter& writer, ExceptionSink* xsink);
136 
137  DLLLOCAL static void serializeToStream(const QoreHashNode& h, OutputStream& stream, ExceptionSink* xsink);
138 
139  DLLLOCAL static QoreValue deserializeData(const QoreValue val, const oimap_t& oimap, ExceptionSink* xsink);
140 
141  DLLLOCAL static QoreValue deserializeHashData(const QoreStringNode& type, const QoreHashNode& h, const oimap_t& oimap, ExceptionSink* xsink);
142 
143  DLLLOCAL static QoreValue deserializeListData(const QoreListNode& l, const oimap_t& oimap, ExceptionSink* xsink);
144 
145  DLLLOCAL static QoreObject* deserializeIndexedObject(const char* key, const oimap_t& oimap, ExceptionSink* xsink);
146 
147  DLLLOCAL static QoreValue deserializeValueFromStream(StreamReader& reader, ExceptionSink* xsink);
148 
149  DLLLOCAL static QoreHashNode* deserializeHashFromStream(StreamReader& reader, qore_stream_type code, ExceptionSink* xsink);
150 
151  DLLLOCAL static QoreStringNode* deserializeStringFromStream(StreamReader& reader, qore_stream_type code, ExceptionSink* xsink);
152 
153  DLLLOCAL static int64 deserializeIntFromStream(StreamReader& reader, qore_stream_type code, ExceptionSink* xsink);
154 
155  DLLLOCAL static QoreListNode* deserializeListFromStream(StreamReader& reader, ExceptionSink* xsink);
156 
157  DLLLOCAL static double deserializeFloatFromStream(StreamReader& reader, ExceptionSink* xsink);
158 
159  DLLLOCAL static QoreNumberNode* deserializeNumberFromStream(StreamReader& reader, ExceptionSink* xsink);
160 
161  DLLLOCAL static DateTimeNode* deserializeAbsDateFromStream(StreamReader& reader, ExceptionSink* xsink);
162 
163  DLLLOCAL static DateTimeNode* deserializeRelDateFromStream(StreamReader& reader, ExceptionSink* xsink);
164 
165  DLLLOCAL static BinaryNode* deserializeBinaryFromStream(StreamReader& reader, ExceptionSink* xsink);
166 };
167 
168 #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:45
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
Interface for private data of input streams.
Definition: InputStream.h:44
Interface for private data of output streams.
Definition: OutputStream.h:44
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:50
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:60
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
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:260
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275