1
0
forked from boostorg/core

Update NVP implementation, tests, docs

This commit is contained in:
Glen Fernandes
2019-09-15 21:12:09 -04:00
parent f52dec58c2
commit 7cc1047ab7
3 changed files with 1 additions and 144 deletions

View File

@ -48,14 +48,8 @@ public:
T& value() const noexcept;
const T& const_value() const noexcept;
template<class A>
void serialize(A& archive, unsigned);
};
template<class T>
struct is_nvp;
template<class T>
const nvp<T> make_nvp(const char* name, T& value) noexcept;
@ -79,19 +73,7 @@ const nvp<T> make_nvp(const char* name, T& value) noexcept;
[[`T& value() const noexcept;`]
[Returns a reference to the value.]]
[[`const T& const_value() const noexcept;`]
[Returns a reference to the value.]]
[[`template<class A> void serialize(A& archive, unsigned);`]
[Calls `archive.operator<<(const_value())` if `A::is_saving` is a true type,
otherwise `archive.operator>>(value())`.]]]
[endsect]
[section Traits]
[variablelist
[[`template<class T> struct is_nvp;`]
[Provides static constant `value` equal to true if `T` is an NVP type,
otherwise false.]]]
[Returns a reference to the value.]]]
[endsect]

View File

@ -12,28 +12,6 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/config.hpp>
namespace boost {
namespace detail {
template<bool V>
struct nvp_bool {
typedef bool value_type;
typedef nvp_bool type;
BOOST_STATIC_CONSTEXPR bool value = V;
BOOST_CONSTEXPR operator bool() const BOOST_NOEXCEPT {
return V;
}
BOOST_CONSTEXPR bool operator()() const BOOST_NOEXCEPT {
return V;
}
};
template<bool V>
BOOST_CONSTEXPR_OR_CONST bool nvp_bool<V>::value;
} /* detail */
template<class T>
class nvp {
@ -54,46 +32,11 @@ public:
return *v_;
}
template<class A>
void serialize(A& a, unsigned) {
archive(a, detail::nvp_bool<A::is_saving::value>());
}
private:
template<class A>
void archive(A& a, detail::nvp_bool<true>) const {
a.operator<<(*v_);
}
template<class A>
void archive(A& a, detail::nvp_bool<false>) {
a.operator>>(*v_);
}
const char* n_;
T* v_;
};
template<class>
struct is_nvp
: detail::nvp_bool<false> { };
template<class T>
struct is_nvp<nvp<T> >
: detail::nvp_bool<true> { };
template<class T>
struct is_nvp<const nvp<T> >
: detail::nvp_bool<true> { };
template<class T>
struct is_nvp<volatile nvp<T> >
: detail::nvp_bool<true> { };
template<class T>
struct is_nvp<const volatile nvp<T> >
: detail::nvp_bool<true> { };
template<class T>
inline const nvp<T>
make_nvp(const char* n, T& v) BOOST_NOEXCEPT

View File

@ -8,44 +8,6 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/core/nvp.hpp>
#include <boost/core/lightweight_test_trait.hpp>
class saver {
public:
struct is_saving {
static const bool value = true;
};
explicit saver(int value)
: value_(value) { }
int get() const {
return value_;
}
void operator<<(int value) {
value_ = value;
}
private:
int value_;
};
class loader {
public:
struct is_saving {
static const bool value = false;
};
explicit loader(int value)
: value_(value) { }
void operator>>(int& value) {
value = value_;
}
private:
int value_;
};
void test()
{
const char* n = "name";
@ -56,33 +18,6 @@ void test()
BOOST_TEST_EQ(&p.value(), &v);
}
void test_serialize()
{
int v = 1;
boost::nvp<int> p("name", v);
saver s(0);
p.serialize(s, unsigned());
BOOST_TEST_EQ(s.get(), 1);
}
void test_deserialize()
{
int v = 1;
boost::nvp<int> p("name", v);
loader l(5);
p.serialize(l, unsigned());
BOOST_TEST_EQ(p.value(), 5);
}
void test_trait()
{
BOOST_TEST_TRAIT_FALSE((boost::is_nvp<int>));
BOOST_TEST_TRAIT_TRUE((boost::is_nvp<boost::nvp<int> >));
BOOST_TEST_TRAIT_TRUE((boost::is_nvp<const boost::nvp<int> >));
BOOST_TEST_TRAIT_TRUE((boost::is_nvp<volatile boost::nvp<int> >));
BOOST_TEST_TRAIT_TRUE((boost::is_nvp<const volatile boost::nvp<int> >));
}
void test_factory()
{
const char* n = "name";
@ -96,9 +31,6 @@ void test_factory()
int main()
{
test();
test_serialize();
test_deserialize();
test_trait();
test_factory();
return boost::report_errors();
}