]>
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.
52 virtual ~Serializable();
54 virtual void serialize(Serializer
& out
) const = 0;
55 virtual void deserialize(Deserializer
& in
) = 0;
57 virtual void print() const = 0;
59 virtual bool get(long& value
);
60 virtual bool get(double& value
);
61 virtual bool get(bool& value
);
62 virtual bool get(std::string
& value
);
63 virtual bool get(std::wstring
& value
);
64 virtual bool get(std::vector
<SerializablePtr
>& value
);
65 virtual bool get(std::map
<std::string
,SerializablePtr
>& value
);
68 * To get a number value which may have been parsed as either an integer or
69 * double, use these getters instead.
72 bool getNumber(long&);
73 bool getNumber(double&);
75 virtual bool isNull();
80 class SerializableBase
: public Serializable
84 SerializableBase(const T
& value
) :
87 void serialize(Serializer
& out
) const;
88 void deserialize(Deserializer
& in
);
97 class SerializableNull
: public Serializable
100 SerializableNull() {}
101 void serialize(Serializer
& out
) const;
102 void deserialize(Deserializer
& in
);
109 typedef SerializableBase
<long> SerializableInteger
;
110 typedef SerializableBase
<double> SerializableReal
;
111 typedef SerializableBase
<bool> SerializableBoolean
;
112 typedef SerializableBase
<std::string
> SerializableString
;
113 typedef SerializableBase
<std::wstring
> SerializableWideString
;
114 typedef SerializableBase
<std::vector
<SerializablePtr
> > SerializableArray
;
115 typedef SerializableBase
<std::map
<std::string
,SerializablePtr
> >
121 inline void SerializableBase
<T
>::serialize(Serializer
& out
) const
128 SerializableBase
<std::vector
<SerializablePtr
> >::serialize(Serializer
& out
) const
132 std::vector
<SerializablePtr
>::const_iterator it
;
133 for (it
= value_
.begin(); it
< value_
.end(); ++it
)
135 (*it
)->serialize(out
);
143 SerializableBase
<std::map
<std::string
,SerializablePtr
> >::serialize(Serializer
& out
) const
147 std::map
<std::string
,SerializablePtr
>::const_iterator it
;
148 for (it
= value_
.begin(); it
!= value_
.end(); ++it
)
150 out
.push((*it
).first
);
151 (*it
).second
->serialize(out
);
157 inline void SerializableNull::serialize(Serializer
& out
) const
164 inline void SerializableBase
<T
>::deserialize(Deserializer
& in
)
170 inline void SerializableBase
<std::vector
<SerializablePtr
> >::deserialize(Deserializer
& in
)
176 while (obj
= in
.deserialize())
178 value_
.push_back(SerializablePtr(obj
));
186 SerializableBase
<std::map
<std::string
,SerializablePtr
> >::deserialize(Deserializer
& in
)
192 while (obj
= in
.deserialize())
197 value_
[key
] = in
.deserialize();
204 inline void SerializableNull::deserialize(Deserializer
& in
)
211 inline void SerializableBase
<T
>::print() const
213 std::cout
<< std::boolalpha
<< typeid(T
).name() << "(" << value_
<< ")";
217 inline void SerializableBase
<std::wstring
>::print() const
219 std::wcout
<< value_
;
223 inline void SerializableBase
<std::vector
<SerializablePtr
> >::print() const
225 std::cout
<< "array";
229 inline void SerializableBase
<std::map
<std::string
,SerializablePtr
> >::print() const
234 inline void SerializableNull::print() const
241 inline bool SerializableBase
<T
>::get(T
& value
)
247 inline bool SerializableNull::isNull()
255 #endif // _MOOF_SERIALIZABLE_HH_
257 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.048831 seconds and 4 git commands to generate.