update NativeObject & NativePtr

This commit is contained in:
Nomango 2020-08-04 02:19:08 +08:00
parent cbd9060277
commit 69f298211c
5 changed files with 83 additions and 31 deletions

View File

@ -166,6 +166,7 @@
<ClCompile Include="..\..\src\kiwano\render\Font.cpp" />
<ClCompile Include="..\..\src\kiwano\render\Frame.cpp" />
<ClCompile Include="..\..\src\kiwano\render\FrameSequence.cpp" />
<ClCompile Include="..\..\src\kiwano\render\NativeObject.cpp" />
<ClCompile Include="..\..\src\kiwano\render\Shape.cpp" />
<ClCompile Include="..\..\src\kiwano\render\ShapeMaker.cpp" />
<ClCompile Include="..\..\src\kiwano\render\GifImage.cpp" />

View File

@ -587,6 +587,9 @@
<ClCompile Include="..\..\src\kiwano\render\TextStyle.cpp">
<Filter>render</Filter>
</ClCompile>
<ClCompile Include="..\..\src\kiwano\render\NativeObject.cpp">
<Filter>render</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="suppress_warning.ruleset" />

View File

@ -34,7 +34,7 @@ public:
{
if (object)
{
ComPtr<IUnknown> ptr = object->GetNativePointer<ComPtr<IUnknown>>();
ComPtr<IUnknown> ptr = object->GetNativePointer<IUnknown>();
if (ptr)
{
ComPtr<_Ty> native;
@ -61,7 +61,7 @@ public:
{
if (object)
{
object->SetNativePointer(com_ptr);
object->ResetNativePointer(com_ptr.Get());
}
}

View File

@ -0,0 +1,47 @@
#include "NativeObject.h"
namespace kiwano
{
NativeObjectBase::NativeObjectBase()
: native_pointer_(nullptr)
{
}
bool NativeObjectBase::IsValid() const
{
return native_pointer_ != nullptr;
}
void NativeObjectBase::ResetNativePointer(void* native_pointer)
{
native_pointer_ = native_pointer;
}
//
// NativeObject for Windows
//
#if defined(KGE_PLATFORM_WINDOWS)
NativeObject::~NativeObject()
{
ResetNativePointer();
}
void NativeObject::ResetNativePointer(void* native_pointer)
{
if (native_pointer_)
{
static_cast<IUnknown*>(native_pointer_)->Release();
native_pointer_ = nullptr;
}
if (native_pointer)
{
native_pointer_ = native_pointer;
static_cast<IUnknown*>(native_pointer_)->AddRef();
}
}
#endif
}

View File

@ -39,54 +39,55 @@ KGE_DECLARE_SMART_PTR(NativeObject);
* \~chinese
* @brief
*/
class KGE_API NativeObject : public ObjectBase
class KGE_API NativeObjectBase : public ObjectBase
{
public:
virtual bool IsValid() const;
NativeObjectBase();
Any GetNativePointer() const;
bool IsValid() const override;
void* GetNativePointer() const;
template <typename _NativeTy>
_NativeTy GetNativePointer() const;
_NativeTy* GetNativePointer() const;
void SetNativePointer(const Any& native_pointer);
virtual void ResetNativePointer(void* native_pointer = nullptr);
void ResetNativePointer();
private:
Any native_pointer_;
protected:
void* native_pointer_;
};
#if defined(KGE_PLATFORM_WINDOWS)
class KGE_API NativeObject : public NativeObjectBase
{
public:
virtual ~NativeObject();
void ResetNativePointer(void* native_pointer = nullptr) override;
};
#else
typedef NativeObjectBase NativeObject;
#endif
/** @} */
inline bool NativeObject::IsValid() const
{
return native_pointer_.HasValue();
}
inline Any NativeObject::GetNativePointer() const
inline void* NativeObjectBase::GetNativePointer() const
{
return native_pointer_;
}
template <typename _NativeTy>
inline _NativeTy NativeObject::GetNativePointer() const
inline _NativeTy* NativeObjectBase::GetNativePointer() const
{
if (native_pointer_.HasValue())
if (native_pointer_ != nullptr)
{
return native_pointer_.Cast<_NativeTy>();
return static_cast<_NativeTy*>(native_pointer_);
}
return _NativeTy();
}
inline void NativeObject::SetNativePointer(const Any& native_pointer)
{
native_pointer_ = native_pointer;
}
inline void NativeObject::ResetNativePointer()
{
native_pointer_.Clear();
return nullptr;
}
} // namespace kiwano