]>
Dogcows Code - chaz/yoink/blob - src/Moof/Serializable.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
29 #ifndef _MOOF_SERIALIZABLE_HH_
30 #define _MOOF_SERIALIZABLE_HH_
37 #include <Moof/Deserializer.hh>
38 #include <Moof/Serializer.hh>
39 #include <Moof/StringTools.hh>
46 * Interface for a type which can be serialized and deserialized.
51 // basic types which are serializable
55 typedef std::string String
;
56 typedef std::wstring WideString
;
57 typedef std::vector
<SerializableP
> Array
;
58 typedef std::map
<String
,SerializableP
> Map
;
61 virtual ~Serializable();
63 virtual void serialize(Serializer
& out
) const = 0;
64 virtual void deserialize(Deserializer
& in
) = 0;
66 virtual void print() const = 0;
68 virtual bool get(Integer
& value
);
69 virtual bool get(Float
& value
);
70 virtual bool get(Boolean
& value
);
71 virtual bool get(String
& value
);
72 virtual bool get(WideString
& value
);
73 virtual bool get(Array
& value
);
74 virtual bool get(Map
& value
);
77 * To get a number value which may have been parsed as either an integer or
78 * double, use these getters instead.
81 bool getNumber(Integer
& value
);
82 bool getNumber(Float
& value
);
84 virtual bool isNull();
89 class SerializableBasic
: public Serializable
95 SerializableBasic() {}
96 SerializableBasic(const T
& value
) :
99 void serialize(Serializer
& out
) const;
100 void deserialize(Deserializer
& in
);
107 struct SerializableNull
: public Serializable
109 SerializableNull() {}
111 void serialize(Serializer
& out
) const;
112 void deserialize(Deserializer
& in
);
120 inline void SerializableBasic
<T
>::serialize(Serializer
& out
) const
127 SerializableBasic
<Serializable::Array
>::serialize(Serializer
& out
) const
131 std::vector
<SerializableP
>::const_iterator it
;
132 for (it
= value_
.begin(); it
< value_
.end(); ++it
)
134 (*it
)->serialize(out
);
142 SerializableBasic
<Serializable::Map
>::serialize(Serializer
& out
) const
146 for (Map::const_iterator it
= value_
.begin(); it
!= value_
.end(); ++it
)
148 out
.push((*it
).first
);
149 (*it
).second
->serialize(out
);
155 inline void SerializableNull::serialize(Serializer
& out
) const
162 inline void SerializableBasic
<T
>::deserialize(Deserializer
& in
)
169 SerializableBasic
<Serializable::Array
>::deserialize(Deserializer
& in
)
175 while (obj
= in
.deserialize())
177 value_
.push_back(obj
);
185 SerializableBasic
<Serializable::Map
>::deserialize(Deserializer
& in
)
191 while (obj
= in
.deserialize())
196 value_
[key
] = in
.deserialize();
203 inline void SerializableNull::deserialize(Deserializer
& in
)
210 inline void SerializableBasic
<T
>::print() const
212 std::cout
<< std::boolalpha
<< typeid(T
).name() << "(" << value_
<< ")";
216 inline void SerializableBasic
<Serializable::WideString
>::print() const
218 std::wcout
<< value_
;
222 inline void SerializableBasic
<Serializable::Array
>::print() const
224 std::cout
<< "array";
228 inline void SerializableBasic
<Serializable::Map
>::print() const
233 inline void SerializableNull::print() const
240 inline bool SerializableBasic
<T
>::get(T
& value
)
246 inline bool SerializableNull::isNull()
254 #endif // _MOOF_SERIALIZABLE_HH_
256 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.042879 seconds and 4 git commands to generate.