blob: 1c0ae1f9dcbf929d0caec53dc4771e8a55ba857f [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Nicolas Capens48461502018-08-06 14:20:45 -040015#ifndef rr_Routine_hpp
16#define rr_Routine_hpp
Nicolas Capens0bac2852016-05-07 06:09:58 -040017
Antonio Maiorano62f49b22019-10-29 09:27:58 -040018#include <memory>
19
Nicolas Capens157ba262019-12-10 17:49:14 -050020namespace rr {
21
22class Routine
Nicolas Capens0bac2852016-05-07 06:09:58 -040023{
Nicolas Capens157ba262019-12-10 17:49:14 -050024public:
25 Routine() = default;
26 virtual ~Routine() = default;
27
28 virtual const void *getEntry(int index = 0) const = 0;
29};
30
31// RoutineT is a type-safe wrapper around a Routine and its callable entry, returned by FunctionT
32template<typename FunctionType>
33class RoutineT;
34
35template<typename Return, typename... Arguments>
36class RoutineT<Return(Arguments...)>
37{
38public:
39 RoutineT() = default;
40
Ben Clayton713b8d32019-12-17 20:37:56 +000041 explicit RoutineT(const std::shared_ptr<Routine> &routine)
42 : routine(routine)
Nicolas Capens0bac2852016-05-07 06:09:58 -040043 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -050044 if(routine)
Nicolas Capens157ba262019-12-10 17:49:14 -050045 {
Ben Clayton713b8d32019-12-17 20:37:56 +000046 callable = reinterpret_cast<CallableType>(const_cast<void *>(routine->getEntry(0)));
Nicolas Capens157ba262019-12-10 17:49:14 -050047 }
48 }
Nicolas Capens0bac2852016-05-07 06:09:58 -040049
Nicolas Capens157ba262019-12-10 17:49:14 -050050 operator bool() const
Antonio Maiorano62f49b22019-10-29 09:27:58 -040051 {
Nicolas Capens157ba262019-12-10 17:49:14 -050052 return callable != nullptr;
53 }
Antonio Maiorano62f49b22019-10-29 09:27:58 -040054
Ben Clayton713b8d32019-12-17 20:37:56 +000055 template<typename... Args>
56 Return operator()(Args &&... args) const
Nicolas Capens157ba262019-12-10 17:49:14 -050057 {
58 return callable(std::forward<Args>(args)...);
59 }
Antonio Maiorano62f49b22019-10-29 09:27:58 -040060
Ben Clayton713b8d32019-12-17 20:37:56 +000061 const void *getEntry() const
Nicolas Capens157ba262019-12-10 17:49:14 -050062 {
Ben Clayton713b8d32019-12-17 20:37:56 +000063 return reinterpret_cast<void *>(callable);
Nicolas Capens157ba262019-12-10 17:49:14 -050064 }
Antonio Maiorano62f49b22019-10-29 09:27:58 -040065
Nicolas Capens157ba262019-12-10 17:49:14 -050066private:
67 std::shared_ptr<Routine> routine;
Ben Clayton713b8d32019-12-17 20:37:56 +000068 using CallableType = Return (*)(Arguments...);
Nicolas Capens157ba262019-12-10 17:49:14 -050069 CallableType callable = nullptr;
70};
Antonio Maiorano62f49b22019-10-29 09:27:58 -040071
Nicolas Capens157ba262019-12-10 17:49:14 -050072} // namespace rr
Nicolas Capens0bac2852016-05-07 06:09:58 -040073
Ben Clayton713b8d32019-12-17 20:37:56 +000074#endif // rr_Routine_hpp