mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 16:57:04 +08:00
Make front() back() checked
This commit is contained in:
parent
f03659c865
commit
14eaefdceb
@ -335,9 +335,24 @@ namespace chaiscript
|
||||
template<typename ContainerType>
|
||||
void back_insertion_sequence_type(const std::string &type, Module& m)
|
||||
{
|
||||
typedef typename ContainerType::reference (ContainerType::*backptr)();
|
||||
|
||||
m.add(fun(static_cast<backptr>(&ContainerType::back)), "back");
|
||||
m.add(fun([](ContainerType &container)->decltype(auto){
|
||||
if (container.empty()) {
|
||||
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 &);
|
||||
@ -380,13 +395,29 @@ namespace chaiscript
|
||||
template<typename ContainerType>
|
||||
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::*pop_ptr)();
|
||||
|
||||
m.add(fun(static_cast<front_ptr>(&ContainerType::front)), "front");
|
||||
m.add(fun(static_cast<const_front_ptr>(&ContainerType::front)), "front");
|
||||
m.add(fun([](ContainerType &container)->decltype(auto){
|
||||
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)),
|
||||
[&]()->std::string{
|
||||
@ -577,11 +608,27 @@ namespace chaiscript
|
||||
{
|
||||
m.add(user_type<VectorType>(), type);
|
||||
|
||||
typedef typename VectorType::reference (VectorType::*frontptr)();
|
||||
typedef typename VectorType::const_reference (VectorType::*constfrontptr)() const;
|
||||
m.add(fun([](VectorType &container)->decltype(auto){
|
||||
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user