Reduce copies of UDTs

This commit is contained in:
Jason Turner 2015-04-07 10:23:43 -06:00
parent 962bdf4b3c
commit 79181fe41e
3 changed files with 20 additions and 22 deletions

View File

@ -7,6 +7,7 @@
#ifndef CHAISCRIPT_THREADING_HPP_ #ifndef CHAISCRIPT_THREADING_HPP_
#define CHAISCRIPT_THREADING_HPP_ #define CHAISCRIPT_THREADING_HPP_
#include <unordered_map> #include <unordered_map>
#ifndef CHAISCRIPT_NO_THREADS #ifndef CHAISCRIPT_NO_THREADS
@ -16,6 +17,8 @@
#pragma message ("ChaiScript is compiling without thread safety.") #pragma message ("ChaiScript is compiling without thread safety.")
#endif #endif
#include "chaiscript_defines.hpp"
/// \file /// \file
/// ///
/// This file contains code necessary for thread support in ChaiScript. /// This file contains code necessary for thread support in ChaiScript.

View File

@ -65,21 +65,11 @@ namespace chaiscript
typedef const Result * Result_Type; typedef const Result * Result_Type;
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions *) 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 static_cast<const Result *>(throw_if_null(ob.get_const_ptr()));
{
return &(ob.get().cast<std::reference_wrapper<Result> >()).get();
} else {
return &(ob.get().cast<std::reference_wrapper<const Result> >()).get();
}
} else { } else {
if (!ob.get_type_info().is_const()) throw chaiscript::detail::exception::bad_any_cast();
{
return (ob.get().cast<std::shared_ptr<Result> >()).get();
} else {
return (ob.get().cast<std::shared_ptr<const Result> >()).get();
}
} }
} }
}; };
@ -91,11 +81,11 @@ namespace chaiscript
typedef Result * Result_Type; typedef Result * Result_Type;
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions *) 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<std::reference_wrapper<Result> >()).get(); return static_cast<Result *>(throw_if_null(ob.get_ptr()));
} else { } else {
return (ob.get().cast<std::shared_ptr<Result> >()).get(); throw chaiscript::detail::exception::bad_any_cast();
} }
} }
}; };

View File

@ -12,6 +12,7 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <vector> #include <vector>
#include <type_traits>
#include "boxed_number.hpp" #include "boxed_number.hpp"
#include "boxed_value.hpp" #include "boxed_value.hpp"
@ -33,15 +34,19 @@ namespace chaiscript
template<typename Ret> template<typename Ret>
struct Handle_Return struct Handle_Return
{ {
static Boxed_Value handle(const Ret &r) template<typename T,
{ typename = typename std::enable_if<std::is_pod<typename std::decay<T>::type>::value>::type>
return const_var(r); static Boxed_Value handle(T r)
}
static Boxed_Value handle(Ret &&r)
{ {
return Boxed_Value(std::move(r)); return Boxed_Value(std::move(r));
} }
template<typename T,
typename = typename std::enable_if<!std::is_pod<typename std::decay<T>::type>::value>::type>
static Boxed_Value handle(T &&r)
{
return Boxed_Value(std::make_shared<T>(std::forward<T>(r)));
}
}; };
template<typename Ret> template<typename Ret>