Make front() back() checked

This commit is contained in:
Jason Turner 2017-07-19 15:52:34 -06:00
parent f03659c865
commit 14eaefdceb

View File

@ -335,9 +335,24 @@ namespace chaiscript
template<typename ContainerType> template<typename ContainerType>
void back_insertion_sequence_type(const std::string &type, Module& m) void back_insertion_sequence_type(const std::string &type, Module& m)
{ {
typedef typename ContainerType::reference (ContainerType::*backptr)(); m.add(fun([](ContainerType &container)->decltype(auto){
if (container.empty()) {
m.add(fun(static_cast<backptr>(&ContainerType::back)), "back"); throw std::range_error("Container empty");
} else {
return (container.back());
}
}
)
, "back");
m.add(fun([](const ContainerType &container)->decltype(auto){
if (container.empty()) {
throw std::range_error("Container empty");
} else {
return (container.back());
}
}
)
, "back");
typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &); typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &);
@ -380,13 +395,29 @@ namespace chaiscript
template<typename ContainerType> template<typename ContainerType>
void front_insertion_sequence_type(const std::string &type, Module& m) void front_insertion_sequence_type(const std::string &type, Module& m)
{ {
typedef typename ContainerType::reference (ContainerType::*front_ptr)();
typedef typename ContainerType::const_reference (ContainerType::*const_front_ptr)() const;
typedef void (ContainerType::*push_ptr)(typename ContainerType::const_reference); typedef void (ContainerType::*push_ptr)(typename ContainerType::const_reference);
typedef void (ContainerType::*pop_ptr)(); typedef void (ContainerType::*pop_ptr)();
m.add(fun(static_cast<front_ptr>(&ContainerType::front)), "front"); m.add(fun([](ContainerType &container)->decltype(auto){
m.add(fun(static_cast<const_front_ptr>(&ContainerType::front)), "front"); if (container.empty()) {
throw std::range_error("Container empty");
} else {
return (container.front());
}
}
)
, "front");
m.add(fun([](const ContainerType &container)->decltype(auto){
if (container.empty()) {
throw std::range_error("Container empty");
} else {
return (container.front());
}
}
)
, "front");
m.add(fun(static_cast<push_ptr>(&ContainerType::push_front)), m.add(fun(static_cast<push_ptr>(&ContainerType::push_front)),
[&]()->std::string{ [&]()->std::string{
@ -577,11 +608,27 @@ namespace chaiscript
{ {
m.add(user_type<VectorType>(), type); m.add(user_type<VectorType>(), type);
typedef typename VectorType::reference (VectorType::*frontptr)(); m.add(fun([](VectorType &container)->decltype(auto){
typedef typename VectorType::const_reference (VectorType::*constfrontptr)() const; if (container.empty()) {
throw std::range_error("Container empty");
} else {
return (container.front());
}
}
)
, "front");
m.add(fun([](const VectorType &container)->decltype(auto){
if (container.empty()) {
throw std::range_error("Container empty");
} else {
return (container.front());
}
}
)
, "front");
m.add(fun(static_cast<frontptr>(&VectorType::front)), "front");
m.add(fun(static_cast<constfrontptr>(&VectorType::front)), "front");
back_insertion_sequence_type<VectorType>(type, m); back_insertion_sequence_type<VectorType>(type, m);