From 86b851eff6c6ada64264aaedff5ec451c8da1146 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 19 Feb 2012 11:32:23 -0800 Subject: [PATCH] Prefab framework --- main.cpp | 25 --------------- mc_binding.cpp | 1 + mc_prefab.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ mc_prefab.hpp | 37 ++++++++++++++++++++++ mc_token.cpp | 2 +- metacall.hpp | 1 + metacall.pro | 6 ++-- 7 files changed, 128 insertions(+), 28 deletions(-) create mode 100644 mc_prefab.cpp create mode 100644 mc_prefab.hpp diff --git a/main.cpp b/main.cpp index 56b1566..92890c0 100644 --- a/main.cpp +++ b/main.cpp @@ -29,31 +29,6 @@ using namespace metacall; -// -// Serialization -// - -namespace metacall { - - -bool serialize(Serializer* serializer, const char str[]) { - serializer->writeRaw(str, strlen(str) + 1); - return true; -} - -bool deserialize(Deserializer* deserializer, const char ** str) { - *str = reinterpret_cast(deserializer->readRaw(1)); - if (*str == NULL) { - return false; - } - - return deserializer->readRaw(strlen(*str)) != NULL; -} - - -} - - // // Local functions // diff --git a/mc_binding.cpp b/mc_binding.cpp index 13103f5..a7b3e58 100644 --- a/mc_binding.cpp +++ b/mc_binding.cpp @@ -24,6 +24,7 @@ // #include "metacall.hpp" +#include "mc_binding.hpp" namespace metacall { diff --git a/mc_prefab.cpp b/mc_prefab.cpp new file mode 100644 index 0000000..8e0b033 --- /dev/null +++ b/mc_prefab.cpp @@ -0,0 +1,84 @@ +// +// 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. +// + +#include "metacall.hpp" +#include "mc_prefab.hpp" + +namespace metacall { + + +// +// Local functions +// + +template +static int genStrLen(const T str[]) { + int length = 0; + while (str[length] != 0) { + ++length; + } + + return length; +} + +template +static bool serializeStr(Serializer* serializer, const T str[]) { + serializer->writeRaw(str, sizeof(T) * (genStrLen(str) + 1)); + return true; +} + +template +static bool deserializeStr(Deserializer* deserializer, const T ** str) { + *str = reinterpret_cast(deserializer->readRaw(1)); + if (*str == NULL) { + return false; + } + + return deserializer->readRaw(genStrLen(*str)) != NULL; +} + + +// +// Shared functions +// + +bool serialize(Serializer* serializer, const char str[]) { + return serializeStr(serializer, str); +} + +bool deserialize(Deserializer* deserializer, const char ** str) { + return deserializeStr(deserializer, str); +} + +bool serialize(Serializer* serializer, const wchar_t str[]) { + return serializeStr(serializer, str); +} + +bool deserialize(Deserializer* deserializer, const wchar_t ** str) { + return deserializeStr(deserializer, str); +} + + +} diff --git a/mc_prefab.hpp b/mc_prefab.hpp new file mode 100644 index 0000000..8aeb5da --- /dev/null +++ b/mc_prefab.hpp @@ -0,0 +1,37 @@ +// +// 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. +// + +#pragma once + +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); + + +} diff --git a/mc_token.cpp b/mc_token.cpp index 1f942ad..4b362f6 100644 --- a/mc_token.cpp +++ b/mc_token.cpp @@ -38,7 +38,7 @@ Token::Token(const char value[]) : { } -Token::Token(const unsigned value) : +Token::Token(unsigned value) : m_value(value) { } diff --git a/metacall.hpp b/metacall.hpp index e3928ce..39c5fca 100644 --- a/metacall.hpp +++ b/metacall.hpp @@ -62,3 +62,4 @@ #include "mc_protocol-inl.hpp" #include "mc_client.hpp" #include "mc_server.hpp" +#include "mc_prefab.hpp" diff --git a/metacall.pro b/metacall.pro index 48f5431..11ae209 100644 --- a/metacall.pro +++ b/metacall.pro @@ -11,7 +11,8 @@ SOURCES += main.cpp \ mc_protocol.cpp \ mc_client.cpp \ mc_buffer.cpp \ - mc_binding.cpp + mc_binding.cpp \ + mc_prefab.cpp OTHER_FILES += \ metacall.py \ @@ -35,5 +36,6 @@ HEADERS += \ mc_client.hpp \ mc_buffer.hpp \ mc_binding.hpp \ - mc_binding-inl.hpp + mc_binding-inl.hpp \ + mc_prefab.hpp