Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
DataExport.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "cheetah/exporters/DataExport.h"
25 #include "cheetah/exporters/ExporterType.h"
26 #include <panda/Log.h>
27 #include <type_traits>
28 
29 
30 namespace ska {
31 namespace cheetah {
32 namespace exporters {
33 
34 /*
35  * helper class to initialise the DataExporter by type
36  */
37 template<typename DataExporterType, typename T>
38 class ExportInitialiser {
39 
40  public:
41  typedef std::function<void(T&)> HandlerType;
42  typedef std::function<std::function<void(T&)>(DataExportStreamConfig const&)> FactoryType;
43 
44  public:
45  ExportInitialiser(DataExporterType& object)
46  : _obj(object)
47  {
48  }
49 
50  void init_exporter(DataExportStreamConfig const& config)
51  {
52  auto it = _type_lookup.find(config.type());
53  if( it == _type_lookup.end() ) {
54  // this initialiser has no sinks associated with the type
55  return; // ignore unregistered channels
56  }
57  // ensure we only have one object per id
58  std::string const& id = config.sink_config().id();
59  auto id_it = _id_lookup.find(id);
60  if( id_it == _id_lookup.end() ) {
61  PANDA_LOG_DEBUG << "Creating sink of type " << config.type().to_string() << " (id=" << id << ")";
62  _id_lookup.insert(std::pair<std::string, HandlerType*>(id, &_obj.template add<T>(config.channel_name(), std::move((it->second).operator()(config)) )));
63  } else {
64  // redirect to the object already instantiated with that id
65  _obj.template add<T>(config.channel_name(), *(*id_it).second );
66  }
67  }
68 
69  void add_factory(ExporterType const& type, FactoryType factory) {
70  _type_lookup.emplace(type, std::move(factory));
71  }
72 
73  private:
74  DataExporterType& _obj;
75  std::map<ExporterType, FactoryType> _type_lookup;
76  std::map<std::string, HandlerType*> _id_lookup;
77 };
78 
79 template<typename... DataTypes>
80 DataExport<DataTypes...>::DataExport(exporters::DataExportConfig const& config)
81  : DataExport<DataTypes...>::BaseT(config.switch_config())
82  //, _initialisers(std::make_tuple<ExportInitialiser<typename std::remove_reference<decltype(*this)>::type, DataTypes>...>(*this)...)
83  , _initialisers(ExportInitialiser<typename std::remove_reference<decltype(*this)>::type, DataTypes>(*this)...)
84  , _config(config)
85 {
86 }
87 
88 template<typename... DataTypes>
89 DataExport<DataTypes...>::~DataExport()
90 {
91 }
92 
93 template<typename... DataTypes>
94 template<typename DataType, typename FactoryType>
95 void DataExport<DataTypes...>::set_factory(ExporterType const& type_id, FactoryType factory)
96 {
97  if(_types.find(type_id) == _types.end()) _types.insert(type_id);
98  typedef std::tuple<ExportInitialiser<DataExport<DataTypes...>, typename std::remove_const<DataTypes>::type>...> ConstRemovedTupleType;
99  //std::get<panda::Index<ExportInitialiser<DataExport<DataTypes...>, DataType>, decltype(_initialisers)>::value>(_initialisers).add_factory(type_id, std::move(factory));
100  std::get<panda::Index<ExportInitialiser<DataExport<DataTypes...>, typename std::remove_const<DataType>::type>, ConstRemovedTupleType>::value>(_initialisers).add_factory(type_id, std::move(factory));
101 }
102 
103 // helper class for panda::for_each
104 template<typename ConfigType >
106  public:
107  LaunchInitialiser(ConfigType const& config) : _config(config) {}
108 
109  template<typename T>
110  void operator()(T& initialiser) {
111  initialiser.init_exporter(_config);
112  }
113 
114  private:
115  ConfigType const& _config;
116 };
117 
118 template<typename... DataTypes>
120 {
121  for(auto const &exporter : _config.exporters()) {
122  panda::for_each(_initialisers, LaunchInitialiser<DataExportStreamConfig>(exporter));
123  }
124 }
125 
126 template<typename... DataTypes>
127 std::set<ExporterType> const& DataExport<DataTypes...>::available() const
128 {
129  return _types;
130 }
131 
132 template<typename... DataTypes>
133 template<typename DataType>
135 {
136  auto& handler = this->template add<DataType>(id, TestProbe<DataType>(id));
137  this->template activate<DataType>(id);
138 
139  TestProbe<DataType>* rv = handler.template target<TestProbe<DataType>>();
140  assert(rv != nullptr);
141  return *rv;
142 }
143 
144 } // namespace exporters
145 } // namespace cheetah
146 } // namespace ska
std::set< ExporterType > const & available() const
return the exporter types available in this class
Definition: DataExport.cpp:127
Some limits and constants for FLDO.
Definition: Brdz.h:35
void init()
initialise the output handlers using the factories supplied by set_factory(). Only call after all fac...
Definition: DataExport.cpp:119
Attach to a stream to record the data sent.
Definition: TestProbe.h:42
TestProbe< DataType > & activate_test_probe(panda::ChannelId const &)
activates a TestProbe object for monitoring the streamed data of DataType to a specific ...
Definition: DataExport.cpp:134