Matt Spinler | d9bdcf7 | 2017-03-09 15:06:23 -0600 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <functional> |
| 4 | #include <map> |
| 5 | #include <string> |
| 6 | #include <iostream> |
| 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 | */ |
| 21 | #define REGISTER_PROCEDURE(name, func) \ |
| 22 | namespace func##_ns \ |
| 23 | { \ |
| 24 | openpower::util::Registration r{ \ |
| 25 | std::move(name), std::move(func)}; \ |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Used to register procedures. Each procedure function can then |
| 31 | * be found in a map via its name. |
| 32 | */ |
| 33 | class Registration |
| 34 | { |
| 35 | public: |
| 36 | |
| 37 | /** |
| 38 | * Adds the procedure name and function to the internal |
| 39 | * procedure map. |
| 40 | * |
| 41 | * @param[in] name - the procedure name |
| 42 | * @param[in] function - the function to run |
| 43 | */ |
| 44 | Registration(ProcedureName&& name, |
| 45 | ProcedureFunction&& function) |
| 46 | { |
| 47 | procedures.emplace(std::move(name), std::move(function)); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the map of procedures |
| 52 | */ |
| 53 | static const ProcedureMap& getProcedures() |
| 54 | { |
| 55 | return procedures; |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | |
| 60 | static ProcedureMap procedures; |
| 61 | }; |
| 62 | |
| 63 | } |
| 64 | } |