FairMQ  1.4.14
C++ Message Queuing Library and Framework
Properties.h
1 /********************************************************************************
2  * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 #ifndef FAIR_MQ_PROPERTIES_H
9 #define FAIR_MQ_PROPERTIES_H
10 
11 #include <fairmq/EventManager.h>
12 
13 #include <boost/any.hpp>
14 #include <boost/core/demangle.hpp>
15 
16 #include <functional>
17 #include <map>
18 #include <unordered_map>
19 #include <string>
20 #include <typeindex>
21 #include <typeinfo>
22 #include <utility> // pair
23 
24 namespace fair
25 {
26 namespace mq
27 {
28 
29 using Property = boost::any;
30 using Properties = std::map<std::string, Property>;
31 
32 struct PropertyChange : Event<std::string> {};
33 struct PropertyChangeAsString : Event<std::string> {};
34 
36 {
37  public:
38  template<typename T>
39  static void AddType(std::string label = "")
40  {
41  if (label == "") {
42  label = boost::core::demangle(typeid(T).name());
43  }
44  fTypeInfos[std::type_index(typeid(T))] = [label](const Property& p) {
45  std::stringstream ss;
46  ss << boost::any_cast<T>(p);
47  return std::pair<std::string, std::string>{ss.str(), label};
48  };
49  fEventEmitters[std::type_index(typeid(T))] = [](const fair::mq::EventManager& em, const std::string& k, const Property& p) {
50  em.Emit<PropertyChange, T>(k, boost::any_cast<T>(p));
51  };
52  }
53 
54  static std::string ConvertPropertyToString(const Property& p)
55  {
56  return fTypeInfos.at(p.type())(p).first;
57  }
58 
59  // returns <valueAsString, typenameAsString>
60  static std::pair<std::string, std::string> GetPropertyInfo(const Property& p)
61  {
62  try {
63  return fTypeInfos.at(p.type())(p);
64  } catch (std::out_of_range& oor) {
65  return {"[unidentified_type]", "[unidentified_type]"};
66  }
67  }
68 
69  static std::unordered_map<std::type_index, void(*)(const fair::mq::EventManager&, const std::string&, const Property&)> fEventEmitters;
70  private:
71  static std::unordered_map<std::type_index, std::function<std::pair<std::string, std::string>(const Property&)>> fTypeInfos;
72 };
73 
74 }
75 }
76 
77 #endif /* FAIR_MQ_PROPERTIES_H */
Definition: Properties.h:35
Definition: EventManager.h:31
Manages event callbacks from different subscribers.
Definition: EventManager.h:51
Definition: Properties.h:33
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
Definition: Properties.h:32

privacy