blob: 24c5198032f87a58c8d4fb5f49b26ba62b28a2a9 [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
Antonio Maiorano16bec862020-12-17 11:17:45 -050031// RoutineT is a type-safe wrapper around a Routine and its function entry, returned by FunctionT
Nicolas Capens157ba262019-12-10 17:49:14 -050032template<typename FunctionType>
33class RoutineT;
34
35template<typename Return, typename... Arguments>
36class RoutineT<Return(Arguments...)>
37{
38public:
Antonio Maiorano16bec862020-12-17 11:17:45 -050039 using FunctionType = Return (*)(Arguments...);
40
Nicolas Capens157ba262019-12-10 17:49:14 -050041 RoutineT() = default;
42
Ben Clayton713b8d32019-12-17 20:37:56 +000043 explicit RoutineT(const std::shared_ptr<Routine> &routine)
44 : routine(routine)
Nicolas Capens0bac2852016-05-07 06:09:58 -040045 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -050046 if(routine)
Nicolas Capens157ba262019-12-10 17:49:14 -050047 {
Antonio Maiorano16bec862020-12-17 11:17:45 -050048 function = reinterpret_cast<FunctionType>(const_cast<void *>(routine->getEntry(0)));
Nicolas Capens157ba262019-12-10 17:49:14 -050049 }
50 }
Nicolas Capens0bac2852016-05-07 06:09:58 -040051
Nicolas Capens157ba262019-12-10 17:49:14 -050052 operator bool() const
Antonio Maiorano62f49b22019-10-29 09:27:58 -040053 {
Antonio Maiorano16bec862020-12-17 11:17:45 -050054 return function != nullptr;
Nicolas Capens157ba262019-12-10 17:49:14 -050055 }
Antonio Maiorano62f49b22019-10-29 09:27:58 -040056
Ben Clayton713b8d32019-12-17 20:37:56 +000057 template<typename... Args>
Nicolas Capensdac99e82020-11-19 04:18:58 +000058 Return operator()(Args &&...args) const
Nicolas Capens157ba262019-12-10 17:49:14 -050059 {
Antonio Maiorano16bec862020-12-17 11:17:45 -050060 return function(std::forward<Args>(args)...);
Nicolas Capens157ba262019-12-10 17:49:14 -050061 }
Antonio Maiorano62f49b22019-10-29 09:27:58 -040062
Antonio Maiorano16bec862020-12-17 11:17:45 -050063 const FunctionType getEntry() const
Nicolas Capens157ba262019-12-10 17:49:14 -050064 {
Antonio Maiorano16bec862020-12-17 11:17:45 -050065 return function;
Nicolas Capens157ba262019-12-10 17:49:14 -050066 }
Antonio Maiorano62f49b22019-10-29 09:27:58 -040067
Nicolas Capens157ba262019-12-10 17:49:14 -050068private:
69 std::shared_ptr<Routine> routine;
Antonio Maiorano16bec862020-12-17 11:17:45 -050070 FunctionType function = nullptr;
Nicolas Capens157ba262019-12-10 17:49:14 -050071};
Antonio Maiorano62f49b22019-10-29 09:27:58 -040072
Nicolas Capens157ba262019-12-10 17:49:14 -050073} // namespace rr
Nicolas Capens0bac2852016-05-07 06:09:58 -040074
Ben Clayton713b8d32019-12-17 20:37:56 +000075#endif // rr_Routine_hpp