]> Dogcows Code - chaz/yoink/blob - src/deserializer.hh
beginnings of scene rendering
[chaz/yoink] / src / deserializer.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _DESERIALIZER_HH_
30 #define _DESERIALIZER_HH_
31
32 /**
33 * @file deserializer.hh
34 * Deserialize structures and types from input on a stream.
35 */
36
37 #include <istream>
38 #include <string>
39 #include <stdexcept>
40
41 #include <boost/shared_ptr.hpp>
42
43
44 namespace dc {
45
46
47 class serializable; // forward declaration
48 typedef boost::shared_ptr<serializable> serializable_ptr;
49
50 class deserializer
51 {
52 public:
53
54 /**
55 * Construction is initialization. A deserializer can be constructed with
56 * either an input stream or a string representing a filename that will be
57 * read from. If a stream is provided, it will not be closed after parsing,
58 * but the parser may read past the end of usable bytes, so you should not
59 * use a deserializer on a stream that you expect to continue to use after
60 * the deserialization.
61 * @param comments If true, C and C++-style comments will be allowed and
62 * ignored by the parser.
63 * @param check If true, UTF-8 strings will be checked for validity and an
64 * exception thrown if such a problem exists. Otherwise strings will not be
65 * checked.
66 */
67
68 deserializer(const std::string& filePath, bool comments = false,
69 bool check = false);
70 deserializer(std::istream& input, bool comments = false, bool check = false);
71
72
73 /**
74 * Parse the object from of the stream. The stream is considered to be
75 * dominated by the parser and may read and discard bytes past the end of
76 * the actual parsed object. Only one object can be deserialized by the
77 * deserializer.
78 */
79
80 serializable_ptr deserialize();
81
82 /**
83 * Used by serializable objects to parse themselves. These methods should
84 * generally not be used directory for deserialization. This one returns
85 * the next object in the queue which has been parsed. This method may
86 * block if more data is pending and an object has not bee constructed yet.
87 * The caller takes ownership of the object which has been allocated with
88 * the new operator and must therefore be sure to delete the object as
89 * appropriated. Null (0) will be returned by this method to signify one of
90 * three things: 1) the end of an array, 2) the end of a map/dictionary, or
91 * 3) there is nothing more to be obtained. Container objects will be empty
92 * and will have to be filled with their contained elements by repeatedly
93 * calling this method until a null is returned. This method will continue
94 * to return the same value until pop() is called which will cause this
95 * method to return the next object as expected.
96 */
97
98 serializable* pullNext();
99
100 /**
101 * If the object returned by pullNext() has been received successfully and
102 * the caller is ready for the next object, this method should be called to
103 * take that object off of the queue.
104 */
105
106 void pop();
107
108
109 /**
110 * This exception is thrown upon deserialization errors.
111 */
112
113 struct exception : std::runtime_error
114 {
115 explicit exception(const std::string& what_arg) :
116 std::runtime_error(what_arg) {}
117 };
118
119 private:
120 class deserializer_impl;
121 boost::shared_ptr<deserializer_impl> impl;
122 };
123
124
125 } // namespace dc
126
127
128 #endif // _DESERIALIZER_HH_
129
130 /** vim: set ts=4 sw=4 tw=80: *************************************************/
131
This page took 0.036163 seconds and 4 git commands to generate.