From 14eaefdceba3be88265a6992b677292ba8dcbf4c Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 19 Jul 2017 15:52:34 -0600 Subject: [PATCH] Make `front()` `back()` checked --- .../chaiscript/dispatchkit/bootstrap_stl.hpp | 69 ++++++++++++++++--- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 6710ed00..7f409a3a 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -335,9 +335,24 @@ namespace chaiscript template void back_insertion_sequence_type(const std::string &type, Module& m) { - typedef typename ContainerType::reference (ContainerType::*backptr)(); - - m.add(fun(static_cast(&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 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(&ContainerType::front)), "front"); - m.add(fun(static_cast(&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(&ContainerType::push_front)), [&]()->std::string{ @@ -577,11 +608,27 @@ namespace chaiscript { m.add(user_type(), 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(&VectorType::front)), "front"); - m.add(fun(static_cast(&VectorType::front)), "front"); back_insertion_sequence_type(type, m);