blob: 5b398871e2c6400f8b63689abb60632a258cd645 [file] [log] [blame]
Matt Spinlerd9bdcf72017-03-09 15:06:23 -06001#pragma once
2
3#include <functional>
Patrick Venturef78d9042018-11-01 15:39:53 -07004#include <iostream>
Matt Spinlerd9bdcf72017-03-09 15:06:23 -06005#include <map>
6#include <string>
Matt Spinlerd9bdcf72017-03-09 15:06:23 -06007
8namespace openpower
9{
10namespace util
11{
12
13using ProcedureName = std::string;
14using ProcedureFunction = std::function<void()>;
15using 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 Venturef78d9042018-11-01 15:39:53 -070021#define REGISTER_PROCEDURE(name, func) \
22 namespace func##_ns \
23 { \
24 openpower::util::Registration r{std::move(name), std::move(func)}; \
25 }
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060026
27/**
28 * Used to register procedures. Each procedure function can then
29 * be found in a map via its name.
30 */
31class Registration
32{
Patrick Venturef78d9042018-11-01 15:39:53 -070033 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 Geisslere04c1172021-06-14 11:27:35 -050043 procedures().emplace(std::move(name), std::move(function));
Patrick Venturef78d9042018-11-01 15:39:53 -070044 }
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060045
Patrick Venturef78d9042018-11-01 15:39:53 -070046 /**
47 * Returns the map of procedures
48 */
49 static const ProcedureMap& getProcedures()
50 {
Andrew Geisslere04c1172021-06-14 11:27:35 -050051 return procedures();
Patrick Venturef78d9042018-11-01 15:39:53 -070052 }
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060053
Patrick Venturef78d9042018-11-01 15:39:53 -070054 private:
Andrew Geisslere04c1172021-06-14 11:27:35 -050055 static ProcedureMap& procedures()
56 {
57 static ProcedureMap procMap;
58 return procMap;
59 }
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060060};
61
Patrick Venturef78d9042018-11-01 15:39:53 -070062} // namespace util
63} // namespace openpower