Skip to content

Commit 605642e

Browse files
committed
RObject Optimizations: refactor.
1 parent 8845e64 commit 605642e

File tree

7 files changed

+51
-49
lines changed

7 files changed

+51
-49
lines changed

ReflectionTemplateLib/access/inc/RObject.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ namespace rtl
4545
mutable std::any m_object;
4646
mutable detail::RObjectId m_objectId;
4747

48+
mutable const std::vector<traits::ConverterPair>* m_converters;
49+
4850
RObject(const RObject&) = default;
49-
RObject(std::any&& pObject, Cloner&& pCloner, const detail::RObjectId& pRObjectId);
51+
RObject(std::any&& pObject, Cloner&& pCloner, const detail::RObjectId& pRObjectId,
52+
const std::vector<traits::ConverterPair>& pConverters);
5053

5154
static std::atomic<std::size_t>& getInstanceCounter();
5255

56+
std::size_t getConverterIndex(const std::size_t pToTypeId) const;
57+
5358
template<rtl::alloc _allocOn, detail::EntityKind _entityKind>
5459
std::pair<rtl::error, RObject> createCopy() const;
5560

ReflectionTemplateLib/access/inc/RObject.hpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,25 @@
2424

2525
namespace rtl
2626
{
27-
inline RObject::RObject(std::any&& pObject, Cloner&& pCloner, const detail::RObjectId& pRObjectId)
27+
inline RObject::RObject(std::any&& pObject, Cloner&& pCloner, const detail::RObjectId& pRObjectId,
28+
const std::vector<traits::ConverterPair>& pConverters)
2829
: m_getClone(std::forward<Cloner>(pCloner))
2930
, m_object(std::forward<std::any>(pObject))
3031
, m_objectId(pRObjectId)
32+
, m_converters(&pConverters)
3133
{ }
3234

3335
inline RObject::RObject(RObject&& pOther) noexcept
3436
: m_object(std::move(pOther.m_object))
3537
, m_getClone(std::move(pOther.m_getClone))
3638
, m_objectId(pOther.m_objectId)
39+
, m_converters(pOther.m_converters)
3740
{
3841
// Explicitly clear moved-from source
3942
pOther.m_object.reset();
4043
pOther.m_objectId.reset();
4144
pOther.m_getClone = nullptr;
45+
pOther.m_converters = nullptr;
4246
}
4347

4448
inline std::atomic<std::size_t>& RObject::getInstanceCounter()
@@ -47,6 +51,20 @@ namespace rtl
4751
return instanceCounter;
4852
}
4953

54+
55+
inline std::size_t RObject::getConverterIndex(const std::size_t pToTypeId) const
56+
{
57+
if (m_objectId.m_containsAs != detail::EntityKind::None) {
58+
for (std::size_t index = 0; index < m_converters->size(); index++) {
59+
if ((*m_converters)[index].first == pToTypeId) {
60+
return index;
61+
}
62+
}
63+
}
64+
return index_none;
65+
}
66+
67+
5068
template<class T>
5169
inline bool RObject::canViewAs() const
5270
{
@@ -57,7 +75,7 @@ namespace rtl
5775
}
5876
}
5977
const auto& typeId = detail::TypeId<T>::get();
60-
return (m_objectId.m_typeId == typeId || m_objectId.getConverterIndex(typeId) != index_none);
78+
return (m_objectId.m_typeId == typeId || getConverterIndex(typeId) != index_none);
6179
}
6280
}
6381

