diff --git a/configure.in b/configure.in index 45a04a92..8646d1e4 100644 --- a/configure.in +++ b/configure.in @@ -1,6 +1,6 @@ AC_INIT(msgpack/unpack_template.h) AC_CONFIG_AUX_DIR(ac) -AM_INIT_AUTOMAKE(msgpack, 0.3.3) +AM_INIT_AUTOMAKE(msgpack, 0.3.4) AC_CONFIG_HEADER(config.h) AC_SUBST(CFLAGS) diff --git a/cpp/Makefile.am b/cpp/Makefile.am index b9126f8c..5efac3f8 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 1dfd4141..8ffe3bfc 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 00000000..9be4f77c --- /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 \ + 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 A<%=i%> = void<%}%>> +struct define; + + +template <> +struct define<> { + typedef define<> value_type; + typedef tuple<> tuple_type; + template + 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 A<%=j%><%}%>> +struct define, A<%=j%><%}%>> { + typedef define, A<%=j%><%}%>> value_type; + typedef tuple, 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 + 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 A<%=j%><%}%>> +define, A<%=j%><%}%>> make_define(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>) +{ + return define, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>); +} +<%}%> + + +} // namespace type +} // namespace msgpack + + +#endif /* msgpack/type/define.hpp */ +