1111
1212#pragma once
1313
14+ #include < utility>
15+
1416#include " RObject.hpp"
1517#include " RObjectUPtr.h"
1618#include " RObjectBuilder.h"
@@ -33,37 +35,43 @@ namespace rtl::detail {
3335 }
3436
3537 template <class T >
36- inline RObjectBuilder::Cloner RObjectBuilder::buildCloner ()
38+ inline const RObjectBuilder::Cloner& RObjectBuilder::buildCloner ()
3739 {
3840 using W = traits::std_wrapper<T>;
3941 using _T = std::conditional_t <W::type == Wrapper::None, T, typename W::value_type>;
4042
4143 if constexpr (std::is_copy_constructible_v<_T>)
4244 {
43- return [](error& pError, const RObject& pOther, alloc pAllocOn)-> RObject
45+ static const Cloner cloner = [](const RObject& pOther, alloc pAllocOn) -> Return
4446 {
4547 const auto & srcObj = pOther.view <_T>()->get ();
46- pError = error::None;
47- if (pAllocOn == alloc::Stack) {
48- return RObjectBuilder::template build<_T, alloc::Stack>(_T (srcObj), true );
49- }
50- else if (pAllocOn == alloc::Heap) {
51- return RObjectBuilder::template build<_T*, alloc::Heap>(new _T (srcObj), true );
48+ switch (pAllocOn)
49+ {
50+ case alloc::Stack:
51+ return { error::None,
52+ RObjectBuilder::template build<_T, alloc::Stack>(_T (srcObj), true ) };
53+
54+ case alloc::Heap:
55+ return { error::None,
56+ RObjectBuilder::template build<_T*, alloc::Heap>(new _T (srcObj), true ) };
57+
58+ default :
59+ return { error::EmptyRObject, RObject{} };
5260 }
53- return RObject{ }; // dead code. compiler warning ommited.
5461 };
62+ return cloner;
5563 }
56- else
64+ else
5765 {
58- return [](error& pError, const RObject& pOther, alloc pAllocOn)-> RObject
59- {
60- pError = error::TypeNotCopyConstructible;
61- return RObject{ };
66+ static const Cloner cloner = [](const RObject&, alloc) -> Return {
67+ return { error::TypeNotCopyConstructible, RObject{} };
6268 };
69+ return cloner;
6370 }
6471 }
6572
6673
74+
6775 template <class T , rtl::alloc _allocOn>
6876 inline RObject RObjectBuilder::build (T&& pVal, const bool pIsConstCastSafe)
6977 {
@@ -73,32 +81,37 @@ namespace rtl::detail {
7381 if constexpr (_allocOn == alloc::Heap)
7482 {
7583 static_assert (isRawPointer, " Invalid 'alloc' specified for non-pointer-type 'T'" );
76- _T* objPtr = static_cast <_T*>(pVal);
77- const RObjectId robjId = RObjectId::create< std::unique_ptr<_T>, _allocOn>(pIsConstCastSafe);
78- const std::vector<traits::ConverterPair>& conversions = getConverters<std::unique_ptr< _T>>();
79- return RObject ( std::any (RObjectUPtr<_T>( std::unique_ptr<_T>(objPtr))), buildCloner<_T >(), robjId, conversions );
84+ return RObject (RObjectId::create<std::unique_ptr<_T>, _allocOn>(pIsConstCastSafe),
85+ std::any (RObjectUPtr<_T>( std::unique_ptr<_T>( static_cast <_T*>(pVal)))),
86+ buildCloner< _T>(),
87+ getConverters< std::unique_ptr<_T>>());
8088 }
8189 else if constexpr (_allocOn == alloc::Stack)
8290 {
8391 if constexpr (isRawPointer)
8492 {
85- const RObjectId robjId = RObjectId::create<T, _allocOn>(pIsConstCastSafe);
86- const std::vector<traits::ConverterPair>& conversions = getConverters<T>();
87- return RObject (std::any (static_cast <const _T*>(pVal)), buildCloner<_T>(), robjId, conversions);
93+ return RObject (RObjectId::create<T, _allocOn>(pIsConstCastSafe),
94+ std::any (static_cast <const _T*>(pVal)),
95+ buildCloner<_T>(),
96+ getConverters<T>());
8897 }
8998 else
9099 {
91- const RObjectId robjId = RObjectId::create<T, _allocOn>(pIsConstCastSafe);
92- const std::vector<traits::ConverterPair>& conversions = getConverters<T>();
93100 if constexpr (traits::std_wrapper<_T>::type == Wrapper::Unique)
94101 {
95102 using U = traits::std_wrapper<_T>::value_type;
96- return RObject (std::any (RObjectUPtr<U>(std::move (pVal))), buildCloner<_T>(), robjId, conversions);
103+ return RObject (RObjectId::create<T, _allocOn>(pIsConstCastSafe),
104+ std::any (RObjectUPtr<U>(std::move (pVal))),
105+ buildCloner<_T>(),
106+ getConverters<T>());
97107 }
98108 else
99109 {
100110 static_assert (std::is_copy_constructible_v<_T>, " T must be copy-constructible (std::any requires this)." );
101- return RObject (std::any (std::forward<T>(pVal)), buildCloner<_T>(), robjId, conversions);
111+ return RObject (RObjectId::create<T, _allocOn>(pIsConstCastSafe),
112+ std::any (std::forward<T>(pVal)),
113+ buildCloner<_T>(),
114+ getConverters<T>());
102115 }
103116 }
104117 }
0 commit comments