summaryrefslogtreecommitdiffstats
path: root/registration.hpp
blob: 0f093ff614fae0d3003b5387d4c697935bf70b7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once

#include <functional>
#include <iostream>
#include <map>
#include <string>

namespace openpower
{
namespace util
{

using ProcedureName = std::string;
using ProcedureFunction = std::function<void()>;
using ProcedureMap = std::map<ProcedureName, ProcedureFunction>;

/**
 * This macro can be used in each procedure cpp file to make it
 * available to the openpower-proc-control executable.
 */
#define REGISTER_PROCEDURE(name, func)                                         \
    namespace func##_ns                                                        \
    {                                                                          \
        openpower::util::Registration r{std::move(name), std::move(func)};     \
    }

/**
 * Used to register procedures.  Each procedure function can then
 * be found in a map via its name.
 */
class Registration
{
  public:
    /**
     *  Adds the procedure name and function to the internal
     *  procedure map.
     *
     *  @param[in] name - the procedure name
     *  @param[in] function - the function to run
     */
    Registration(ProcedureName&& name, ProcedureFunction&& function)
    {
        procedures.emplace(std::move(name), std::move(function));
    }

    /**
     * Returns the map of procedures
     */
    static const ProcedureMap& getProcedures()
    {
        return procedures;
    }

  private:
    static ProcedureMap procedures;
};

} // namespace util
} // namespace openpower
OpenPOWER on IntegriCloud