cpp: object::object(const T& v, zone* z)

This commit is contained in:
frsyuki 2010-04-25 01:12:25 +09:00
parent 120e8bffd7
commit 4e85ebbf98
10 changed files with 152 additions and 1 deletions

View file

@ -49,6 +49,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::deque<T>& v)
return o;
}
template <typename T>
inline void operator<< (object::object_zone& o, const std::deque<T>& v)
{
o.type = type::ARRAY;
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
object* const pend = p + v.size();
o.via.array.ptr = p;
o.via.array.size = v.size();
typename std::deque<T>::const_iterator it(v.begin());
do {
*p = object(*it, o.zone);
++p;
++it;
} while(p < pend);
}
} // namespace msgpack

View file

@ -49,6 +49,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::list<T>& v)
return o;
}
template <typename T>
inline void operator<< (object::object_zone& o, const std::list<T>& v)
{
o.type = type::ARRAY;
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
object* const pend = p + v.size();
o.via.array.ptr = p;
o.via.array.size = v.size();
typename std::list<T>::const_iterator it(v.begin());
do {
*p = object(*it, o.zone);
++p;
++it;
} while(p < pend);
}
} // namespace msgpack

View file

@ -43,6 +43,17 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::pair<T1, T2>& v
return o;
}
template <typename T1, typename T2>
inline void operator<< (object::object_zone& o, const std::pair<T1, T2>& v)
{
o.type = type::ARRAY;
object* p = (object*)o.zone->malloc(sizeof(object)*2);
o.via.array.ptr = p;
o.via.array.size = 2;
p[0] = object(v.first, o.zone);
p[1] = object(v.second, o.zone);
}
} // namespace msgpack

View file

@ -77,6 +77,13 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const type::raw_ref& v)
return o;
}
inline void operator<< (object& o, const type::raw_ref& v)
{
o.type = type::RAW;
o.via.raw.ptr = v.ptr;
o.via.raw.size = v.size;
}
} // namespace msgpack

View file

@ -48,6 +48,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::set<T>& v)
return o;
}
template <typename T>
inline void operator<< (object::object_zone& o, const std::set<T>& v)
{
o.type = type::ARRAY;
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
object* const pend = p + v.size();
o.via.array.ptr = p;
o.via.array.size = v.size();
typename std::set<T>::const_iterator it(v.begin());
do {
*p = object(*it, o.zone);
++p;
++it;
} while(p < pend);
}
template <typename T>
inline std::multiset<T>& operator>> (object o, std::multiset<T>& v)
@ -73,6 +89,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::multiset<T>& v)
return o;
}
template <typename T>
inline void operator<< (object::object_zone& o, const std::multiset<T>& v)
{
o.type = type::ARRAY;
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
object* const pend = p + v.size();
o.via.array.ptr = p;
o.via.array.size = v.size();
typename std::multiset<T>::const_iterator it(v.begin());
do {
*p = object(*it, o.zone);
++p;
++it;
} while(p < pend);
}
} // namespace msgpack

View file

@ -39,6 +39,15 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::string& v)
return o;
}
inline void operator<< (object::object_zone& o, const std::string& v)
{
o.type = type::RAW;
char* ptr = (char*)o.zone->malloc(v.size());
o.via.raw.ptr = ptr;
o.via.raw.size = v.size();
memcpy(ptr, v.data(), v.size());
}
} // namespace msgpack

View file

@ -165,6 +165,26 @@ const packer<Stream>& operator<< (
}
<%}%>
inline void operator<< (
object::object_zone& o,
const type::tuple<>& v) {
o.type = type::ARRAY;
o.via.array.ptr = NULL;
o.via.array.size = 0;
}
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
inline void operator<< (
object::object_zone& o,
const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
o.type = type::ARRAY;
o.via.array.ptr = (object*)o.zone->malloc(sizeof(object)*<%=i+1%>);
o.via.array.size = <%=i+1%>;
<%0.upto(i) {|j|%>
o.via.array.ptr[<%=j%>] = object(v.template get<<%=j%>>(), o.zone);<%}%>
}
<%}%>
} // namespace msgpack
#endif /* msgpack/type/tuple.hpp */

View file

@ -53,6 +53,22 @@ inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<T>& v)
return o;
}
template <typename T>
inline void operator<< (object::object_zone& o, const std::vector<T>& v)
{
o.type = type::ARRAY;
object* p = (object*)o.zone->malloc(sizeof(object)*v.size());
object* const pend = p + v.size();
o.via.array.ptr = p;
o.via.array.size = v.size();
typename std::vector<T>::const_iterator it(v.begin());
do {
*p = object(*it, o.zone);
++p;
++it;
} while(p < pend);
}
} // namespace msgpack