Skip to content
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

Added std::shared_ptr and std::unique_ptr adaptors. #329

Merged
merged 1 commit into from
Aug 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/adaptor/cpp11/array.hpp
include/msgpack/adaptor/cpp11/array_char.hpp
include/msgpack/adaptor/cpp11/forward_list.hpp
include/msgpack/adaptor/cpp11/shared_ptr.hpp
include/msgpack/adaptor/cpp11/tuple.hpp
include/msgpack/adaptor/cpp11/unique_ptr.hpp
include/msgpack/adaptor/cpp11/unordered_map.hpp
include/msgpack/adaptor/cpp11/unordered_set.hpp
include/msgpack/adaptor/define.hpp
Expand Down
90 changes: 90 additions & 0 deletions include/msgpack/adaptor/cpp11/shared_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// MessagePack for C++ static resolution routine
//
// 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.
//

#ifndef MSGPACK_CPP11_SHARED_PTR_HPP
#define MSGPACK_CPP11_SHARED_PTR_HPP

#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"

#include <memory>

namespace msgpack {

/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond

namespace adaptor {

template <typename T>
struct as<std::shared_ptr<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
std::shared_ptr<T> operator()(msgpack::object const& o) const {
if(o.is_nil()) return nullptr;
return std::make_shared<T>(o.as<T>());
}
};

template <typename T>
struct convert<std::shared_ptr<T>> {
msgpack::object const& operator()(msgpack::object const& o, std::shared_ptr<T>& v) const {
if(o.is_nil()) v.reset();
else {
v = std::make_shared<T>();
msgpack::adaptor::convert<T>()(o, *v);
}
return o;
}
};

template <typename T>
struct pack<std::shared_ptr<T>> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::shared_ptr<T>& v) const {
if (v) o.pack(*v);
else o.pack_nil();
return o;
}
};

template <typename T>
struct object<std::shared_ptr<T> > {
void operator()(msgpack::object& o, const std::shared_ptr<T>& v) const {
if (v) msgpack::adaptor::object<T>()(o, *v);
else o.type = msgpack::type::NIL;
}
};

template <typename T>
struct object_with_zone<std::shared_ptr<T>> {
void operator()(msgpack::object::with_zone& o, const std::shared_ptr<T>& v) const {
if (v) msgpack::adaptor::object_with_zone<T>()(o, *v);
else o.type = msgpack::type::NIL;
}
};

} // namespace adaptor

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond

} // namespace msgpack

#endif // MSGPACK_CPP11_SHARED_PTR_HPP
90 changes: 90 additions & 0 deletions include/msgpack/adaptor/cpp11/unique_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// MessagePack for C++ static resolution routine
//
// 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.
//

#ifndef MSGPACK_CPP11_UNIQUE_PTR_HPP
#define MSGPACK_CPP11_UNIQUE_PTR_HPP

#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"

#include <memory>

namespace msgpack {

/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond

namespace adaptor {

template <typename T>
struct as<std::unique_ptr<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
std::unique_ptr<T> operator()(msgpack::object const& o) const {
if(o.is_nil()) return nullptr;
return std::unique_ptr<T>(new T(o.as<T>()));
}
};

template <typename T>
struct convert<std::unique_ptr<T>> {
msgpack::object const& operator()(msgpack::object const& o, std::unique_ptr<T>& v) const {
if(o.is_nil()) v.reset();
else {
v.reset(new T);
msgpack::adaptor::convert<T>()(o, *v);
}
return o;
}
};

template <typename T>
struct pack<std::unique_ptr<T>> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::unique_ptr<T>& v) const {
if (v) o.pack(*v);
else o.pack_nil();
return o;
}
};

template <typename T>
struct object<std::unique_ptr<T> > {
void operator()(msgpack::object& o, const std::unique_ptr<T>& v) const {
if (v) msgpack::adaptor::object<T>()(o, *v);
else o.type = msgpack::type::NIL;
}
};

template <typename T>
struct object_with_zone<std::unique_ptr<T>> {
void operator()(msgpack::object::with_zone& o, const std::unique_ptr<T>& v) const {
if (v) msgpack::adaptor::object_with_zone<T>()(o, *v);
else o.type = msgpack::type::NIL;
}
};

} // namespace adaptor

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond

} // namespace msgpack

#endif // MSGPACK_CPP11_UNIQUE_PTR_HPP
2 changes: 2 additions & 0 deletions include/msgpack/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include "adaptor/cpp11/array.hpp"
#include "adaptor/cpp11/array_char.hpp"
#include "adaptor/cpp11/forward_list.hpp"
#include "adaptor/cpp11/shared_ptr.hpp"
#include "adaptor/cpp11/tuple.hpp"
#include "adaptor/cpp11/unique_ptr.hpp"
#include "adaptor/cpp11/unordered_map.hpp"
#include "adaptor/cpp11/unordered_set.hpp"

Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ nobase_include_HEADERS += \
../include/msgpack/adaptor/cpp11/array.hpp \
../include/msgpack/adaptor/cpp11/array_char.hpp \
../include/msgpack/adaptor/cpp11/forward_list.hpp \
../include/msgpack/adaptor/cpp11/shared_ptr.hpp \
../include/msgpack/adaptor/cpp11/tuple.hpp \
../include/msgpack/adaptor/cpp11/unique_ptr.hpp \
../include/msgpack/adaptor/cpp11/unordered_map.hpp \
../include/msgpack/adaptor/cpp11/unordered_set.hpp \
../include/msgpack/adaptor/define.hpp \
Expand Down
32 changes: 17 additions & 15 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ INCLUDE_DIRECTORIES (
)

LIST (APPEND check_PROGRAMS
zone.cpp
pack_unpack.cpp
pack_unpack_c.cpp
streaming.cpp
streaming_c.cpp
object.cpp
object_with_zone.cpp
version.cpp
convert.cpp
buffer.cpp
cases.cpp
convert.cpp
fixint.cpp
fixint_c.cpp
msgpack_tuple.cpp
json.cpp
limit.cpp
msgpack_basic.cpp
msgpack_c.cpp
msgpack_container.cpp
msgpack_stream.cpp
msgpack_tuple.cpp
msgpack_vref.cpp
msgpack_c.cpp
reference.cpp
limit.cpp
json.cpp
object.cpp
object_with_zone.cpp
pack_unpack.cpp
pack_unpack_c.cpp
raw.cpp
reference.cpp
streaming.cpp
streaming_c.cpp
user_class.cpp
version.cpp
zone.cpp
)

IF (MSGPACK_BOOST)
Expand All @@ -44,9 +44,11 @@ ENDIF ()

IF (MSGPACK_CXX11)
LIST (APPEND check_PROGRAMS
iterator_cpp11.cpp
msgpack_cpp11.cpp
shared_ptr_cpp11.cpp
unique_ptr_cpp11.cpp
reference_cpp11.cpp
iterator_cpp11.cpp
)
ENDIF ()

Expand Down