IQRF Gateway Daemon
LaunchUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 MICRORISC s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "IqrfLogging.h"
20 #include <memory>
21 #include <string>
22 #include <map>
23 
27 {
28 public:
30  static StaticBuildFunctionMap& get()
31  {
32  static StaticBuildFunctionMap s;
33  return s;
34  }
35 
37 
44  void* getFunction(const std::string& fceName) const
45  {
46  auto found = m_fceMap.find(fceName);
47  if (found != m_fceMap.end()) {
48  return found->second;
49  }
50  return nullptr;
51  }
52 
59  void setFunction(const std::string& fceName, void* fcePtr)
60  {
61  auto inserted = m_fceMap.insert(make_pair(fceName, fcePtr));
62  if (!inserted.second) {
63  std::cerr << PAR(fceName) << ": is already stored. The binary isn't probably correctly linked" << std::endl;
64  THROW_EX(std::logic_error, PAR(fceName) << ": is already stored. The binary isn't probably correctly linked");
65  }
66  }
67 
68 private:
70  std::map<std::string, void*> m_fceMap;
71 };
72 
83 #define INIT_COMPONENT(IComponentClassName, ComponentClassName) \
84 std::unique_ptr<IComponentClassName> __launch_create_##ComponentClassName(const std::string& iname) { \
85  return std::unique_ptr<IComponentClassName>(new ComponentClassName(iname)); } \
86  \
87 class __Loader_##ComponentClassName { \
88 public: \
89  __Loader_##ComponentClassName() { \
90  StaticBuildFunctionMap::get().setFunction("__launch_create_" #ComponentClassName, \
91  (void*)&__launch_create_##ComponentClassName); \
92  } \
93 }; \
94  \
95 void init_##ComponentClassName() { \
96  static __Loader_##ComponentClassName cs; \
97 }
void setFunction(const std::string &fceName, void *fcePtr)
Set pointer to function.
Definition: LaunchUtils.h:59
virtual ~StaticBuildFunctionMap()
Definition: LaunchUtils.h:36
Holds pointers to component loader functions.
Definition: LaunchUtils.h:26
void * getFunction(const std::string &fceName) const
Get pointer to function.
Definition: LaunchUtils.h:44