Added CallbackAction and CallbackEvent

This commit is contained in:
Kah Wei 2022-09-20 16:23:03 +08:00
parent 8772ce0cea
commit 9896c5c913
5 changed files with 262 additions and 1 deletions

View File

@ -0,0 +1,48 @@
/************************************************************************************//*!
\file CallbackAction.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 20, 2022
\brief Contains the definition of the functions for the CallbackAction managed
class.
Note: This file is written in C++17/CLI.
Copyright (C) 2021 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.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "CallbackAction.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
generic<typename T1>
CallbackAction<T1>::CallbackAction(System::Reflection::MethodInfo^ method)
: CallbackAction(method, nullptr)
{}
generic<typename T1>
CallbackAction<T1>::CallbackAction(System::Reflection::MethodInfo^ method, System::Object^ obj)
: method { method }
, object { obj }
{
paramArray = gcnew cli::array<System::Object^>(1);
}
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
generic<typename T1>
void CallbackAction<T1>::Invoke(T1 param1)
{
// Set parameters
paramArray[0] = safe_cast<System::Object^>(param1);
// Invoke
method->Invoke(object, paramArray);
}
}

View File

@ -0,0 +1,57 @@
/************************************************************************************//*!
\file CallbackAction.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 20, 2021
\brief Contains the definition of the managed CallbackAction 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.
*//*************************************************************************************/
#pragma once
namespace SHADE
{
/// <summary>
/// Holds a function and their associated object for invoking later.
/// </summary>
/// <typeparam name="T1">Type of the first parameter.</typeparam>
generic<typename T1>
public ref class CallbackAction
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructs a CallbackAction with the specified static method.
/// </summary>
/// <param name="method">MethodInfo representing the method to call.</param>
/// <param name="obj">Object to call the method on. Set null if static.</param>
CallbackAction(System::Reflection::MethodInfo^ method);
/// <summary>
/// Constructs a CallbackAction with the specified instance method and object
/// which will invoke the method.
/// </summary>
/// <param name="method">MethodInfo representing the method to call.</param>
/// <param name="obj">Object to call the method on.</param>
CallbackAction(System::Reflection::MethodInfo^ method, System::Object^ obj);
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Invokes this action with the specified parameters.
/// </summary>
/// <param name="param1"></param>
void Invoke(T1 param1);
private:
System::Reflection::MethodInfo^ method;
cli::array<System::Object^>^ paramArray;
System::Object^ object;
};
}

View File

@ -0,0 +1,71 @@
/************************************************************************************//*!
\file CallbackEvent.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 20, 2022
\brief Contains the definition of the functions for the CallbackEvent managed
class.
Note: This file is written in C++17/CLI.
Copyright (C) 2021 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.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "CallbackEvent.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
generic<typename T1>
CallbackEvent<T1>::CallbackEvent()
{
actions = gcnew System::Collections::Generic::List<CallbackAction<T1> ^>();
}
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
generic<typename T1>
System::Collections::Generic::IEnumerable<CallbackAction<T1>^>^ CallbackEvent<T1>::Actions::get()
{
return actions;
}
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
generic<typename T1>
void CallbackEvent<T1>::AddAction(CallbackAction<T1>^ action)
{
actions->Add(action);
}
generic<typename T1>
void CallbackEvent<T1>::AddAction(System::Reflection::MethodInfo^ method)
{
actions->Add(gcnew CallbackAction<T1>(method));
}
generic<typename T1>
void CallbackEvent<T1>::AddAction(System::Reflection::MethodInfo^ method, System::Object^ object)
{
actions->Add(gcnew CallbackAction<T1>(method, object));
}
generic<typename T1>
void CallbackEvent<T1>::RemoveAction(CallbackAction<T1>^ action)
{
actions->Remove(action);
}
generic<typename T1>
void CallbackEvent<T1>::Invoke(T1 param1)
{
for each (CallbackAction<T1>^ action in actions)
{
action->Invoke(param1);
}
}
}

View File

@ -0,0 +1,85 @@
/************************************************************************************//*!
\file CallbackEvent.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 20, 2021
\brief Contains the definition of the managed CallbackEvent 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.
*//*************************************************************************************/
#pragma once
#include "CallbackAction.hxx"
namespace SHADE
{
/// <summary>
/// Holds a set of CallbackActions that can be invoked together at a later time.
/// </summary>
/// <typeparam name="T1">Type of the first parameter.</typeparam>
generic<typename T1>
public ref class CallbackEvent
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Default Constructor
/// </summary>
CallbackEvent();
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Read only access to the list of Actions
/// </summary>
property System::Collections::Generic::IEnumerable<CallbackAction<T1>^>^ Actions
{
System::Collections::Generic::IEnumerable<CallbackAction<T1>^>^ get();
}
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds the specified CallbackAction to the CallbackEvent.
/// </summary>
/// <param name="action">CallbackAction to add.</param>
void AddAction(CallbackAction<T1>^ action);
/// <summary>
/// Adds a specified static method as a CallbackAction to the CallbackEvent.
/// </summary>
/// <param name="method">Method to add.</param>
void AddAction(System::Reflection::MethodInfo^ method);
/// <summary>
/// Adds a specified instance method and associated object as a CallbackAction to
/// the CallbackEvent.
/// </summary>
/// <param name="method">Method to add.</param>
/// <param name="object">Object that will call the specified method.</param>
void AddAction(System::Reflection::MethodInfo^ method, System::Object^ object);
/// <summary>
/// Removes the specified CallbackAction from the CallbackEvent.
/// </summary>
/// <param name="">CallbackAction to remove.</param>
void RemoveAction(CallbackAction<T1>^ action);
/// <summary>
/// Invokes all CallbackActions registered with this CallbackEvent with the
/// specified parameters.
/// </summary>
/// <param name="param1"></param>
void Invoke(T1 param1);
private:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
System::Collections::Generic::List<CallbackAction<T1>^>^ actions;
};
}

View File

@ -19,7 +19,7 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE
{
///<summary>
/// CLR version of the the PlushieEngine's Vector2 class that represents a
/// CLR version of the the SHADE Engine's Vector2 class that represents a
/// 2-Dimensional Vector. Designed to closely match Unity's Vector2 struct.
/// </summary>
[System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)]