-
Notifications
You must be signed in to change notification settings - Fork 918
Variant #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Variant #349
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cd9d6c5
Added msgpack::type::variant and msgpack::type::variant_ref that is b…
redboltz 23a040f
Fixed std::map::erase is ambiguous problem in osx, clang, libc++ comb…
redboltz 61eb4b1
Replaced boost::variant typedef with a class inheriting boost::variant.
redboltz 0609347
Fixed typo.
redboltz df5f84d
Added totally ordered.
redboltz 88ab7b6
Unified variant and variant_ref to basic_variant.
redboltz d17c70c
Added whitespaces at inheritance.
redboltz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| ADD_SUBDIRECTORY (c) | ||
| ADD_SUBDIRECTORY (cpp03) | ||
| ADD_SUBDIRECTORY (cpp11) | ||
| ADD_SUBDIRECTORY (boost) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| IF (MSGPACK_BOOST) | ||
| LIST (APPEND exec_PROGRAMS | ||
| msgpack_variant_capitalize.cpp | ||
| msgpack_variant_mapbased.cpp | ||
| ) | ||
| ENDIF () | ||
|
|
||
| FOREACH (source_file ${exec_PROGRAMS}) | ||
| GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE) | ||
| ADD_EXECUTABLE ( | ||
| ${source_file_we} | ||
| ${source_file} | ||
| ) | ||
| IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") | ||
| SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -Wno-mismatched-tags -g -O3") | ||
| ENDIF () | ||
| IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") | ||
| IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]") | ||
| STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") | ||
| ELSE () | ||
| SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX") | ||
| ENDIF () | ||
| ENDIF () | ||
| ENDFOREACH () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // 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 <string> | ||
| #include <sstream> | ||
| #include <iostream> | ||
| #include <algorithm> | ||
| #include <cctype> | ||
|
|
||
| #include <msgpack.hpp> | ||
|
|
||
| struct user { | ||
| std::string name; | ||
| int age; | ||
| std::string address; | ||
| MSGPACK_DEFINE(name, age, address); | ||
| }; | ||
|
|
||
| struct proc:boost::static_visitor<void> { | ||
| void operator()(std::string& v) const { | ||
| std::cout << " match std::string& v" << std::endl; | ||
| std::cout << " v: " << v << std::endl; | ||
| std::cout << " capitalize" << std::endl; | ||
| for (std::string::iterator it = v.begin(), end = v.end(); | ||
| it != end; | ||
| ++it) { | ||
| *it = std::toupper(*it); | ||
| } | ||
| } | ||
| void operator()(std::vector<msgpack::type::variant>& v) const { | ||
| std::cout << "match vector (msgpack::type::ARRAY)" << std::endl; | ||
| std::vector<msgpack::type::variant>::iterator it = v.begin(); | ||
| std::vector<msgpack::type::variant>::const_iterator end = v.end(); | ||
| for (; it != end; ++it) { | ||
| boost::apply_visitor(*this, *it); | ||
| } | ||
| } | ||
| template <typename T> | ||
| void operator()(T const&) const { | ||
| std::cout << " match others" << std::endl; | ||
| } | ||
| }; | ||
|
|
||
| void print(std::string const& buf) { | ||
| for (std::string::const_iterator it = buf.begin(), end = buf.end(); | ||
| it != end; | ||
| ++it) { | ||
| std::cout | ||
| << std::setw(2) | ||
| << std::hex | ||
| << std::setfill('0') | ||
| << (static_cast<int>(*it) & 0xff) | ||
| << ' '; | ||
| } | ||
| std::cout << std::dec << std::endl; | ||
| } | ||
|
|
||
|
|
||
| int main() { | ||
| std::stringstream ss1; | ||
| user u; | ||
| u.name = "Takatoshi Kondo"; | ||
| u.age = 42; | ||
| u.address = "Tokyo, JAPAN"; | ||
|
|
||
| std::cout << "Packing object." << std::endl; | ||
| msgpack::pack(ss1, u); | ||
| print(ss1.str()); | ||
|
|
||
| msgpack::unpacked unp1 = msgpack::unpack(ss1.str().data(), ss1.str().size()); | ||
| msgpack::object const& obj1 = unp1.get(); | ||
| std::cout << "Unpacked msgpack object." << std::endl; | ||
| std::cout << obj1 << std::endl; | ||
|
|
||
| msgpack::type::variant v = obj1.as<msgpack::type::variant>(); | ||
| std::cout << "Applying proc..." << std::endl; | ||
| boost::apply_visitor(proc(), v); | ||
|
|
||
| std::stringstream ss2; | ||
| std::cout << "Packing modified object." << std::endl; | ||
| msgpack::pack(ss2, v); | ||
| print(ss2.str()); | ||
|
|
||
| msgpack::unpacked unp2 = msgpack::unpack(ss2.str().data(), ss2.str().size()); | ||
| msgpack::object const& obj2 = unp2.get(); | ||
| std::cout << "Modified msgpack object." << std::endl; | ||
| std::cout << obj2 << std::endl; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // 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 <string> | ||
| #include <sstream> | ||
| #include <iostream> | ||
|
|
||
| #include <msgpack.hpp> | ||
|
|
||
| struct user { | ||
| std::string name; | ||
| int age; | ||
| std::string address; | ||
| MSGPACK_DEFINE_MAP(name, age, address); | ||
| }; | ||
|
|
||
| struct proc:boost::static_visitor<void> { | ||
| // msgpack::type::MAP is converted to std::multimap, not std::map. | ||
| void operator()(std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>& v) const { | ||
| std::cout << "match map" << std::endl; | ||
| std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::iterator it = v.begin(); | ||
| std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::iterator end = v.end(); | ||
| while(it != end) { | ||
| boost::string_ref const& key = boost::get<boost::string_ref>(it->first); | ||
| if (key == "name") { | ||
| boost::string_ref const& value = boost::get<boost::string_ref>(it->second); | ||
| if (value == "Takatoshi Kondo") { | ||
| // You can add values to msgpack::type::variant_ref. | ||
| v.insert( | ||
| std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::value_type( | ||
| std::string("role"), | ||
| std::string("msgpack-c committer") | ||
| ) | ||
| ); | ||
| } | ||
| ++it; | ||
| } | ||
| else if (key == "age") { | ||
| // You can remove key-value pair from msgpack::type::variant_ref | ||
|
|
||
| #if defined(MSGPACK_USE_CPP03) | ||
| v.erase(it++); | ||
| #else // defined(MSGPACK_USE_CPP03) | ||
| # if MSGPACK_LIB_STD_CXX | ||
| it = v.erase(std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::const_iterator(it)); | ||
| # else // MSGPACK_LIB_STD_CXX | ||
| it = v.erase(it); | ||
| # endif // MSGPACK_LIB_STD_CXX | ||
| #endif // defined(MSGPACK_USE_CPP03) | ||
| } | ||
| else if (key == "address") { | ||
| // When you want to append string | ||
| // "Tokyo" -> "Tokyo, JAPAN" | ||
| // Use msgpack::type::variant instead of msgpack::type::varinat_ref | ||
| // or do as follows: | ||
| boost::string_ref const& value = boost::get<boost::string_ref>(it->second); | ||
| it->second = std::string(&value.front(), value.size()) + ", JAPAN"; | ||
| ++it; | ||
| } | ||
| } | ||
| } | ||
| template <typename T> | ||
| void operator()(T const&) const { | ||
| std::cout << " match others" << std::endl; | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| std::stringstream ss; | ||
| user u; | ||
| u.name = "Takatoshi Kondo"; | ||
| u.age = 42; | ||
| u.address = "Tokyo"; | ||
| msgpack::pack(ss, u); | ||
|
|
||
| msgpack::unpacked unp = msgpack::unpack(ss.str().data(), ss.str().size()); | ||
| msgpack::object const& obj = unp.get(); | ||
| std::cout << "Unpacked msgpack object." << std::endl; | ||
| std::cout << obj << std::endl; | ||
| msgpack::type::variant_ref v = obj.as<msgpack::type::variant_ref>(); | ||
| std::cout << "Applying proc..." << std::endl; | ||
| boost::apply_visitor(proc(), v); | ||
| msgpack::zone z; | ||
| std::cout << "Applied msgpack object." << std::endl; | ||
| std::cout << msgpack::object(v, z) << std::endl; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: should be
msgpack::type::variant_refThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks for the review. I will fix it.