diff --git a/mc_prefab-inl.hpp b/mc_prefab-inl.hpp index 46b8839..da15126 100644 --- a/mc_prefab-inl.hpp +++ b/mc_prefab-inl.hpp @@ -76,11 +76,102 @@ bool deserialize(Deserializer* deserializer, std::vector* value) { return false; } - value->reserve(length); for (int i = 0; i < length; ++i) { - if (!deserializer->read(value->at(i))) { + T item = T(); + if (!deserializer->read(&item)) { return false; } + + value->push_back(item); + } + + return true; +} + + +// +// std::list +// + +template +bool serialize(Serializer* serializer, const std::list& value) { + serializer->write(value.size()); + for (const typename std::list::const_iterator iter = value.begin(); iter != value.end(); ++iter) { + serializer->write(*iter); + } + + return true; +} + +template +bool deserialize(Deserializer* deserializer, std::list* value) { + value->clear(); + + int length = 0; + if (!deserializer->read(&length)) { + return false; + } + + for (int i = 0; i < length; ++i) { + T item = T(); + if (!deserializer->read(&item)) { + return false; + } + + value->push_back(item); + } + + return true; +} + + +// +// std::pair +// + +template +bool serialize(Serializer* serializer, const std::pair& value) { + serializer->write(value.first); + serializer->write(value.second); + return true; +} + +template +bool deserialize(Deserializer* deserializer, std::pair* value) { + return deserializer->read(&value->first) && deserializer->read(&value->second); +} + + +// +// std::map +// + +template +bool serialize(Serializer* serializer, const std::map& value) { + serializer->write(value.size()); + for (const typename std::map::const_iterator iter = value.begin(); iter != value.end(); ++iter) { + serializer->write(*iter); + } + + return true; +} + +template +bool deserialize(Deserializer* deserializer, std::map* value) { + value->clear(); + + int length = 0; + if (!deserializer->read(&length)) { + return false; + } + + for (unsigned i = 0; i < length; ++i) { + std::pair item; + if (!deserializer->read(&item)) { + return false; + } + + value->insert(item); } return true; diff --git a/mc_prefab.hpp b/mc_prefab.hpp index b2c1f98..9363c9a 100644 --- a/mc_prefab.hpp +++ b/mc_prefab.hpp @@ -58,9 +58,34 @@ template bool deserialize(Deserializer* deserializer, std::vector* value); +// +// std::list +// + +template +bool serialize(Serializer* serializer, const std::list& value); +template +bool deserialize(Deserializer* deserializer, std::list* value); + + +// +// std::pair +// + +template +bool serialize(Serializer* serializer, const std::pair& value); +template +bool deserialize(Deserializer* deserializer, std::pair* value); + + // // std::map // +template +bool serialize(Serializer* serializer, const std::map& value); +template +bool deserialize(Deserializer* deserializer, std::map* value); + } diff --git a/metacall.hpp b/metacall.hpp index d1dcb4e..4b14df8 100644 --- a/metacall.hpp +++ b/metacall.hpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include