Apply some IIFE to reduce copies

This commit is contained in:
Jason Turner
2015-03-29 21:58:14 -06:00
parent d514fa3346
commit 59103b5a22
2 changed files with 43 additions and 38 deletions

View File

@@ -119,24 +119,27 @@ namespace chaiscript
// Dynamic cast out the contained boxed value, which we know is the type we want // Dynamic cast out the contained boxed value, which we know is the type we want
if (t_from.is_const()) if (t_from.is_const())
{ {
std::shared_ptr<const To> data return Boxed_Value(
= std::dynamic_pointer_cast<const To>(detail::Cast_Helper<std::shared_ptr<const From> >::cast(t_from, nullptr)); [&]()->std::shared_ptr<const To>{
if (!data) if (auto data = std::dynamic_pointer_cast<const To>(detail::Cast_Helper<std::shared_ptr<const From> >::cast(t_from, nullptr)))
{ {
throw std::bad_cast(); return data;
} } else {
throw std::bad_cast();
return Boxed_Value(data); }
}()
);
} else { } else {
std::shared_ptr<To> data return Boxed_Value(
= std::dynamic_pointer_cast<To>(detail::Cast_Helper<std::shared_ptr<From> >::cast(t_from, nullptr)); [&]()->std::shared_ptr<To>{
if (auto data = std::dynamic_pointer_cast<To>(detail::Cast_Helper<std::shared_ptr<From> >::cast(t_from, nullptr)))
if (!data) {
{ return data;
throw std::bad_cast(); } else {
} throw std::bad_cast();
}
return Boxed_Value(data); }()
);
} }
} else { } else {
// Pull the reference out of the contained boxed value, which we know is the type we want // 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<From, To>::value, "Types are not automatically convertible"); static_assert(std::is_convertible<From, To>::value, "Types are not automatically convertible");
auto func = [](const Boxed_Value &t_bv) -> Boxed_Value { 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 // not even attempting to call boxed_cast so that we don't get caught in some call recursion
auto &&from = detail::Cast_Helper<From>::cast(t_bv, nullptr); return chaiscript::Boxed_Value(To(detail::Cast_Helper<From>::cast(t_bv, nullptr)));
To to(from);
return chaiscript::Boxed_Value(to);
}; };
return std::make_shared<detail::Type_Conversion_Impl<decltype(func)>>(user_type<From>(), user_type<To>(), func); return std::make_shared<detail::Type_Conversion_Impl<decltype(func)>>(user_type<From>(), user_type<To>(), func);

View File

@@ -617,14 +617,16 @@ namespace chaiscript
} }
size_t size = sizeof(int) * 8; const size_t size = [&](){
if (longlong_)
if (longlong_) {
{ return sizeof(int64_t) * 8;
size = sizeof(int64_t) * 8; } else if (long_) {
} else if (long_) { return sizeof(long) * 8;
size = sizeof(long) * 8; } else {
} return sizeof(int) * 8;
}
}();
if ( (u >> (size - 1)) > 0) if ( (u >> (size - 1)) > 0)
{ {
@@ -794,17 +796,19 @@ namespace chaiscript
return Id_(); return Id_();
} else { } else {
const auto start = m_input_pos; const auto start = m_input_pos;
const int prev_col = m_col; const auto prev_col = m_col;
const int prev_line = m_line; const auto prev_line = m_line;
if (Id_()) { if (Id_()) {
std::string match; m_match_stack.push_back(std::make_shared<eval::Id_AST_Node>(
if (*start == '`') { [&](){
//Id Literal if (*start == '`') {
match = std::string(start+1, m_input_pos-1); //Id Literal
} else { return std::string(start+1, m_input_pos-1);
match = std::string(start, m_input_pos); } else {
} return std::string(start, m_input_pos);
m_match_stack.push_back(std::make_shared<eval::Id_AST_Node>(std::move(match), m_filename, prev_line, prev_col, m_line, m_col)); }
}(),
m_filename, prev_line, prev_col, m_line, m_col));
return true; return true;
} else { } else {
return false; return false;