From 486e75f3ae63421352b3c82f5849c3ae9492c6c1 Mon Sep 17 00:00:00 2001 From: Jinhao Date: Mon, 18 Dec 2017 22:13:03 +0800 Subject: [PATCH] apply constexpr if --- include/nana/gui/detail/general_events.hpp | 51 ++++++++++++++++++++-- include/nana/gui/programming_interface.hpp | 28 ++++++++++++ source/gui/widgets/spinbox.cpp | 32 +++++++++++++- 3 files changed, 106 insertions(+), 5 deletions(-) diff --git a/include/nana/gui/detail/general_events.hpp b/include/nana/gui/detail/general_events.hpp index 5992f1a0..c776678a 100644 --- a/include/nana/gui/detail/general_events.hpp +++ b/include/nana/gui/detail/general_events.hpp @@ -1,7 +1,7 @@ /** * Definition of General Events * Nana C++ Library(http://www.nanapro.org) -* Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com) +* Copyright(C) 2003-2017 Jinhao(cnjinhao@hotmail.com) * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at @@ -133,11 +133,25 @@ namespace nana /// Creates an event handler at the beginning of event chain template event_handle connect_front(Function && fn) - { + { +#ifdef _nana_cxx_constexpr_if + if constexpr(std::is_invocable_v) + { + return _m_emplace(new docker{ this, fn, false }, true); + } + else if constexpr(std::is_invocable_v) + { + return _m_emplace(new docker{ this, [fn](arg_reference) { + fn(); + }, false }, true); + } +#else using prototype = typename std::remove_reference::type; return _m_emplace(new docker(this, factory::value>::build(std::forward(fn)), false), true); +#endif } +#ifndef _nana_cxx_constexpr_if /// It will not get called if stop_propagation() was called. event_handle connect(void (*fn)(arg_reference)) { @@ -145,13 +159,27 @@ namespace nana fn(arg); }); } +#endif /// It will not get called if stop_propagation() was called, because it is set at the end of the chain.. template event_handle connect(Function && fn) { +#ifdef _nana_cxx_constexpr_if + if constexpr(std::is_invocable_v) + { + return _m_emplace(new docker{ this, fn, false }, false); + } + else if constexpr(std::is_invocable_v) + { + return _m_emplace(new docker{ this, [fn](arg_reference){ + fn(); + }, false }, false); + } +#else using prototype = typename std::remove_reference::type; return _m_emplace(new docker(this, factory::value>::build(std::forward(fn)), false), false); +#endif } /// It will not get called if stop_propagation() was called. @@ -164,10 +192,22 @@ namespace nana /// It will get called because it is unignorable. template event_handle connect_unignorable(Function && fn, bool in_front = false) - { + { +#ifdef _nana_cxx_constexpr_if + if constexpr(std::is_invocable_v) + { + return _m_emplace(new docker{ this, fn, true }, in_front); + } + else if constexpr(std::is_invocable_v) + { + return _m_emplace(new docker{ this, [fn](arg_reference) { + fn(); + }, true }, in_front); + } +#else using prototype = typename std::remove_reference::type; - return _m_emplace(new docker(this, factory::value>::build(std::forward(fn)), true), in_front); +#endif } void emit(arg_reference& arg, window window_handle) @@ -210,6 +250,8 @@ namespace nana } } private: + +#ifndef _nana_cxx_constexpr_if template struct factory { @@ -385,6 +427,7 @@ namespace nana }; } }; +#endif }; struct arg_mouse diff --git a/include/nana/gui/programming_interface.hpp b/include/nana/gui/programming_interface.hpp index 8c0d85fe..b5d3e8c2 100644 --- a/include/nana/gui/programming_interface.hpp +++ b/include/nana/gui/programming_interface.hpp @@ -245,6 +245,19 @@ namespace API if (nullptr == general_evt) throw std::invalid_argument("API::events(): bad parameter window handle, no events object or invalid window handle."); +#ifdef _nana_cxx_constexpr_if + if constexpr(std::is_same_v) + { + return *general_evt; + } + else + { + auto * widget_evt = dynamic_cast(general_evt); + if (nullptr == widget_evt) + throw std::invalid_argument("API::events(): bad template parameter Widget, the widget type and window handle do not match."); + return *widget_evt; + } +#else if (std::is_same<::nana::general_events, event_type>::value) return *static_cast(general_evt); @@ -252,6 +265,7 @@ namespace API if (nullptr == widget_evt) throw std::invalid_argument("API::events(): bad template parameter Widget, the widget type and window handle do not match."); return *widget_evt; +#endif } template::value>::type* = nullptr> @@ -278,6 +292,19 @@ namespace API if (nullptr == wdg_colors) throw std::invalid_argument("API::scheme(): bad parameter window handle, no events object or invalid window handle."); +#ifdef _nana_cxx_constexpr_if + if constexpr(std::is_same<::nana::widget_geometrics, scheme_type>::value) + { + return *static_cast(wdg_colors); + } + else + { + auto * comp_wdg_colors = dynamic_cast(wdg_colors); + if (nullptr == comp_wdg_colors) + throw std::invalid_argument("API::scheme(): bad template parameter Widget, the widget type and window handle do not match."); + return *comp_wdg_colors; + } +#else if (std::is_same<::nana::widget_geometrics, scheme_type>::value) return *static_cast(wdg_colors); @@ -285,6 +312,7 @@ namespace API if (nullptr == comp_wdg_colors) throw std::invalid_argument("API::scheme(): bad template parameter Widget, the widget type and window handle do not match."); return *comp_wdg_colors; +#endif } point window_position(window); diff --git a/source/gui/widgets/spinbox.cpp b/source/gui/widgets/spinbox.cpp index 41b592f3..5bf2b342 100644 --- a/source/gui/widgets/spinbox.cpp +++ b/source/gui/widgets/spinbox.cpp @@ -82,10 +82,39 @@ namespace nana bool check_value(const std::string& str) const override { +#ifdef _nana_cxx_constexpr_if + auto i = str.c_str(); + if ('+' == *i || '-' == *i) + ++i; + + if constexpr(std::is_same::value) + { + for (; 0 != *i; ++i) + { + if (*i < '0' || '9' < *i) + return false; + } + } + else + { + bool dot = false; + for (; 0 != *i; ++i) + { + if (('.' == *i) && (!dot)) + { + dot = true; + continue; + } + + if (*i < '0' || '9' < *i) + return false; + } + } +#else if (str.empty()) return true; - auto size = str.size(); + auto const size = str.size(); std::size_t pos = 0; if (str[0] == '+' || str[0] == '-') pos = 1; @@ -115,6 +144,7 @@ namespace nana return false; } } +#endif return true; }