1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2008-2009 FURUHASHI Sadayuki
5 //
6 //    Licensed under the Apache License, Version 2.0 (the "License");
7 //    you may not use this file except in compliance with the License.
8 //    You may obtain a copy of the License at
9 //
10 //        http://www.apache.org/licenses/LICENSE-2.0
11 //
12 //    Unless required by applicable law or agreed to in writing, software
13 //    distributed under the License is distributed on an "AS IS" BASIS,
14 //    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 //    See the License for the specific language governing permissions and
16 //    limitations under the License.
17 //
18 #ifndef MSGPACK_TYPE_STRING_HPP__
19 #define MSGPACK_TYPE_STRING_HPP__
20 
21 #include "msgpack/object.hpp"
22 #include <string>
23 
24 namespace msgpack {
25 
26 
operator >>(object o,std::string & v)27 inline std::string& operator>> (object o, std::string& v)
28 {
29 	if(o.type != type::RAW) { throw type_error(); }
30 	v.assign(o.via.raw.ptr, o.via.raw.size);
31 	return v;
32 }
33 
34 template <typename Stream>
operator <<(packer<Stream> & o,const std::string & v)35 inline packer<Stream>& operator<< (packer<Stream>& o, const std::string& v)
36 {
37 	o.pack_raw(v.size());
38 	o.pack_raw_body(v.data(), v.size());
39 	return o;
40 }
41 
operator <<(object::with_zone & o,const std::string & v)42 inline void operator<< (object::with_zone& o, const std::string& v)
43 {
44 	o.type = type::RAW;
45 	char* ptr = (char*)o.zone->malloc(v.size());
46 	o.via.raw.ptr = ptr;
47 	o.via.raw.size = (uint32_t)v.size();
48 	memcpy(ptr, v.data(), v.size());
49 }
50 
operator <<(object & o,const std::string & v)51 inline void operator<< (object& o, const std::string& v)
52 {
53 	o.type = type::RAW;
54 	o.via.raw.ptr = v.data();
55 	o.via.raw.size = (uint32_t)v.size();
56 }
57 
58 
59 }  // namespace msgpack
60 
61 #endif /* msgpack/type/string.hpp */
62 
63