mirror of
https://github.com/ToruNiina/toml11.git
synced 2024-11-28 23:21:04 +00:00
add toml::resize func that resize only resizable container
This commit is contained in:
parent
a506dfd3c1
commit
58c12a69be
@ -1,5 +1,6 @@
|
||||
set(TEST_NAMES
|
||||
test_traits
|
||||
test_utility
|
||||
test_value
|
||||
test_to_toml
|
||||
test_from_toml
|
||||
|
64
tests/test_utility.cpp
Normal file
64
tests/test_utility.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
#define BOOST_TEST_MODULE "test_acceptor"
|
||||
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#else
|
||||
#define BOOST_TEST_NO_LIB
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#endif
|
||||
#include <toml/utility.hpp>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_resize)
|
||||
{
|
||||
{
|
||||
typedef std::vector<int> resizable_type;
|
||||
typedef std::array<int,1> non_resizable_type;
|
||||
BOOST_CHECK(toml::detail::has_resize_method<resizable_type>::value);
|
||||
BOOST_CHECK(not toml::detail::has_resize_method<non_resizable_type>::value);
|
||||
}
|
||||
|
||||
{
|
||||
bool thrown = false;
|
||||
std::vector<int> v;
|
||||
try
|
||||
{
|
||||
toml::resize(v, 10);
|
||||
}
|
||||
catch(std::exception& ex)
|
||||
{
|
||||
thrown = true;
|
||||
}
|
||||
BOOST_CHECK(not thrown);
|
||||
BOOST_CHECK_EQUAL(v.size(), 10);
|
||||
}
|
||||
|
||||
{
|
||||
bool thrown = false;
|
||||
std::array<int, 15> a;
|
||||
try
|
||||
{
|
||||
toml::resize(a, 10);
|
||||
}
|
||||
catch(std::exception& ex)
|
||||
{
|
||||
thrown = true;
|
||||
}
|
||||
BOOST_CHECK(not thrown);
|
||||
BOOST_CHECK_EQUAL(a.size(), 15);
|
||||
}
|
||||
|
||||
{
|
||||
bool thrown = false;
|
||||
std::array<int, 15> a;
|
||||
try
|
||||
{
|
||||
toml::resize(a, 20);
|
||||
}
|
||||
catch(std::exception& ex)
|
||||
{
|
||||
thrown = true;
|
||||
}
|
||||
BOOST_CHECK(thrown);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#ifndef TOML11_UTILITY
|
||||
#define TOML11_UTILITY
|
||||
#include "traits.hpp"
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
|
||||
@ -12,5 +13,31 @@ inline std::unique_ptr<T> make_unique(Ts&& ... args)
|
||||
return std::unique_ptr<T>(new T(std::forward<Ts>(args)...));
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
inline void resize_impl(T& container, std::size_t N, std::true_type)
|
||||
{
|
||||
container.resize(N);
|
||||
return ;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void resize_impl(T& container, std::size_t N, std::false_type)
|
||||
{
|
||||
if(container.size() >= N) return;
|
||||
else throw std::invalid_argument("not resizable type");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void resize(T& container, std::size_t N)
|
||||
{
|
||||
if(container.size() == N) return;
|
||||
else return detail::resize_impl(container, N, detail::has_resize_method<T>());
|
||||
}
|
||||
|
||||
}// toml
|
||||
#endif // TOML11_UTILITY
|
||||
|
Loading…
Reference in New Issue
Block a user