Commit 16bec866 by Antonio Maiorano Committed by Nicolas Capens

Make RoutineT::getEntry() type safe

RoutineT::getEntry() now returns the actual function pointer type, rather than void*. Also rename "callable" to "function", since a callable is a function- like object (like RoutineT), while getEntry() returns a C function. Bug: b/143479561 Change-Id: I879b7f9aa13d05d31390596e5e0c5b23f7f80d7d Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/51968 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAntonio Maiorano <amaiorano@google.com> Kokoro-Result: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent b009745f
...@@ -28,7 +28,7 @@ public: ...@@ -28,7 +28,7 @@ public:
virtual const void *getEntry(int index = 0) const = 0; virtual const void *getEntry(int index = 0) const = 0;
}; };
// RoutineT is a type-safe wrapper around a Routine and its callable entry, returned by FunctionT // RoutineT is a type-safe wrapper around a Routine and its function entry, returned by FunctionT
template<typename FunctionType> template<typename FunctionType>
class RoutineT; class RoutineT;
...@@ -36,6 +36,8 @@ template<typename Return, typename... Arguments> ...@@ -36,6 +36,8 @@ template<typename Return, typename... Arguments>
class RoutineT<Return(Arguments...)> class RoutineT<Return(Arguments...)>
{ {
public: public:
using FunctionType = Return (*)(Arguments...);
RoutineT() = default; RoutineT() = default;
explicit RoutineT(const std::shared_ptr<Routine> &routine) explicit RoutineT(const std::shared_ptr<Routine> &routine)
...@@ -43,30 +45,29 @@ public: ...@@ -43,30 +45,29 @@ public:
{ {
if(routine) if(routine)
{ {
callable = reinterpret_cast<CallableType>(const_cast<void *>(routine->getEntry(0))); function = reinterpret_cast<FunctionType>(const_cast<void *>(routine->getEntry(0)));
} }
} }
operator bool() const operator bool() const
{ {
return callable != nullptr; return function != nullptr;
} }
template<typename... Args> template<typename... Args>
Return operator()(Args &&... args) const Return operator()(Args &&... args) const
{ {
return callable(std::forward<Args>(args)...); return function(std::forward<Args>(args)...);
} }
const void *getEntry() const const FunctionType getEntry() const
{ {
return reinterpret_cast<void *>(callable); return function;
} }
private: private:
std::shared_ptr<Routine> routine; std::shared_ptr<Routine> routine;
using CallableType = Return (*)(Arguments...); FunctionType function = nullptr;
CallableType callable = nullptr;
}; };
} // namespace rr } // namespace rr
......
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