summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2009-08-06 13:51:49 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2009-08-06 13:51:49 +0900
commit3afa9f265ebb283f26cb516ceca52cc083916cf7 (patch)
tree061f2af807adc15cf7fbaefeffde47c1f5df570e /cpp
parentec8932d6a11977136516fb2b7a3e3015cab0c3ac (diff)
downloadmsgpack-python-3afa9f265ebb283f26cb516ceca52cc083916cf7.tar.gz
cpp: add msgpack::type::define
Diffstat (limited to 'cpp')
-rw-r--r--cpp/Makefile.am7
-rw-r--r--cpp/type.hpp1
-rw-r--r--cpp/type/define.hpp.erb102
3 files changed, 109 insertions, 1 deletions
diff --git a/cpp/Makefile.am b/cpp/Makefile.am
index b9126f8..5efac3f 100644
--- a/cpp/Makefile.am
+++ b/cpp/Makefile.am
@@ -19,7 +19,8 @@ nobase_include_HEADERS = \
msgpack/type/map.hpp \
msgpack/type/nil.hpp \
msgpack/type/raw.hpp \
- msgpack/type/tuple.hpp
+ msgpack/type/tuple.hpp \
+ msgpack/type/define.hpp
# FIXME
object.lo: msgpack/type/tuple.hpp msgpack/zone.hpp
@@ -28,6 +29,10 @@ msgpack/type/tuple.hpp: msgpack/type/tuple.hpp.erb
$(ERB) $< > $@.tmp
mv $@.tmp $@
+msgpack/type/define.hpp: msgpack/type/define.hpp.erb
+ $(ERB) $< > $@.tmp
+ mv $@.tmp $@
+
msgpack/zone.hpp: msgpack/zone.hpp.erb
$(ERB) $< > $@.tmp
mv $@.tmp $@
diff --git a/cpp/type.hpp b/cpp/type.hpp
index 1dfd414..8ffe3bf 100644
--- a/cpp/type.hpp
+++ b/cpp/type.hpp
@@ -6,4 +6,5 @@
#include "msgpack/type/nil.hpp"
#include "msgpack/type/raw.hpp"
#include "msgpack/type/tuple.hpp"
+#include "msgpack/type/define.hpp"
diff --git a/cpp/type/define.hpp.erb b/cpp/type/define.hpp.erb
new file mode 100644
index 0000000..9be4f77
--- /dev/null
+++ b/cpp/type/define.hpp.erb
@@ -0,0 +1,102 @@
+//
+// MessagePack for C++ static resolution routine
+//
+// Copyright (C) 2008-2009 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+#ifndef MSGPACK_TYPE_DEFINE_HPP__
+#define MSGPACK_TYPE_DEFINE_HPP__
+
+#define MSGPACK_DEFINE(...) \
+ template <typename Packer> \
+ void msgpack_pack(Packer& pk) const \
+ { \
+ msgpack::type::make_define(__VA_ARGS__).msgpack_pack(pk); \
+ } \
+ \
+ void msgpack_unpack(msgpack::object o) \
+ { \
+ msgpack::type::make_define(__VA_ARGS__).msgpack_unpack(o); \
+ }
+
+namespace msgpack {
+namespace type {
+
+
+<% GENERATION_LIMIT = 15 %>
+template <typename A0 = void<%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%>>
+struct define;
+
+
+template <>
+struct define<> {
+ typedef define<> value_type;
+ typedef tuple<> tuple_type;
+ template <typename Packer>
+ void msgpack_pack(Packer& pk) const
+ {
+ pk.pack_array(1);
+ }
+ void msgpack_unpack(msgpack::object o)
+ {
+ if(o.type != type::ARRAY) { throw type_error(); }
+ }
+};
+
+<%0.upto(GENERATION_LIMIT) {|i|%>
+template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
+struct define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
+ typedef define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
+ typedef tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> tuple_type;
+ define(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
+ a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
+ template <typename Packer>
+ void msgpack_pack(Packer& pk) const
+ {
+ pk.pack_array(<%=i+1%>);
+ <%0.upto(i) {|j|%>
+ pk.pack(a<%=j%>);<%}%>
+ }
+ void msgpack_unpack(msgpack::object o)
+ {
+ if(o.type != type::ARRAY) { throw type_error(); }
+ const size_t size = o.via.array.size;
+ <%0.upto(i) {|j|%>
+ if(size <= <%=j%>) { return; } o.via.array.ptr[<%=j%>].convert(&a<%=j%>);<%}%>
+ }
+ <%0.upto(i) {|j|%>
+ A<%=j%>& a<%=j%>;<%}%>
+};
+<%}%>
+
+define<> make_define()
+{
+ return define<>();
+}
+
+<%0.upto(GENERATION_LIMIT) {|i|%>
+template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
+define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
+{
+ return define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
+}
+<%}%>
+
+
+} // namespace type
+} // namespace msgpack
+
+
+#endif /* msgpack/type/define.hpp */
+