From 59103b5a228d43f7ce572704e033b70096ecdc36 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 29 Mar 2015 21:58:14 -0600 Subject: [PATCH] Apply some IIFE to reduce copies --- .../dispatchkit/type_conversions.hpp | 41 ++++++++++--------- .../chaiscript/language/chaiscript_parser.hpp | 40 ++++++++++-------- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/include/chaiscript/dispatchkit/type_conversions.hpp b/include/chaiscript/dispatchkit/type_conversions.hpp index 13eb680..dc29efe 100644 --- a/include/chaiscript/dispatchkit/type_conversions.hpp +++ b/include/chaiscript/dispatchkit/type_conversions.hpp @@ -119,24 +119,27 @@ namespace chaiscript // Dynamic cast out the contained boxed value, which we know is the type we want if (t_from.is_const()) { - std::shared_ptr data - = std::dynamic_pointer_cast(detail::Cast_Helper >::cast(t_from, nullptr)); - if (!data) - { - throw std::bad_cast(); - } - - return Boxed_Value(data); + return Boxed_Value( + [&]()->std::shared_ptr{ + if (auto data = std::dynamic_pointer_cast(detail::Cast_Helper >::cast(t_from, nullptr))) + { + return data; + } else { + throw std::bad_cast(); + } + }() + ); } else { - std::shared_ptr data - = std::dynamic_pointer_cast(detail::Cast_Helper >::cast(t_from, nullptr)); - - if (!data) - { - throw std::bad_cast(); - } - - return Boxed_Value(data); + return Boxed_Value( + [&]()->std::shared_ptr{ + if (auto data = std::dynamic_pointer_cast(detail::Cast_Helper >::cast(t_from, nullptr))) + { + return data; + } else { + throw std::bad_cast(); + } + }() + ); } } else { // Pull the reference out of the contained boxed value, which we know is the type we want @@ -439,9 +442,7 @@ namespace chaiscript static_assert(std::is_convertible::value, "Types are not automatically convertible"); auto func = [](const Boxed_Value &t_bv) -> Boxed_Value { // not even attempting to call boxed_cast so that we don't get caught in some call recursion - auto &&from = detail::Cast_Helper::cast(t_bv, nullptr); - To to(from); - return chaiscript::Boxed_Value(to); + return chaiscript::Boxed_Value(To(detail::Cast_Helper::cast(t_bv, nullptr))); }; return std::make_shared>(user_type(), user_type(), func); diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 8e5bc0d..e16c452 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -617,14 +617,16 @@ namespace chaiscript } - size_t size = sizeof(int) * 8; - - if (longlong_) - { - size = sizeof(int64_t) * 8; - } else if (long_) { - size = sizeof(long) * 8; - } + const size_t size = [&](){ + if (longlong_) + { + return sizeof(int64_t) * 8; + } else if (long_) { + return sizeof(long) * 8; + } else { + return sizeof(int) * 8; + } + }(); if ( (u >> (size - 1)) > 0) { @@ -794,17 +796,19 @@ namespace chaiscript return Id_(); } else { const auto start = m_input_pos; - const int prev_col = m_col; - const int prev_line = m_line; + const auto prev_col = m_col; + const auto prev_line = m_line; if (Id_()) { - std::string match; - if (*start == '`') { - //Id Literal - match = std::string(start+1, m_input_pos-1); - } else { - match = std::string(start, m_input_pos); - } - m_match_stack.push_back(std::make_shared(std::move(match), m_filename, prev_line, prev_col, m_line, m_col)); + m_match_stack.push_back(std::make_shared( + [&](){ + if (*start == '`') { + //Id Literal + return std::string(start+1, m_input_pos-1); + } else { + return std::string(start, m_input_pos); + } + }(), + m_filename, prev_line, prev_col, m_line, m_col)); return true; } else { return false;