From 79181fe41ef0e01fc215f12a0ee178f711019e70 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 7 Apr 2015 10:23:43 -0600 Subject: [PATCH] Reduce copies of UDTs --- include/chaiscript/chaiscript_threading.hpp | 3 +++ .../dispatchkit/boxed_cast_helper.hpp | 22 +++++-------------- .../chaiscript/dispatchkit/handle_return.hpp | 17 +++++++++----- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/include/chaiscript/chaiscript_threading.hpp b/include/chaiscript/chaiscript_threading.hpp index 15b4a64..6bac83a 100644 --- a/include/chaiscript/chaiscript_threading.hpp +++ b/include/chaiscript/chaiscript_threading.hpp @@ -7,6 +7,7 @@ #ifndef CHAISCRIPT_THREADING_HPP_ #define CHAISCRIPT_THREADING_HPP_ + #include #ifndef CHAISCRIPT_NO_THREADS @@ -16,6 +17,8 @@ #pragma message ("ChaiScript is compiling without thread safety.") #endif +#include "chaiscript_defines.hpp" + /// \file /// /// This file contains code necessary for thread support in ChaiScript. diff --git a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp index a71c7cd..69cfc7c 100644 --- a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp @@ -65,21 +65,11 @@ namespace chaiscript typedef const Result * Result_Type; static Result_Type cast(const Boxed_Value &ob, const Type_Conversions *) { - if (ob.is_ref()) + if (ob.get_type_info().bare_equal_type_info(typeid(Result))) { - if (!ob.get_type_info().is_const()) - { - return &(ob.get().cast >()).get(); - } else { - return &(ob.get().cast >()).get(); - } + return static_cast(throw_if_null(ob.get_const_ptr())); } else { - if (!ob.get_type_info().is_const()) - { - return (ob.get().cast >()).get(); - } else { - return (ob.get().cast >()).get(); - } + throw chaiscript::detail::exception::bad_any_cast(); } } }; @@ -91,11 +81,11 @@ namespace chaiscript typedef Result * Result_Type; static Result_Type cast(const Boxed_Value &ob, const Type_Conversions *) { - if (ob.is_ref()) + if (!ob.get_type_info().is_const() && ob.get_type_info().bare_equal_type_info(typeid(Result))) { - return &(ob.get().cast >()).get(); + return static_cast(throw_if_null(ob.get_ptr())); } else { - return (ob.get().cast >()).get(); + throw chaiscript::detail::exception::bad_any_cast(); } } }; diff --git a/include/chaiscript/dispatchkit/handle_return.hpp b/include/chaiscript/dispatchkit/handle_return.hpp index a9e1164..9e7c0c2 100644 --- a/include/chaiscript/dispatchkit/handle_return.hpp +++ b/include/chaiscript/dispatchkit/handle_return.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "boxed_number.hpp" #include "boxed_value.hpp" @@ -33,15 +34,19 @@ namespace chaiscript template struct Handle_Return { - static Boxed_Value handle(const Ret &r) - { - return const_var(r); - } - - static Boxed_Value handle(Ret &&r) + template::type>::value>::type> + static Boxed_Value handle(T r) { return Boxed_Value(std::move(r)); } + + template::type>::value>::type> + static Boxed_Value handle(T &&r) + { + return Boxed_Value(std::make_shared(std::forward(r))); + } }; template