diff --git a/Import/Vlpp.h b/Import/Vlpp.h index 5644bebe..32fee2f6 100644 --- a/Import/Vlpp.h +++ b/Import/Vlpp.h @@ -2200,10 +2200,7 @@ Interfaces virtual IEnumerator* CreateEnumerator() const = 0; /// Get the underlying collection object. - /// - /// The underlying collection object. - /// It could returns nullptr when returns . - /// + /// The underlying collection object, could be nullptr. virtual const Object* GetCollectionObject() const = 0; /// Get the associated collection reference. diff --git a/Import/VlppReflection.h b/Import/VlppReflection.h index 4489c6ed..6deab133 100644 --- a/Import/VlppReflection.h +++ b/Import/VlppReflection.h @@ -2957,6 +2957,10 @@ Collections /// The enumerator. virtual Ptr CreateEnumerator() = 0; + /// Get the underlying collection object, which is boxed to be this interface. + /// The underlying collection object, could be nullptr. + virtual const Object* GetCollectionObject() { return nullptr; } + /// Create an enumerable from another lazy list. /// The created enumerable. /// The lazy list to wrap. @@ -3377,6 +3381,10 @@ Collections /// The reference to the value. It will crash if the key does not exist. /// The key to find. virtual Value Get(const Value& key) = 0; + + /// Get the underlying collection object, which is boxed to be this interface. + /// The underlying collection object, could be nullptr. + virtual const Object* GetCollectionObject() { return nullptr; } }; /// @@ -5833,6 +5841,25 @@ Collection Wrappers wrapperPointer->CreateEnumerator() ); } + + const Object* GetCollectionObject()override + { + if constexpr (std::is_same_v::Type*, T>) + { + if (wrapperPointer->GetCollectionReference() == this) + { + return wrapperPointer; + } + else + { + return nullptr; + } + } + else + { + return nullptr; + } + } }; template @@ -6029,6 +6056,25 @@ Collection Wrappers ValueType result = wrapperPointer->Get(item); return BoxValue(result); } + + const Object* GetCollectionObject()override + { + if constexpr (std::is_same_v::Type*, T>) + { + if (wrapperPointer->GetCollectionReference() == this) + { + return wrapperPointer; + } + else + { + return nullptr; + } + } + else + { + return nullptr; + } + } }; #define KEY_VALUE_TYPE typename ValueReadonlyDictionaryWrapper::KeyValueType @@ -6197,25 +6243,41 @@ Containers return BoxValue(result, td); } - template - Unboxed UnboxCollection(const Ptr& colref) + template + Unboxed UnboxCollection(const Ptr& colref) { - auto collection = new TCollection(); - auto lazyList = GetLazyList(colref); - collections::CopyFrom(*collection, lazyList); - return { collection, true }; + using TCollection = std::remove_const_t; + if (auto colobj = dynamic_cast(const_cast(colref->GetCollectionObject()))) + { + return { colobj,false }; + } + else + { + auto collection = new TCollection(); + auto lazyList = GetLazyList(colref); + collections::CopyFrom(*collection, lazyList); + return { collection, true }; + } } - template - Unboxed UnboxDictionary(const Ptr& colref) + template + Unboxed UnboxDictionary(const Ptr& colref) { - auto collection = new TCollection(); - auto lazyList = GetLazyList< - typename TCollection::KeyContainer::ElementType, - typename TCollection::ValueContainer::ElementType + using TCollection = std::remove_const_t; + if (auto colobj = dynamic_cast(const_cast(colref->GetCollectionObject()))) + { + return { colobj,false }; + } + else + { + auto collection = new TCollection(); + auto lazyList = GetLazyList< + typename TCollection::KeyContainer::ElementType, + typename TCollection::ValueContainer::ElementType >(colref); - collections::CopyFrom(*collection, lazyList); - return { collection, true }; + collections::CopyFrom(*collection, lazyList); + return { collection, true }; + } } template