mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-05-29 23:42:40 +02:00
commit
d6a7bd1995
@ -1,10 +1,28 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
|
||||
#include <msgpack.hpp>
|
||||
// msgpack.hpp is also ok
|
||||
#include <msgpack_fwd.hpp>
|
||||
|
||||
|
||||
class my_class {
|
||||
@ -36,10 +54,13 @@ void print(std::string const& buf) {
|
||||
<< (static_cast<int>(*it) & 0xff)
|
||||
<< ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << std::dec << std::endl;
|
||||
}
|
||||
|
||||
#include <msgpack.hpp>
|
||||
|
||||
int main() {
|
||||
{ // pack, unpack
|
||||
my_class my("John Smith", 42);
|
||||
std::stringstream ss;
|
||||
msgpack::pack(ss, my);
|
||||
@ -49,5 +70,14 @@ int main() {
|
||||
msgpack::unpacked unp;
|
||||
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
||||
msgpack::object obj = unp.get();
|
||||
std::cout << obj << std::endl;
|
||||
assert(obj.as<my_class>() == my);
|
||||
}
|
||||
{ // create object with zone
|
||||
my_class my("John Smith", 42);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj(my, z);
|
||||
std::cout << obj << std::endl;
|
||||
assert(obj.as<my_class>() == my);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,27 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
|
||||
// msgpack.hpp should be included at after user declarations
|
||||
#include <msgpack_fwd.hpp>
|
||||
|
||||
|
||||
@ -19,6 +37,8 @@ object const& operator>> (msgpack::object const& o, my_class& v);
|
||||
template <typename Stream>
|
||||
packer<Stream>& operator<< (msgpack::packer<Stream>& o, my_class const& v);
|
||||
|
||||
void operator<< (msgpack::object::with_zone& o, my_class const& v);
|
||||
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
||||
} // namespace msgpack
|
||||
|
||||
@ -69,6 +89,14 @@ inline packer<Stream>& operator<< (msgpack::packer<Stream>& o, my_class const& v
|
||||
return o;
|
||||
}
|
||||
|
||||
inline void operator<< (msgpack::object::with_zone& o, my_class const& v) {
|
||||
o.type = type::ARRAY;
|
||||
o.via.array.size = 2;
|
||||
o.via.array.ptr = static_cast<object*>(o.zone.allocate_align(sizeof(object) * o.via.array.size));
|
||||
o.via.array.ptr[0] = msgpack::object(v.get_name(), o.zone);
|
||||
o.via.array.ptr[1] = msgpack::object(v.get_age(), o.zone);
|
||||
}
|
||||
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
||||
} // namespace msgpack
|
||||
|
||||
@ -84,10 +112,11 @@ void print(std::string const& buf) {
|
||||
<< (static_cast<int>(*it) & 0xff)
|
||||
<< ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << std::dec << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
{ // pack, unpack
|
||||
my_class my("John Smith", 42);
|
||||
std::stringstream ss;
|
||||
msgpack::pack(ss, my);
|
||||
@ -97,5 +126,14 @@ int main() {
|
||||
msgpack::unpacked unp;
|
||||
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
||||
msgpack::object obj = unp.get();
|
||||
std::cout << obj << std::endl;
|
||||
assert(obj.as<my_class>() == my);
|
||||
}
|
||||
{ // create object with zone
|
||||
my_class my("John Smith", 42);
|
||||
msgpack::zone z;
|
||||
msgpack::object obj(my, z);
|
||||
std::cout << obj << std::endl;
|
||||
assert(obj.as<my_class>() == my);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,22 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include <msgpack.hpp>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
@ -27,11 +45,11 @@ int main(void)
|
||||
old_class oc;
|
||||
new_class nc;
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
std::stringstream sbuf;
|
||||
msgpack::pack(sbuf, oc);
|
||||
|
||||
msgpack::unpacked result;
|
||||
msgpack::unpack(result, sbuf.data(), sbuf.size());
|
||||
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size());
|
||||
msgpack::object obj = result.get();
|
||||
|
||||
obj.convert(&nc);
|
||||
@ -43,11 +61,11 @@ int main(void)
|
||||
new_class nc;
|
||||
old_class oc;
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
std::stringstream sbuf;
|
||||
msgpack::pack(sbuf, nc);
|
||||
|
||||
msgpack::unpacked result;
|
||||
msgpack::unpack(result, sbuf.data(), sbuf.size());
|
||||
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size());
|
||||
msgpack::object obj = result.get();
|
||||
|
||||
obj.convert(&oc);
|
||||
@ -55,4 +73,3 @@ int main(void)
|
||||
std::cout << obj << " value=" << oc.value << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
68
example/cpp03/enum.cpp
Normal file
68
example/cpp03/enum.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2015 KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include <msgpack_fwd.hpp>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
enum my_enum {
|
||||
elem1,
|
||||
elem2,
|
||||
elem3
|
||||
};
|
||||
|
||||
MSGPACK_ADD_ENUM(my_enum);
|
||||
|
||||
#include <msgpack.hpp>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
{ // pack, unpack
|
||||
std::stringstream sbuf;
|
||||
msgpack::pack(sbuf, elem1);
|
||||
msgpack::pack(sbuf, elem2);
|
||||
my_enum e3 = elem3;
|
||||
msgpack::pack(sbuf, e3);
|
||||
|
||||
msgpack::unpacked result;
|
||||
std::size_t off = 0;
|
||||
|
||||
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off);
|
||||
std::cout << result.get().as<my_enum>() << std::endl;
|
||||
assert(result.get().as<my_enum>() == elem1);
|
||||
|
||||
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off);
|
||||
std::cout << result.get().as<my_enum>() << std::endl;
|
||||
assert(result.get().as<my_enum>() == elem2);
|
||||
|
||||
msgpack::unpack(result, sbuf.str().data(), sbuf.str().size(), off);
|
||||
std::cout << result.get().as<my_enum>() << std::endl;
|
||||
assert(result.get().as<my_enum>() == elem3);
|
||||
}
|
||||
{ // create object without zone
|
||||
msgpack::object obj(elem2);
|
||||
std::cout << obj.as<my_enum>() << std::endl;
|
||||
assert(obj.as<my_enum>() == elem2);
|
||||
}
|
||||
{ // create object with zone
|
||||
msgpack::zone z;
|
||||
msgpack::object objz(elem3, z);
|
||||
std::cout << objz.as<my_enum>() << std::endl;
|
||||
assert(objz.as<my_enum>() == elem3);
|
||||
}
|
||||
}
|
@ -1,3 +1,20 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include <msgpack.hpp>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
@ -84,4 +101,3 @@ int main(void)
|
||||
std::cout << "received: " << o << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,20 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include <msgpack.hpp>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
@ -35,4 +52,3 @@ int main(void)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,20 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2013-2015 KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// g++ -std=c++11 -O3 -g -Ipath_to_msgpack_src -Ipath_to_boost speed_test.cc -Lpath_to_boost_lib -lboost_timer -lboost_system
|
||||
// export LD_LIBRARY_PATH=path_to_boost_lib
|
||||
|
||||
@ -52,4 +69,3 @@ int main(void)
|
||||
{
|
||||
test_map_pack_unpack();
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,20 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2013-2015 KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
// g++ -std=c++11 -O3 -g -Ipath_to_msgpack_src -Ipath_to_boost speed_test.cc -Lpath_to_boost_lib -lboost_timer -lboost_system
|
||||
// export LD_LIBRARY_PATH=path_to_boost_lib
|
||||
|
||||
@ -75,4 +92,3 @@ int main(void)
|
||||
{
|
||||
test_array_of_array();
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,20 @@
|
||||
// MessagePack for C++ example
|
||||
//
|
||||
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#include <msgpack.hpp>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
@ -13,7 +30,7 @@ public:
|
||||
|
||||
~Server() { }
|
||||
|
||||
typedef msgpack::unique_ptr<msgpack::zone> auto_zone;
|
||||
typedef msgpack::unique_ptr<msgpack::zone> unique_zone;
|
||||
|
||||
void socket_readable()
|
||||
{
|
||||
@ -37,7 +54,7 @@ public:
|
||||
msgpack::unpacked result;
|
||||
while (m_pac.next(&result)) {
|
||||
msgpack::object msg = result.get();
|
||||
auto_zone& life = result.zone();
|
||||
unique_zone& life = result.zone();
|
||||
process_message(msg, life);
|
||||
}
|
||||
|
||||
@ -47,7 +64,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
void process_message(msgpack::object msg, auto_zone& life)
|
||||
void process_message(msgpack::object msg, unique_zone&)
|
||||
{
|
||||
std::cout << "message reached: " << msg << std::endl;
|
||||
}
|
||||
@ -59,6 +76,7 @@ private:
|
||||
|
||||
|
||||
static void* run_server(void* arg)
|
||||
{
|
||||
try {
|
||||
Server* srv = reinterpret_cast<Server*>(arg);
|
||||
|
||||
@ -77,7 +95,7 @@ try {
|
||||
<< "unknown error" << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct fwriter {
|
||||
fwriter(int fd) : m_fp( fdopen(fd, "w") ) { }
|
||||
@ -130,4 +148,3 @@ int main(void)
|
||||
|
||||
pthread_join(thread, NULL);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user