Adding support for STL types

This commit is contained in:
Alex Yatskov 2012-02-19 12:57:24 -08:00
parent 86b851eff6
commit 7b26257af8
15 changed files with 157 additions and 39 deletions

View File

@ -33,8 +33,8 @@ using namespace metacall;
// Local functions // Local functions
// //
static void serverTest1(const char str[], int num) { static void serverTest1(const std::string & str, int num) {
printf("Server function params are \"%s\" and \"%d\"\n", str, num); printf("Server function params are \"%s\" and \"%d\"\n", str.c_str(), num);
} }

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_binding.hpp"
namespace metacall { namespace metacall {

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_buffer.hpp"
namespace metacall { namespace metacall {

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_client.hpp"
namespace metacall { namespace metacall {

90
mc_prefab-inl.hpp Normal file
View File

@ -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 <typename T, typename C, typename A>
bool serialize(Serializer* serializer, const std::basic_string<T, C, A>& value) {
const T * const str = value.empty() ? NULL : value.c_str();
return serialize(serializer, str);
}
template <typename T, typename C, typename A>
bool deserialize(Deserializer* deserializer, std::basic_string<T, C, A>* value) {
value->clear();
const T * str = NULL;
if (!deserialize(deserializer, &str)) {
return false;
}
if (str != NULL) {
*value = str;
}
return true;
}
//
// std::vector
//
template <typename T, typename A>
bool serialize(Serializer* serializer, const std::vector<T, A>& value) {
serializer->write(value.size());
for (const typename std::vector<T, A>::const_iterator iter = value.begin(); iter != value.end(); ++iter) {
serializer->write(*iter);
}
return true;
}
template <typename T, typename A>
bool deserialize(Deserializer* deserializer, std::vector<T, A>* 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;
}
}

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_prefab.hpp"
namespace metacall { namespace metacall {
@ -33,51 +32,58 @@ namespace metacall {
// Local functions // Local functions
// //
template <typename T> static int strLen(const char str[]) {
static int genStrLen(const T str[]) { return strlen(str);
int length = 0;
while (str[length] != 0) {
++length;
} }
return length; static int strLen(const wchar_t str[]) {
return wcslen(str);
} }
template <typename T> template <typename T>
static bool serializeStr(Serializer* serializer, const T str[]) { static bool serializeStr(Serializer* serializer, const T value[]) {
serializer->writeRaw(str, sizeof(T) * (genStrLen(str) + 1)); const int length = value == NULL ? 0 : strLen(value) + 1;
serializer->write(length);
serializer->writeRaw(value, length * sizeof(T));
return true; return true;
} }
template <typename T> template <typename T>
static bool deserializeStr(Deserializer* deserializer, const T ** str) { static bool deserializeStr(Deserializer* deserializer, const T ** value) {
*str = reinterpret_cast<const T*>(deserializer->readRaw(1)); *value = NULL;
if (*str == NULL) {
int length = 0;
if (!deserializer->read(&length)) {
return false; return false;
} }
return deserializer->readRaw(genStrLen(*str)) != NULL; if (length == 0) {
return true;
}
*value = reinterpret_cast<const T*>(deserializer->readRaw(length * sizeof(T)));
return *value != NULL;
} }
// //
// Shared functions // C strings
// //
bool serialize(Serializer* serializer, const char str[]) { bool serialize(Serializer* serializer, const char value[]) {
return serializeStr(serializer, str); return serializeStr(serializer, value);
} }
bool deserialize(Deserializer* deserializer, const char ** str) { bool deserialize(Deserializer* deserializer, const char ** value) {
return deserializeStr(deserializer, str); return deserializeStr(deserializer, value);
} }
bool serialize(Serializer* serializer, const wchar_t str[]) { bool serialize(Serializer* serializer, const wchar_t value[]) {
return serializeStr(serializer, str); return serializeStr(serializer, value);
} }
bool deserialize(Deserializer* deserializer, const wchar_t ** str) { bool deserialize(Deserializer* deserializer, const wchar_t ** value) {
return deserializeStr(deserializer, str); return deserializeStr(deserializer, value);
} }

View File

@ -28,10 +28,39 @@
namespace metacall { namespace metacall {
bool serialize(Serializer* serializer, const char str[]); //
bool deserialize(Deserializer* deserializer, const char ** str); // C strings
bool serialize(Serializer* serializer, const wchar_t str[]); //
bool deserialize(Deserializer* deserializer, const wchar_t ** str);
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 <typename T, typename C, typename A>
bool serialize(Serializer* serializer, const std::basic_string<T, C, A>& value);
template <typename T, typename C, typename A>
bool deserialize(Deserializer* deserializer, std::basic_string<T, C, A>* value);
//
// std::vector
//
template <typename T, typename A>
bool serialize(Serializer* serializer, const std::vector<T, A>& value);
template <typename T, typename A>
bool deserialize(Deserializer* deserializer, std::vector<T, A>* value);
//
// std::map
//
} }

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_protocol.hpp"
namespace metacall { namespace metacall {

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_serial.hpp"
namespace metacall { namespace metacall {

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_server.hpp"
namespace metacall { namespace metacall {

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_socket.hpp"
namespace metacall { namespace metacall {

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_stream.hpp"
namespace metacall { namespace metacall {

View File

@ -24,7 +24,6 @@
// //
#include "metacall.hpp" #include "metacall.hpp"
#include "mc_token.hpp"
namespace metacall { namespace metacall {

View File

@ -63,3 +63,4 @@
#include "mc_client.hpp" #include "mc_client.hpp"
#include "mc_server.hpp" #include "mc_server.hpp"
#include "mc_prefab.hpp" #include "mc_prefab.hpp"
#include "mc_prefab-inl.hpp"

View File

@ -37,5 +37,6 @@ HEADERS += \
mc_buffer.hpp \ mc_buffer.hpp \
mc_binding.hpp \ mc_binding.hpp \
mc_binding-inl.hpp \ mc_binding-inl.hpp \
mc_prefab.hpp mc_prefab.hpp \
mc_prefab-inl.hpp