Commit 62f49b2e by Antonio Maiorano

Reactor: add type-safe FunctionT and RoutineT

Bug: b/143479561 Change-Id: Ie1b8dad6611d5fb1e2a6e4cc679c01ac98cf8e4d Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/37668 Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Reviewed-by: 's avatarBen Clayton <bclayton@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Tested-by: 's avatarAntonio Maiorano <amaiorano@google.com>
parent 855e8b1b
......@@ -2522,6 +2522,39 @@ namespace rr
{
};
// FunctionT accepts a C-style function type template argument, allowing it to return a type-safe RoutineT wrapper
template<typename FunctionType>
class FunctionT;
template<typename Return, typename... Arguments>
class FunctionT<Return(Arguments...)> : public Function<CToReactor<Return>(CToReactor<Arguments>...)>
{
public:
// Type of base class
using BaseType = Function<CToReactor<Return>(CToReactor<Arguments>...)>;
// Function type, e.g. void(int,float)
using CFunctionType = Return(Arguments...);
// Reactor function type, e.g. Void(Int, Float)
using ReactorFunctionType = CToReactor<Return>(CToReactor<Arguments>...);
// Returned RoutineT type
using RoutineType = RoutineT<CFunctionType>;
// Hide base implementations of operator()
RoutineType operator()(const char* name, ...)
{
return RoutineType(BaseType::operator()(name));
}
RoutineType operator()(const Config::Edit& cfg, const char* name, ...)
{
return RoutineType(BaseType::operator()(cfg, name));
}
};
RValue<Long> Ticks();
}
......
......@@ -15,6 +15,8 @@
#ifndef rr_Routine_hpp
#define rr_Routine_hpp
#include <memory>
namespace rr
{
class Routine
......@@ -25,6 +27,47 @@ namespace rr
virtual const void *getEntry(int index = 0) = 0;
};
// RoutineT is a type-safe wrapper around a Routine and its callable entry, returned by FunctionT
template<typename FunctionType>
class RoutineT;
template<typename Return, typename... Arguments>
class RoutineT<Return(Arguments...)>
{
public:
RoutineT() = default;
explicit RoutineT(const std::shared_ptr<Routine>& routine)
: routine(routine)
{
if (routine)
{
callable = reinterpret_cast<CallableType>(const_cast<void*>(routine->getEntry(0)));
}
}
operator bool() const
{
return callable != nullptr;
}
template <typename... Args>
Return operator()(Args&&... args) const
{
return callable(std::forward<Args>(args)...);
}
const void* getEntry() const
{
return reinterpret_cast<void*>(callable);
}
private:
std::shared_ptr<Routine> routine;
using CallableType = Return(*)(Arguments...);
CallableType callable = nullptr;
};
}
#endif // rr_Routine_hpp
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment