1 #pragma once 2 3 #include <functional> 4 #include <iostream> 5 #include <map> 6 #include <string> 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{std::move(name), std::move(func)}; \ 25 } 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 { 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 { 43 procedures().emplace(std::move(name), std::move(function)); 44 } 45 46 /** 47 * Returns the map of procedures 48 */ 49 static const ProcedureMap& getProcedures() 50 { 51 return procedures(); 52 } 53 54 private: 55 static ProcedureMap& procedures() 56 { 57 static ProcedureMap procMap; 58 return procMap; 59 } 60 }; 61 62 } // namespace util 63 } // namespace openpower 64