@@ -66,7 +84,7 @@ namespace rtl
6684
inline std::optional<rtl::view<T>> RObject::performConversion(const std::size_t pIndex) const
6785
{
6886
detail::EntityKind newKind = detail::EntityKind::None;
69-
const traits::Converter& convert = m_objectId.m_converters[pIndex].second;
87+
const traits::Converter& convert = (*m_converters)[pIndex].second;
7088
const std::any& viewObj = convert(m_object, m_objectId.m_containsAs, newKind);
7189
const T* viewRef = detail::RObjExtractor::getPointer<T>(viewObj, newKind);
7290

@@ -128,7 +146,7 @@ namespace rtl
128146
}
129147
else
130148
{
131-
const std::size_t index = m_objectId.getConverterIndex(asTypeId);
149+
const std::size_t index = getConverterIndex(asTypeId);
132150
if (index != index_none) {
133151
return performConversion<T>(index);
134152
}

ReflectionTemplateLib/detail/inc/RObjectBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace rtl::detail
2626
template <class T>
2727
static Cloner buildCloner();
2828

29+
template <class T>
30+
static const std::vector<traits::ConverterPair>& getConverters();
31+
2932
public:
3033

3134
RObjectBuilder() = delete;

ReflectionTemplateLib/detail/inc/RObjectBuilder.hpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ namespace rtl::detail {
5454
}
5555
}
5656

57+
template<class T>
58+
inline const std::vector<traits::ConverterPair>& RObjectBuilder::getConverters()
59+
{
60+
// extract wrapper info.
61+
using _W = traits::std_wrapper<traits::raw_t<T>>;
62+
// extract Un-Qualified raw type.
63+
using _T = traits::raw_t<std::conditional_t<(_W::type == Wrapper::None), T, typename _W::value_type>>;
64+
65+
return rtl::detail::ReflectCast<_T>::getConversions();
66+
}
67+
5768

5869
template<class T, rtl::alloc _allocOn>
5970
inline RObject RObjectBuilder::build(T&& pVal, const bool pIsConstCastSafe)
@@ -66,27 +77,30 @@ namespace rtl::detail {
6677
static_assert(isRawPointer, "Invalid 'alloc' specified for non-pointer-type 'T'");
6778
_T* objPtr = static_cast<_T*>(pVal);
6879
const RObjectId& robjId = RObjectId::create<std::unique_ptr<_T>, _allocOn>(pIsConstCastSafe);
69-
return RObject(std::any(RObjectUPtr<_T>(std::unique_ptr<_T>(objPtr))), buildCloner<_T>(), robjId);
80+
const std::vector<traits::ConverterPair>& conversions = getConverters<std::unique_ptr<_T>>();
81+
return RObject(std::any(RObjectUPtr<_T>(std::unique_ptr<_T>(objPtr))), buildCloner<_T>(), robjId, conversions);
7082
}
7183
else if constexpr (_allocOn == alloc::Stack)
7284
{
7385
if constexpr (isRawPointer)
7486
{
7587
const RObjectId& robjId = RObjectId::create<T, _allocOn>(pIsConstCastSafe);
76-
return RObject(std::any(static_cast<const _T*>(pVal)), buildCloner<_T>(), robjId);
88+
const std::vector<traits::ConverterPair>& conversions = getConverters<T>();
89+
return RObject(std::any(static_cast<const _T*>(pVal)), buildCloner<_T>(), robjId, conversions);
7790
}
7891
else
7992
{
8093
const RObjectId& robjId = RObjectId::create<T, _allocOn>(pIsConstCastSafe);
94+
const std::vector<traits::ConverterPair>& conversions = getConverters<T>();
8195
if constexpr (traits::std_wrapper<_T>::type == Wrapper::Unique)
8296
{
8397
using U = traits::std_wrapper<_T>::value_type;
84-
return RObject(std::any(RObjectUPtr<U>(std::move(pVal))), buildCloner<_T>(), robjId);
98+
return RObject(std::any(RObjectUPtr<U>(std::move(pVal))), buildCloner<_T>(), robjId, conversions);
8599
}
86100
else
87101
{
88102
static_assert(std::is_copy_constructible_v<_T>, "T must be copy-constructible (std::any requires this).");
89-
return RObject(std::any(std::forward<T>(pVal)), buildCloner<_T>(), robjId);
103+
return RObject(std::any(std::forward<T>(pVal)), buildCloner<_T>(), robjId, conversions);
90104
}
91105
}
92106
}

