FairMQ  1.4.33
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 <stdexcept>
20 #include <string>
21 #include <typeindex>
22 #include <typeinfo>
23 #include <utility> // pair
24 
25 namespace fair::mq
26 {
27 
28 using Property = boost::any;
29 using Properties = std::map<std::string, Property>;
30 
31 struct PropertyChange : Event<std::string> {};
32 struct PropertyChangeAsString : Event<std::string> {};
33 
34 class PropertyHelper
35 {
36  public:
37  template<typename T>
38  static void AddType(std::string label = "")
39  {
40  if (label == "") {
41  label = boost::core::demangle(typeid(T).name());
42  }
43  fTypeInfos[std::type_index(typeid(T))] = [label](const Property& p) {
44  std::stringstream ss;
45  ss << boost::any_cast<T>(p);
46  return std::pair<std::string, std::string>{ss.str(), label};
47  };
48  fEventEmitters[std::type_index(typeid(T))] = [](const fair::mq::EventManager& em, const std::string& k, const Property& p) {
49  em.Emit<PropertyChange, T>(k, boost::any_cast<T>(p));
50  };
51  }
52 
53  static std::string ConvertPropertyToString(const Property& p)
54  {
55  return fTypeInfos.at(p.type())(p).first;
56  }
57 
58  // returns <valueAsString, typenameAsString>
59  static std::pair<std::string, std::string> GetPropertyInfo(const Property& p)
60  {
61  try {
62  return fTypeInfos.at(p.type())(p);
63  } catch (std::out_of_range& oor) {
64  return {"[unidentified_type]", "[unidentified_type]"};
65  }
66  }
67 
68  static std::unordered_map<std::type_index, void(*)(const fair::mq::EventManager&, const std::string&, const Property&)> fEventEmitters;
69  private:
70  static std::unordered_map<std::type_index, std::function<std::pair<std::string, std::string>(const Property&)>> fTypeInfos;
71 };
72 
73 }
74 
75 #endif /* FAIR_MQ_PROPERTIES_H */
fair::mq
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
fair::mq::EventManager
Manages event callbacks from different subscribers.
Definition: EventManager.h:56

privacy