blob: 0f093ff614fae0d3003b5387d4c697935bf70b7c [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 {
43 procedures.emplace(std::move(name), std::move(function));
44 }
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 {
51 return procedures;
52 }
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060053
Patrick Venturef78d9042018-11-01 15:39:53 -070054 private:
55 static ProcedureMap procedures;
Matt Spinlerd9bdcf72017-03-09 15:06:23 -060056};
57
Patrick Venturef78d9042018-11-01 15:39:53 -070058} // namespace util
59} // namespace openpower