ReflectionTemplateLib/detail/inc/RObjectId.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ namespace rtl::detail
3535

3636
private:
3737

38-
static std::vector<traits::ConverterPair> m_conversions;
39-
4038
mutable bool m_isWrappingConst;
4139
mutable bool m_isConstCastSafe;
4240

@@ -47,8 +45,6 @@ namespace rtl::detail
4745
mutable std::size_t m_typeId;
4846
mutable std::size_t m_wrapperTypeId;
4947

50-
const std::vector<traits::ConverterPair>& m_converters;
51-
5248
RObjectId(RObjectId&&) = default;
5349
RObjectId(const RObjectId&) = default;
5450
RObjectId& operator=(RObjectId&&) = delete;
@@ -63,20 +59,18 @@ namespace rtl::detail
6359
, m_containsAs(EntityKind::None)
6460
, m_typeId(TypeId<>::None)
6561
, m_wrapperTypeId(TypeId<>::None)
66-
, m_converters(m_conversions)
6762
{ }
6863

6964

7065
RObjectId(alloc pAllocOn, bool pIsConstCastSafe, Wrapper pWrapperType, bool pIsStoredConst, EntityKind pContainsAs,
71-
std::size_t pTypeId, const std::vector<traits::ConverterPair>& pConverters, std::size_t pWrapperTypeId)
66+
std::size_t pTypeId, std::size_t pWrapperTypeId)
7267
: m_isWrappingConst(pIsStoredConst)
7368
, m_isConstCastSafe(pIsConstCastSafe)
7469
, m_allocatedOn(pAllocOn)
7570
, m_wrapperType(pWrapperType)
7671
, m_containsAs(pContainsAs)
7772
, m_typeId(pTypeId)
7873
, m_wrapperTypeId(pWrapperTypeId)
79-
, m_converters(pConverters)
8074
{ }
8175

8276

@@ -92,19 +86,6 @@ namespace rtl::detail
9286
}
9387

9488

95-
inline std::size_t getConverterIndex(const std::size_t pToTypeId) const
96-
{
97-
if (m_containsAs != EntityKind::None) {
98-
for (std::size_t index = 0; index < m_converters.size(); index++) {
99-
if (m_converters[index].first == pToTypeId) {
100-
return index;
101-
}
102-
}
103-
}
104-
return index_none;
105-
}
106-
107-
10889
template<class T>
10990
static constexpr EntityKind getEntityKind()
11091
{
@@ -136,10 +117,9 @@ namespace rtl::detail
136117

137118
const std::size_t wrapperId = _W::id();
138119
const std::size_t typeId = rtl::detail::TypeId<_T>::get();
139-
const auto& conversions = rtl::detail::ReflectCast<_T>::getConversions();
140120
const bool isWrappingConst = (_W::type != Wrapper::None && traits::is_const_v<typename _W::value_type>);
141121
return RObjectId(_allocOn, pIsConstCastSafe, _W::type,
142-
isWrappingConst, containedAs, typeId, conversions, wrapperId);
122+
isWrappingConst, containedAs, typeId, wrapperId);
143123
}
144124
};
145125
}

ReflectionTemplateLib/detail/src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
set(LOCAL_SOURCES
33
"${CMAKE_CURRENT_LIST_DIR}/CxxReflection.cpp"
44
"${CMAKE_CURRENT_LIST_DIR}/FunctorId.cpp"
5-
"${CMAKE_CURRENT_LIST_DIR}/RObjectId.cpp"
65
"${CMAKE_CURRENT_LIST_DIR}/ReflectCast.cpp"
76
"${CMAKE_CURRENT_LIST_DIR}/RObjectConverters_string.cpp"
87
)

ReflectionTemplateLib/detail/src/RObjectId.cpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)