mirror of
https://github.com/boostorg/core.git
synced 2025-07-31 21:34:42 +02:00
Update NVP implementation, tests, docs
This commit is contained in:
20
doc/nvp.qbk
20
doc/nvp.qbk
@@ -48,14 +48,8 @@ public:
|
|||||||
T& value() const noexcept;
|
T& value() const noexcept;
|
||||||
|
|
||||||
const T& const_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>
|
template<class T>
|
||||||
const nvp<T> make_nvp(const char* name, T& value) noexcept;
|
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;`]
|
[[`T& value() const noexcept;`]
|
||||||
[Returns a reference to the value.]]
|
[Returns a reference to the value.]]
|
||||||
[[`const T& const_value() const noexcept;`]
|
[[`const T& const_value() const noexcept;`]
|
||||||
[Returns a reference to the value.]]
|
[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.]]]
|
|
||||||
|
|
||||||
[endsect]
|
[endsect]
|
||||||
|
|
||||||
|
@@ -12,28 +12,6 @@ Distributed under the Boost Software License, Version 1.0.
|
|||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
|
|
||||||
namespace boost {
|
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>
|
template<class T>
|
||||||
class nvp {
|
class nvp {
|
||||||
@@ -54,46 +32,11 @@ public:
|
|||||||
return *v_;
|
return *v_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class A>
|
|
||||||
void serialize(A& a, unsigned) {
|
|
||||||
archive(a, detail::nvp_bool<A::is_saving::value>());
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
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_;
|
const char* n_;
|
||||||
T* v_;
|
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>
|
template<class T>
|
||||||
inline const nvp<T>
|
inline const nvp<T>
|
||||||
make_nvp(const char* n, T& v) BOOST_NOEXCEPT
|
make_nvp(const char* n, T& v) BOOST_NOEXCEPT
|
||||||
|
@@ -8,44 +8,6 @@ Distributed under the Boost Software License, Version 1.0.
|
|||||||
#include <boost/core/nvp.hpp>
|
#include <boost/core/nvp.hpp>
|
||||||
#include <boost/core/lightweight_test_trait.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()
|
void test()
|
||||||
{
|
{
|
||||||
const char* n = "name";
|
const char* n = "name";
|
||||||
@@ -56,33 +18,6 @@ void test()
|
|||||||
BOOST_TEST_EQ(&p.value(), &v);
|
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()
|
void test_factory()
|
||||||
{
|
{
|
||||||
const char* n = "name";
|
const char* n = "name";
|
||||||
@@ -96,9 +31,6 @@ void test_factory()
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
test();
|
test();
|
||||||
test_serialize();
|
|
||||||
test_deserialize();
|
|
||||||
test_trait();
|
|
||||||
test_factory();
|
test_factory();
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user