Added GenericHandles to SHADE_Managed

This commit is contained in:
Kah Wei 2022-10-28 19:00:17 +08:00
parent 7d6af884a4
commit 199897adb4
7 changed files with 165 additions and 4 deletions

View File

@ -195,6 +195,11 @@ namespace SHADE
template<typename T> template<typename T>
inline bool operator==(const Handle<T>& rhs) const noexcept; inline bool operator==(const Handle<T>& rhs) const noexcept;
/*-----------------------------------------------------------------------------*/
/* Query Functions */
/*-----------------------------------------------------------------------------*/
inline SHResourceLibraryBase* GetLibrary() const;
protected: protected:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
@ -206,6 +211,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
template<typename T> template<typename T>
friend class Handle; friend class Handle;
friend class Convert;
}; };
/// <summary> /// <summary>

View File

@ -96,6 +96,11 @@ namespace SHADE
return id.Raw == rhs.id.Raw && library == static_cast<void*>(rhs.library); return id.Raw == rhs.id.Raw && library == static_cast<void*>(rhs.library);
} }
SHResourceLibraryBase* SHADE::Handle<void>::GetLibrary() const
{
return library;
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* ISelfHandle<T> - Constructors */ /* ISelfHandle<T> - Constructors */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -70,13 +70,13 @@ namespace SHADE
} }
template<typename T> template<typename T>
SparseSet<T>::reference SparseSet<T>::at(index_type idx) typename SparseSet<T>::reference SparseSet<T>::at(index_type idx)
{ {
return const_cast<reference>(static_cast<const SparseSet<T>&>(*this).at(idx)); return const_cast<reference>(static_cast<const SparseSet<T>&>(*this).at(idx));
} }
template<typename T> template<typename T>
SparseSet<T>::const_reference SparseSet<T>::at(index_type idx) const typename SparseSet<T>::const_reference SparseSet<T>::at(index_type idx) const
{ {
// Range Check // Range Check
if (idx >= sparseArray.size() || !contains(idx)) if (idx >= sparseArray.size() || !contains(idx))
@ -84,7 +84,7 @@ namespace SHADE
return denseArray[sparseArray[idx]]; return denseArray[sparseArray[idx]];
} }
template<typename T> template<typename T>
SparseSet<T>::size_type SparseSet<T>::size() const typename SparseSet<T>::size_type SparseSet<T>::size() const
{ {
return denseArray.size(); return denseArray.size();
} }
@ -105,7 +105,7 @@ namespace SHADE
} }
template<typename T> template<typename T>
template<typename ...Args> template<typename ...Args>
SparseSet<T>::reference SparseSet<T>::insert(index_type idx, Args && ...args) typename SparseSet<T>::reference SparseSet<T>::insert(index_type idx, Args && ...args)
{ {
// We need to resize the array // We need to resize the array
if (idx >= sparseArray.size()) if (idx >= sparseArray.size())

View File

@ -0,0 +1,47 @@
/************************************************************************************//*!
\file Renderable.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2022
\brief Contains the definition of the functions of the managed Renderable class.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#include "SHpch.h"
#include "GenericHandle.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
GenericHandle::GenericHandle(Handle<void> handle)
: id { handle.GetId().Raw }
, library { reinterpret_cast<void*>(handle.GetLibrary()) }
{}
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
System::UInt64 GenericHandle::Id::get()
{
return id;
}
System::IntPtr GenericHandle::Library::get()
{
return library;
}
/*---------------------------------------------------------------------------------*/
/* Overloaded Operators */
/*---------------------------------------------------------------------------------*/
GenericHandle::operator bool()
{
return library.ToPointer() != nullptr && id != System::UInt64::MaxValue;
}
}

View File

@ -0,0 +1,68 @@
/************************************************************************************//*!
\file Handle.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2022
\brief Contains the definition of the managed GenericHandle struct.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// External Dependencies
#include "Resource/SHHandle.h"
namespace SHADE
{
/// <summary>
/// Managed version of the generic Handle<void>.
/// </summary>
public value struct GenericHandle
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructs a GenericHandle for a native generic Handle<void>.
/// </summary>
/// <param name="handle">Handle to create a GenericHandle from.</param>
explicit GenericHandle(Handle<void> handle);
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// The internal ID of the handle.
/// </summary>
property System::UInt64 Id
{
System::UInt64 get();
}
/// <summary>
/// The library that the handle was issued by.
/// </summary>
property System::IntPtr Library
{
System::IntPtr get();
}
/*-----------------------------------------------------------------------------*/
/* Overloaded Operators */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Converts to true if this is a valid Handle.
/// </summary>
inline operator bool();
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
System::UInt64 id;
System::IntPtr library;
};
}

View File

@ -84,4 +84,20 @@ namespace SHADE
{ {
return msclr::interop::marshal_as<System::String^>(str); return msclr::interop::marshal_as<System::String^>(str);
} }
/*---------------------------------------------------------------------------------*/
/* Handle Conversions */
/*---------------------------------------------------------------------------------*/
Handle<void> Convert::ToNative(GenericHandle handle)
{
Handle<void> nativeHandle;
nativeHandle.id.Raw = handle.Id;
nativeHandle.library = reinterpret_cast<SHResourceLibraryBase*>(handle.Library.ToPointer());
return nativeHandle;
}
GenericHandle Convert::ToCLI(Handle<void> handle)
{
return GenericHandle(handle);
}
} }

View File

@ -20,6 +20,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Math/Vector/SHVec3.h" #include "Math/Vector/SHVec3.h"
#include "Math/SHQuaternion.h" #include "Math/SHQuaternion.h"
#include "Math/SHRay.h" #include "Math/SHRay.h"
#include "Resource/SHHandle.h"
// Project Includes // Project Includes
#include "Engine/Entity.hxx" #include "Engine/Entity.hxx"
@ -27,6 +28,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Math/Vector3.hxx" #include "Math/Vector3.hxx"
#include "Math/Quaternion.hxx" #include "Math/Quaternion.hxx"
#include "Math/Ray.hxx" #include "Math/Ray.hxx"
#include "Engine/GenericHandle.hxx"
namespace SHADE namespace SHADE
{ {
@ -118,6 +120,23 @@ namespace SHADE
/// <param name="str">The native std::string to convert from.</param> /// <param name="str">The native std::string to convert from.</param>
/// <returns>Managed copy of a native std::string.</returns> /// <returns>Managed copy of a native std::string.</returns>
static System::String^ ToCLI(const std::string& str); static System::String^ ToCLI(const std::string& str);
/*-----------------------------------------------------------------------------*/
/* Handle Conversions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Converts from a managed GenericHandle to a Handle<void>.
/// </summary>
/// <param name="handle">GenericHandle to convert from.</param>
/// <returns>Native generic Handle.</returns>
static Handle<void> ToNative(GenericHandle handle);
/// <summary>
/// Converts from a native generic Handle<void> to a managed GenericHandle.
/// </summary>
/// <param name="handle">The native handle to convert.</param>
/// <returns>Managed copy of the native Handle.</returns>
static GenericHandle ToCLI(Handle<void> handle);
}; };
/// <summary> /// <summary>