Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <functional> |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 4 | #include <iostream> |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 5 | #include <map> |
| 6 | #include <string> |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 7 | |
| 8 | namespace openpower |
| 9 | { |
| 10 | namespace util |
| 11 | { |
| 12 | |
| 13 | using ProcedureName = std::string; |
| 14 | using ProcedureFunction = std::function<void()>; |
| 15 | using ProcedureMap = std::map<ProcedureName, ProcedureFunction>; |
| 16 | |
| 17 | /** |
| 18 | * This macro can be used in each procedure cpp file to make it |
| 19 | * available to the openpower-proc-control executable. |
| 20 | */ |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 21 | #define REGISTER_PROCEDURE(name, func) \ |
| 22 | namespace func##_ns \ |
| 23 | { \ |
| 24 | openpower::util::Registration r{std::move(name), std::move(func)}; \ |
| 25 | } |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Used to register procedures. Each procedure function can then |
| 29 | * be found in a map via its name. |
| 30 | */ |
| 31 | class Registration |
| 32 | { |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 33 | public: |
| 34 | /** |
| 35 | * Adds the procedure name and function to the internal |
| 36 | * procedure map. |
| 37 | * |
| 38 | * @param[in] name - the procedure name |
| 39 | * @param[in] function - the function to run |
| 40 | */ |
| 41 | Registration(ProcedureName&& name, ProcedureFunction&& function) |
| 42 | { |
Andrew Geissler | e04c117 | 2021-06-14 11:27:35 -0500 | [diff] [blame] | 43 | procedures().emplace(std::move(name), std::move(function)); |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 44 | } |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 45 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Returns the map of procedures |
| 48 | */ |
| 49 | static const ProcedureMap& getProcedures() |
| 50 | { |
Andrew Geissler | e04c117 | 2021-06-14 11:27:35 -0500 | [diff] [blame] | 51 | return procedures(); |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 52 | } |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 53 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 54 | private: |
Andrew Geissler | e04c117 | 2021-06-14 11:27:35 -0500 | [diff] [blame] | 55 | static ProcedureMap& procedures() |
| 56 | { |
| 57 | static ProcedureMap procMap; |
| 58 | return procMap; |
| 59 | } |
Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame] | 60 | }; |
| 61 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 62 | } // namespace util |
| 63 | } // namespace openpower |