diff --git a/main.cpp b/main.cpp index 92890c0..45cfa82 100644 --- a/main.cpp +++ b/main.cpp @@ -33,8 +33,8 @@ using namespace metacall; // Local functions // -static void serverTest1(const char str[], int num) { - printf("Server function params are \"%s\" and \"%d\"\n", str, num); +static void serverTest1(const std::string & str, int num) { + printf("Server function params are \"%s\" and \"%d\"\n", str.c_str(), num); } diff --git a/mc_binding.cpp b/mc_binding.cpp index a7b3e58..13103f5 100644 --- a/mc_binding.cpp +++ b/mc_binding.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_binding.hpp" namespace metacall { diff --git a/mc_buffer.cpp b/mc_buffer.cpp index b3dcaad..4fe7014 100644 --- a/mc_buffer.cpp +++ b/mc_buffer.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_buffer.hpp" namespace metacall { diff --git a/mc_client.cpp b/mc_client.cpp index 088e397..8f3990f 100644 --- a/mc_client.cpp +++ b/mc_client.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_client.hpp" namespace metacall { diff --git a/mc_prefab-inl.hpp b/mc_prefab-inl.hpp new file mode 100644 index 0000000..46b8839 --- /dev/null +++ b/mc_prefab-inl.hpp @@ -0,0 +1,90 @@ +// +// Copyright (c) 2011 Alex Yatskov +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace metacall { + + +// +// std::basic_string +// + +template +bool serialize(Serializer* serializer, const std::basic_string& value) { + const T * const str = value.empty() ? NULL : value.c_str(); + return serialize(serializer, str); +} + +template +bool deserialize(Deserializer* deserializer, std::basic_string* value) { + value->clear(); + + const T * str = NULL; + if (!deserialize(deserializer, &str)) { + return false; + } + + if (str != NULL) { + *value = str; + } + + return true; +} + + +// +// std::vector +// + +template +bool serialize(Serializer* serializer, const std::vector& value) { + serializer->write(value.size()); + for (const typename std::vector::const_iterator iter = value.begin(); iter != value.end(); ++iter) { + serializer->write(*iter); + } + + return true; +} + +template +bool deserialize(Deserializer* deserializer, std::vector* value) { + value->clear(); + + int length = 0; + if (!deserializer->read(&length)) { + return false; + } + + value->reserve(length); + for (int i = 0; i < length; ++i) { + if (!deserializer->read(value->at(i))) { + return false; + } + } + + return true; +} + + +} diff --git a/mc_prefab.cpp b/mc_prefab.cpp index 8e0b033..d0db252 100644 --- a/mc_prefab.cpp +++ b/mc_prefab.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_prefab.hpp" namespace metacall { @@ -33,51 +32,58 @@ namespace metacall { // Local functions // -template -static int genStrLen(const T str[]) { - int length = 0; - while (str[length] != 0) { - ++length; - } +static int strLen(const char str[]) { + return strlen(str); +} - return length; +static int strLen(const wchar_t str[]) { + return wcslen(str); } template -static bool serializeStr(Serializer* serializer, const T str[]) { - serializer->writeRaw(str, sizeof(T) * (genStrLen(str) + 1)); +static bool serializeStr(Serializer* serializer, const T value[]) { + const int length = value == NULL ? 0 : strLen(value) + 1; + serializer->write(length); + serializer->writeRaw(value, length * sizeof(T)); return true; } template -static bool deserializeStr(Deserializer* deserializer, const T ** str) { - *str = reinterpret_cast(deserializer->readRaw(1)); - if (*str == NULL) { +static bool deserializeStr(Deserializer* deserializer, const T ** value) { + *value = NULL; + + int length = 0; + if (!deserializer->read(&length)) { return false; } - return deserializer->readRaw(genStrLen(*str)) != NULL; + if (length == 0) { + return true; + } + + *value = reinterpret_cast(deserializer->readRaw(length * sizeof(T))); + return *value != NULL; } // -// Shared functions +// C strings // -bool serialize(Serializer* serializer, const char str[]) { - return serializeStr(serializer, str); +bool serialize(Serializer* serializer, const char value[]) { + return serializeStr(serializer, value); } -bool deserialize(Deserializer* deserializer, const char ** str) { - return deserializeStr(deserializer, str); +bool deserialize(Deserializer* deserializer, const char ** value) { + return deserializeStr(deserializer, value); } -bool serialize(Serializer* serializer, const wchar_t str[]) { - return serializeStr(serializer, str); +bool serialize(Serializer* serializer, const wchar_t value[]) { + return serializeStr(serializer, value); } -bool deserialize(Deserializer* deserializer, const wchar_t ** str) { - return deserializeStr(deserializer, str); +bool deserialize(Deserializer* deserializer, const wchar_t ** value) { + return deserializeStr(deserializer, value); } diff --git a/mc_prefab.hpp b/mc_prefab.hpp index 8aeb5da..b2c1f98 100644 --- a/mc_prefab.hpp +++ b/mc_prefab.hpp @@ -28,10 +28,39 @@ namespace metacall { -bool serialize(Serializer* serializer, const char str[]); -bool deserialize(Deserializer* deserializer, const char ** str); -bool serialize(Serializer* serializer, const wchar_t str[]); -bool deserialize(Deserializer* deserializer, const wchar_t ** str); +// +// C strings +// + +bool serialize(Serializer* serializer, const char value[]); +bool deserialize(Deserializer* deserializer, const char ** value); +bool serialize(Serializer* serializer, const wchar_t value[]); +bool deserialize(Deserializer* deserializer, const wchar_t ** value); + + +// +// std::basic_string +// + +template +bool serialize(Serializer* serializer, const std::basic_string& value); +template +bool deserialize(Deserializer* deserializer, std::basic_string* value); + + +// +// std::vector +// + +template +bool serialize(Serializer* serializer, const std::vector& value); +template +bool deserialize(Deserializer* deserializer, std::vector* value); + + +// +// std::map +// } diff --git a/mc_protocol.cpp b/mc_protocol.cpp index e3faca3..c1291d6 100644 --- a/mc_protocol.cpp +++ b/mc_protocol.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_protocol.hpp" namespace metacall { diff --git a/mc_serial.cpp b/mc_serial.cpp index 521b162..9c2a243 100644 --- a/mc_serial.cpp +++ b/mc_serial.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_serial.hpp" namespace metacall { diff --git a/mc_server.cpp b/mc_server.cpp index 3d4e74d..1f36540 100644 --- a/mc_server.cpp +++ b/mc_server.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_server.hpp" namespace metacall { diff --git a/mc_socket.cpp b/mc_socket.cpp index f976f2b..33215cd 100644 --- a/mc_socket.cpp +++ b/mc_socket.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_socket.hpp" namespace metacall { diff --git a/mc_stream.cpp b/mc_stream.cpp index e6938e1..16f30d8 100644 --- a/mc_stream.cpp +++ b/mc_stream.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_stream.hpp" namespace metacall { diff --git a/mc_token.cpp b/mc_token.cpp index 4b362f6..2f31e23 100644 --- a/mc_token.cpp +++ b/mc_token.cpp @@ -24,7 +24,6 @@ // #include "metacall.hpp" -#include "mc_token.hpp" namespace metacall { diff --git a/metacall.hpp b/metacall.hpp index 39c5fca..d1dcb4e 100644 --- a/metacall.hpp +++ b/metacall.hpp @@ -63,3 +63,4 @@ #include "mc_client.hpp" #include "mc_server.hpp" #include "mc_prefab.hpp" +#include "mc_prefab-inl.hpp" diff --git a/metacall.pro b/metacall.pro index 11ae209..dfa8268 100644 --- a/metacall.pro +++ b/metacall.pro @@ -37,5 +37,6 @@ HEADERS += \ mc_buffer.hpp \ mc_binding.hpp \ mc_binding-inl.hpp \ - mc_prefab.hpp + mc_prefab.hpp \ + mc_prefab-inl.hpp