]> Dogcows Code - chaz/yoink/blob - src/Moof/Deserializer.hh
cf99c2de3c966bc22d1ac8da7c95846c27ffaa7d
[chaz/yoink] / src / Moof / 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 _MOOF_DESERIALIZER_HH_
30 #define _MOOF_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 #include <Moof/Exception.hh>
44
45
46 namespace Mf {
47
48
49 class Serializable; // forward declaration
50 typedef boost::shared_ptr<Serializable> SerializableP;
51
52
53 class Deserializer
54 {
55 class Impl;
56 boost::shared_ptr<Impl> impl_;
57
58 public:
59
60 /**
61 * Construction is initialization. A deserializer can be constructed with
62 * either an input stream or a string representing a filename that will be
63 * read from. If a stream is provided, it will not be closed after parsing,
64 * but the parser may read past the end of usable bytes, so you should not
65 * use a deserializer on a stream that you expect to continue to use after
66 * the deserialization.
67 * @param comments If true, C and C++-style comments will be allowed and
68 * ignored by the parser.
69 * @param check If true, UTF-8 strings will be checked for validity and an
70 * exception thrown if such a problem exists. Otherwise strings will not be
71 * checked.
72 */
73
74 Deserializer(const std::string& filePath, bool comments = false,
75 bool check = false);
76 Deserializer(std::istream& input, bool comments = false, bool check = false);
77
78
79 /**
80 * Parse the object from of the stream. The stream is considered to be
81 * dominated by the parser and may read and discard bytes past the end of
82 * the actual parsed object. Only one object can be deserialized by the
83 * deserializer.
84 */
85
86 SerializableP deserialize();
87
88 /**
89 * Used by serializable objects to parse themselves. These methods should
90 * generally not be used directory for deserialization. This one returns
91 * the next object in the queue which has been parsed. This method may
92 * block if more data is pending and an object has not bee constructed yet.
93 * The caller takes ownership of the object which has been allocated with
94 * the new operator and must therefore be sure to delete the object as
95 * appropriated. Null (0) will be returned by this method to signify one of
96 * three things: 1) the end of an array, 2) the end of a map/dictionary, or
97 * 3) there is nothing more to be obtained. Container objects will be empty
98 * and will have to be filled with their contained elements by repeatedly
99 * calling this method until a null is returned. This method will continue
100 * to return the same value until pop() is called which will cause this
101 * method to return the next object as expected.
102 */
103
104 Serializable* pullNext();
105
106 /**
107 * If the object returned by pullNext() has been received successfully and
108 * the caller is ready for the next object, this method should be called to
109 * take that object off of the queue.
110 */
111
112 void pop();
113
114
115 /**
116 * This exception is thrown upon deserialization errors.
117 */
118
119 struct Exception : public Mf::Exception
120 {
121 enum
122 {
123 PARSING_FAILED = 1024
124 };
125
126 explicit Exception(unsigned error) :
127 Mf::Exception(error) {}
128
129 void raise()
130 {
131 throw *this;
132 }
133
134 const char* what() const throw()
135 {
136 switch (code)
137 {
138 case PARSING_FAILED:
139 return "parsing failed";
140 }
141 return Mf::Exception::what();
142 }
143 };
144 };
145
146
147 } // namespace Mf
148
149
150 #endif // _MOOF_DESERIALIZER_HH_
151
152 /** vim: set ts=4 sw=4 tw=80: *************************************************/
153
This page took 0.035376 seconds and 3 git commands to generate.