You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Code/CryEngine/CryCommon/SFunctor.h

117 lines
4.0 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
// Description : User header for multi DLL functors.
#ifndef CRYINCLUDE_CRYCOMMON_SFUNCTOR_H
#define CRYINCLUDE_CRYCOMMON_SFUNCTOR_H
#pragma once
#include "FunctorBaseFunction.h"
#include "FunctorBaseMember.h"
// Needed for CryFont to compile.
// Maybe for others too.
#include "smartptr.h"
struct SFunctor
{
public:
// Calls the functor method.
// returns true, if a functor is registered, false otherwise.
bool Call()
{
if (!m_pFunctor)
{
return false;
}
m_pFunctor->Call();
return true;
}
// Sets a new functor, common function, void return type, no arguments.
// Parameters: the function pointer.
template<typename tCallback>
void Set(tCallback pCallback)
{
typedef TFunctor<tCallback> TType;
m_pFunctor = new TType(pCallback);
}
// Sets a new functor, common function, void return type, 1 argument.
// Parameters: the function pointer, the argument to be passed to the function.
template<typename tCallback, typename tArgument1>
void Set(tCallback pCallback, const tArgument1& Argument1)
{
typedef TFunctor<tCallback> TType;
m_pFunctor = new TType(pCallback, Argument1);
}
// Sets a new functor, common function, void return type, 2 arguments.
// Parameters: the function pointer, the 2 arguments to be passed to the function.
template<typename tCallback, typename tArgument1, typename tArgument2>
void Set(tCallback pCallback, const tArgument1& Argument1, const tArgument2& Argument2)
{
typedef TFunctor<tCallback> TType;
m_pFunctor = new TType(pCallback, Argument1, Argument2);
}
// Sets a new functor, common function, void return type, no arguments.
// Parameters: the function pointer, the 2 arguments to be passed to the function.
template<typename tCallee>
void Set(tCallee* pCallee, void (tCallee::* pCallback)())
{
typedef TFunctor<void (tCallee::*)()> TType;
m_pFunctor = new TType(pCallee, pCallback);
}
// Sets a new functor, common function, void return type, 1 argument.
// Parameters: the function pointer, the 2 arguments to be passed to the function, the argument.
template<typename tCallee, typename tArgument1>
void Set(tCallee* pCallee, void (tCallee::* pCallback)(tArgument1), const tArgument1& Argument1)
{
typedef TFunctor<void (tCallee::*)(tArgument1)> TType;
m_pFunctor = new TType(pCallee, pCallback, Argument1);
}
// Sets a new functor, common function, void return type, 2 arguments.
// Parameters: the function pointer, the 2 arguments to be passed to the function, the 2 arguments.
template<typename tCallee, typename tArgument1, typename tArgument2>
void Set(tCallee* pCallee, void (tCallee::* pCallback)(tArgument1, tArgument2), const tArgument1& Argument1, const tArgument2& Argument2)
{
typedef TFunctor<void (tCallee::*)(tArgument1, tArgument2)> TType;
m_pFunctor = new TType(pCallee, pCallback, Argument1, Argument2);
}
// Used to compare equality between 2 IFunctors.
bool operator==(const SFunctor& rOther)
{
return m_pFunctor == rOther.m_pFunctor;
}
// Used order to IFunctors (needed for some containers, like map).
bool operator<(const SFunctor& rOther)
{
return m_pFunctor < rOther.m_pFunctor;
}
protected:
_smart_ptr<IFunctorBase> m_pFunctor;
};
#endif // CRYINCLUDE_CRYCOMMON_SFUNCTOR_H