lang/c/msgpack: C++ binding: reexamined global operators

git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@72 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
frsyuki
2009-02-15 09:09:58 +00:00
parent f4a6d7faa1
commit 2c7f0b2b1a
10 changed files with 241 additions and 209 deletions

View File

@@ -21,15 +21,14 @@
#include "msgpack/object.hpp"
namespace msgpack {
namespace type {
namespace type {
// FIXME operator==
// FIXME operator!=
<% GENERATION_LIMIT = 15 %>
template < typename A0 <%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%> >
template <typename A0 = void<%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%>>
struct tuple;
template <typename Tuple, int N>
@@ -60,9 +59,9 @@ private:
<%0.upto(GENERATION_LIMIT) {|i|%>
<%0.upto(i) {|j|%>
template < typename A0 <%1.upto(i) {|k|%>, typename A<%=k%> <%}%>>
struct tuple_type<tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>, <%=j%>> : tuple_element<A<%=j%>> {
tuple_type(tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>& x) : tuple_element<A<%=j%>>(x.a<%=j%>) {}
template <typename A0<%1.upto(i) {|k|%>, typename A<%=k%><%}%>>
struct tuple_type<tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>, <%=j%>> : tuple_element<A<%=j%>> {
tuple_type(tuple<A0<%1.upto(i) {|k|%>, A<%=k%> <%}%>>& x) : tuple_element<A<%=j%>>(x.a<%=j%>) {}
};
<%}%>
<%}%>
@@ -70,46 +69,57 @@ struct tuple_type<tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>, <%=j%>> : tuple_e
<%0.upto(GENERATION_LIMIT) {|i|%>
<%0.upto(i) {|j|%>
template < typename A0 <%1.upto(i) {|k|%>, typename A<%=k%> <%}%>>
struct const_tuple_type<tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>, <%=j%>> : const_tuple_element<A<%=j%>> {
const_tuple_type(const tuple<A0 <%1.upto(i) {|k|%>, A<%=k%> <%}%>>& x) : const_tuple_element<A<%=j%>>(x.a<%=j%>) {}
template <typename A0<%1.upto(i) {|k|%>, typename A<%=k%><%}%>>
struct const_tuple_type<tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>, <%=j%>> : const_tuple_element<A<%=j%>> {
const_tuple_type(const tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>& x) : const_tuple_element<A<%=j%>>(x.a<%=j%>) {}
};
<%}%>
<%}%>
<%0.upto(GENERATION_LIMIT) {|i|%>
template < typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>>
tuple< A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>> make_tuple(const A0& a0 <%1.upto(i) {|j|%>, const A<%=j%>& a<%=j%><%}%>)
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_tuple(const A0& a0<%1.upto(i) {|j|%>, const A<%=j%>& a<%=j%><%}%>)
{
return tuple< A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>(a0 <%1.upto(i) {|j|%>, a<%=j%><%}%>);
return tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
}
<%}%>
template <>
struct tuple<> {
};
<%0.upto(GENERATION_LIMIT) {|i|%>
template < typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>>
struct tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>> {
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
struct tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
tuple() {}
tuple(const A0& _a0 <%1.upto(i) {|j|%>, const A<%=j%>& _a<%=j%><%}%>) :
tuple(const A0& _a0<%1.upto(i) {|j|%>, const A<%=j%>& _a<%=j%><%}%>) :
a0(_a0) <%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
tuple(object o) { convert(*this, o); }
template <int N> typename tuple_type<tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>, N>::type& get()
{ return tuple_type<tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>, N>(*this).get(); }
template <int N> const typename const_tuple_type<tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>, N>::type& get() const
{ return const_tuple_type<tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>, N>(*this).get(); }
template <int N> typename tuple_type<tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>, N>::type& get()
{ return tuple_type<tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>, N>(*this).get(); }
template <int N> const typename const_tuple_type<tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>, N>::type& get() const
{ return const_tuple_type<tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>, N>(*this).get(); }
<%0.upto(i) {|j|%>
A<%=j%> a<%=j%>;<%}%>
};
<%}%>
} // namespace type
inline type::tuple<>& operator>> (
object o,
type::tuple<>& v) {
if(o.type != type::ARRAY) { throw type_error(); }
return v;
}
<%0.upto(GENERATION_LIMIT) {|i|%>
template < typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>>
tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& operator<< (
tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& v,
object o) {
if(o.type != ARRAY) { throw type_error(); }
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& operator>> (
object o,
type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
if(o.type != type::ARRAY) { throw type_error(); }
if(o.via.container.size < <%=i+1%>) { throw type_error(); }
<%0.upto(i) {|j|%>
convert<A<%=j%>>(v.template get<<%=j%>>(), o.via.container.ptr[<%=j%>]);<%}%>
@@ -118,6 +128,26 @@ tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& operator<< (
<%}%>
template <typename Stream>
const packer<Stream>& operator<< (
packer<Stream>& o,
const type::tuple<>& v) {
o.pack_array(0);
return o;
}
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename Stream, typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
const packer<Stream>& operator<< (
packer<Stream>& o,
const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
o.pack_array(<%=i+1%>);
<%0.upto(i) {|j|%>
pack(v.template get<<%=j%>>(), o);<%}%>
return o;
}
<%}%>
// FIXME
/*
template <typename A0, typename A1 = void, typename A2 = void>
@@ -128,7 +158,7 @@ struct tuple_just<A0> {
A0 a0;
static inline void convert(object o, tuple_just<A0>& v)
{
if(o.type != ARRAY) { throw type_error(); }
if(o.type != type::ARRAY) { throw type_error(); }
if(o.v.container.size != 1) { throw type_error(); }
msgpack::convert<A0>(o.v.container.ptr[0], v.a0);
}
@@ -140,7 +170,7 @@ struct tuple_just<A0, A1> {
A1 a1;
static inline void convert(object o, tuple_just<A0, A1>& v)
{
if(o.type != ARRAY) { throw type_error(); }
if(o.type != type::ARRAY) { throw type_error(); }
if(o.v.container.size != 2) { throw type_error(); }
msgpack::convert<A0>(o.v.container.ptr[0], v.a0);
msgpack::convert<A1>(o.v.container.ptr[1], v.a1);
@@ -149,24 +179,7 @@ struct tuple_just<A0, A1> {
*/
<%0.upto(GENERATION_LIMIT) {|i|%>
template < typename Stream , typename A0 <%1.upto(i) {|j|%>, typename A<%=j%> <%}%>>
const tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& operator>> (
const tuple<A0 <%1.upto(i) {|j|%>, A<%=j%> <%}%>>& v,
packer<Stream> o) {
o.pack_array(<%=i+1%>);
<%0.upto(i) {|j|%>
pack(v.template get<<%=j%>>(), o);<%}%>
return v;
}
<%}%>
} // namespace type
} // namespace msgpack
#endif /* msgpack/type/tuple.hpp */