Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // 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 Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 15 | #ifndef rr_Routine_hpp |
| 16 | #define rr_Routine_hpp |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 17 | |
Antonio Maiorano | 62f49b2 | 2019-10-29 09:27:58 -0400 | [diff] [blame] | 18 | #include <memory> |
| 19 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 20 | namespace rr { |
| 21 | |
| 22 | class Routine |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 23 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 24 | public: |
| 25 | Routine() = default; |
| 26 | virtual ~Routine() = default; |
| 27 | |
| 28 | virtual const void *getEntry(int index = 0) const = 0; |
| 29 | }; |
| 30 | |
Antonio Maiorano | 16bec86 | 2020-12-17 11:17:45 -0500 | [diff] [blame] | 31 | // RoutineT is a type-safe wrapper around a Routine and its function entry, returned by FunctionT |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 32 | template<typename FunctionType> |
| 33 | class RoutineT; |
| 34 | |
| 35 | template<typename Return, typename... Arguments> |
| 36 | class RoutineT<Return(Arguments...)> |
| 37 | { |
| 38 | public: |
Antonio Maiorano | 16bec86 | 2020-12-17 11:17:45 -0500 | [diff] [blame] | 39 | using FunctionType = Return (*)(Arguments...); |
| 40 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 41 | RoutineT() = default; |
| 42 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 43 | explicit RoutineT(const std::shared_ptr<Routine> &routine) |
| 44 | : routine(routine) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 45 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 46 | if(routine) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 47 | { |
Antonio Maiorano | 16bec86 | 2020-12-17 11:17:45 -0500 | [diff] [blame] | 48 | function = reinterpret_cast<FunctionType>(const_cast<void *>(routine->getEntry(0))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 49 | } |
| 50 | } |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 51 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 52 | operator bool() const |
Antonio Maiorano | 62f49b2 | 2019-10-29 09:27:58 -0400 | [diff] [blame] | 53 | { |
Antonio Maiorano | 16bec86 | 2020-12-17 11:17:45 -0500 | [diff] [blame] | 54 | return function != nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 55 | } |
Antonio Maiorano | 62f49b2 | 2019-10-29 09:27:58 -0400 | [diff] [blame] | 56 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 57 | template<typename... Args> |
Nicolas Capens | 6b6d4c8 | 2022-04-06 16:56:12 -0400 | [diff] [blame] | 58 | Return operator()(Args... args) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 59 | { |
Nicolas Capens | 6b6d4c8 | 2022-04-06 16:56:12 -0400 | [diff] [blame] | 60 | return function(args...); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 61 | } |
Antonio Maiorano | 62f49b2 | 2019-10-29 09:27:58 -0400 | [diff] [blame] | 62 | |
Antonio Maiorano | 16bec86 | 2020-12-17 11:17:45 -0500 | [diff] [blame] | 63 | const FunctionType getEntry() const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 64 | { |
Antonio Maiorano | 16bec86 | 2020-12-17 11:17:45 -0500 | [diff] [blame] | 65 | return function; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 66 | } |
Antonio Maiorano | 62f49b2 | 2019-10-29 09:27:58 -0400 | [diff] [blame] | 67 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 68 | private: |
| 69 | std::shared_ptr<Routine> routine; |
Antonio Maiorano | 16bec86 | 2020-12-17 11:17:45 -0500 | [diff] [blame] | 70 | FunctionType function = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 71 | }; |
Antonio Maiorano | 62f49b2 | 2019-10-29 09:27:58 -0400 | [diff] [blame] | 72 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 73 | } // namespace rr |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 74 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 75 | #endif // rr_Routine_hpp |