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_
#define CHAISCRIPT_THREADING_HPP_
#include <unordered_map>
#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.

View File

@ -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<std::reference_wrapper<Result> >()).get();
} else {
return &(ob.get().cast<std::reference_wrapper<const Result> >()).get();
}
return static_cast<const Result *>(throw_if_null(ob.get_const_ptr()));
} else {
if (!ob.get_type_info().is_const())
{
return (ob.get().cast<std::shared_ptr<Result> >()).get();
} else {
return (ob.get().cast<std::shared_ptr<const Result> >()).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<std::reference_wrapper<Result> >()).get();
return static_cast<Result *>(throw_if_null(ob.get_ptr()));
} 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 <string>
#include <vector>
#include <type_traits>
#include "boxed_number.hpp"
#include "boxed_value.hpp"
@ -33,15 +34,19 @@ namespace chaiscript
template<typename Ret>
struct Handle_Return
{
static Boxed_Value handle(const Ret &r)
{
return const_var(r);
}
static Boxed_Value handle(Ret &&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::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>