SourceXtractorPlusPlus  0.15
Please provide a description of the project.
OutputRegistry.h
Go to the documentation of this file.
1 
17 /*
18  * @file OutputRegistry.h
19  * @author nikoapos
20  */
21 
22 #ifndef _SEFRAMEWORK_OUTPUTREGISTRY_H
23 #define _SEFRAMEWORK_OUTPUTREGISTRY_H
24 
25 #include <functional>
26 #include <vector>
27 #include <string>
28 #include <map>
29 #include <typeindex>
30 
31 #include "Table/Row.h"
33 
34 namespace SourceXtractor {
35 
37 
38 public:
39 
40  template <typename PropertyType, typename OutType>
41  using ColumnConverter = std::function<OutType(const PropertyType&)>;
42 
44 
45  template <typename PropertyType, typename OutType>
47  std::string column_unit="", std::string column_description="") {
48  m_property_to_names_map[typeid(PropertyType)].emplace_back(column_name);
49  std::type_index conv_out_type = typeid(OutType);
50  ColumnFromSource conv_func {converter};
51  m_name_to_converter_map.emplace(column_name,
52  std::pair<std::type_index, ColumnFromSource>(conv_out_type, conv_func));
53  m_name_to_col_info_map.emplace(column_name, ColInfo{column_unit, column_description});
54  }
55 
62  template <typename PropertyType>
64  std::vector<std::string> new_names {};
65  for (auto& current_name : m_property_to_names_map[typeid(PropertyType)]) {
66  // Get the current converter
67  auto converter = m_name_to_converter_map.at(current_name);
68  auto col_info = m_name_to_col_info_map.at(current_name);
69  // Remove the old converter
70  // Do it *before*, because the new name may be the same!
71  m_name_to_converter_map.erase(current_name);
72  m_name_to_col_info_map.erase(current_name);
73 
74  // Add the new ones
75  for (auto instance : instance_names) {
76  // Make a copy of the converter and set the index
77  auto new_converter = converter;
78  new_converter.second.index = instance.second;
79  // Register the new converter with the new name
80  auto& postfix = instance.first;
81  auto new_name = current_name + "_" + postfix;
82  m_name_to_converter_map.emplace(new_name, new_converter);
83  m_name_to_col_info_map.emplace(new_name, col_info);
84  new_names.push_back(new_name);
85  }
86  }
87  // Replace all the old names with the new ones
88  m_property_to_names_map[typeid(PropertyType)] = new_names;
89  }
90 
98  template <typename PropertyType>
99  void registerPropertyInstances(const std::string &current_name, const std::vector<std::pair<std::string, unsigned int>>& instance_names) {
100  std::vector<std::string> new_names {};
101  // Get the current converter
102  auto converter = m_name_to_converter_map.at(current_name);
103  auto col_info = m_name_to_col_info_map.at(current_name);
104  // Remove the old converter
105  // Do it *before*, because the new name may be the same!
106  m_name_to_converter_map.erase(current_name);
107  m_name_to_col_info_map.erase(current_name);
108 
109  // Add the new ones
110  for (auto instance : instance_names) {
111  // Make a copy of the converter and set the index
112  auto new_converter = converter;
113  new_converter.second.index = instance.second;
114  // Register the new converter with the new name
115  auto& new_name = instance.first;
116  m_name_to_converter_map.emplace(new_name, new_converter);
117  m_name_to_col_info_map.emplace(new_name, col_info);
118  new_names.push_back(new_name);
119  }
120 
121  // Replace all the old names with the new ones
122  auto& names = m_property_to_names_map[typeid(PropertyType)];
123  names.erase(std::find(names.begin(), names.end(), current_name));
124  std::copy(new_names.begin(), new_names.end(), std::back_inserter(names));
125  }
126 
140  template <typename PropertyType>
141  void enableOutput(std::string alias_name, bool configurable_output = false) {
142  if (m_property_to_names_map.count(typeid(PropertyType)) == 0 && !configurable_output) {
143  throw Elements::Exception() << "No registered ColumnConverters for"
144  << " property " << typeid(PropertyType).name();
145  }
146  m_output_properties.emplace(alias_name, typeid(PropertyType));
147  }
148 
150  std::set<std::string> result {};
151  for (auto& pair : m_output_properties) {
152  result.emplace(pair.first);
153  }
154  return result;
155  }
156 
158 
159  void printPropertyColumnMap(const std::vector<std::string>& properties={});
160 
161 private:
162 
164  public:
165  template <typename PropertyType, typename OutType>
167  m_convert_func = [converter](const SourceInterface& source, std::size_t index){
168  return converter(source.getProperty<PropertyType>(index));
169  };
170  }
172  return m_convert_func(source, index);
173  }
175  private:
177  };
178 
179  struct ColInfo {
182  };
183 
188 
189 };
190 
191 } /* namespace SourceXtractor */
192 
193 #endif /* _SEFRAMEWORK_OUTPUTREGISTRY_H */
194 
std::string
STL class.
Euclid::Table::Row::cell_type
boost::variant< bool, int32_t, int64_t, float, double, std::string, std::vector< bool >, std::vector< int32_t >, std::vector< int64_t >, std::vector< float >, std::vector< double >, NdArray::NdArray< int32_t >, NdArray::NdArray< int64_t >, NdArray::NdArray< float >, NdArray::NdArray< double > > cell_type
std::pair
SourceXtractor::OutputRegistry::SourceToRowConverter
std::function< Euclid::Table::Row(const SourceInterface &)> SourceToRowConverter
Definition: OutputRegistry.h:43
SourceXtractor::OutputRegistry::ColumnFromSource::index
std::size_t index
Definition: OutputRegistry.h:174
SourceInterface.h
SourceXtractor::OutputRegistry::m_output_properties
std::multimap< std::string, std::type_index > m_output_properties
Definition: OutputRegistry.h:187
std::vector
STL class.
std::find
T find(T... args)
SourceXtractor::OutputRegistry::ColumnFromSource::ColumnFromSource
ColumnFromSource(ColumnConverter< PropertyType, OutType > converter)
Definition: OutputRegistry.h:166
std::back_inserter
T back_inserter(T... args)
SourceXtractor::OutputRegistry
Definition: OutputRegistry.h:36
std::multimap::emplace
T emplace(T... args)
SourceXtractor::OutputRegistry::ColInfo::description
std::string description
Definition: OutputRegistry.h:181
std::type_index
SourceXtractor::OutputRegistry::m_name_to_col_info_map
std::map< std::string, ColInfo > m_name_to_col_info_map
Definition: OutputRegistry.h:186
SourceXtractor::OutputRegistry::m_name_to_converter_map
std::map< std::string, std::pair< std::type_index, ColumnFromSource > > m_name_to_converter_map
Definition: OutputRegistry.h:185
Row.h
std::function
SourceXtractor::OutputRegistry::ColInfo
Definition: OutputRegistry.h:179
SourceXtractor::OutputRegistry::registerPropertyInstances
void registerPropertyInstances(const std::string &current_name, const std::vector< std::pair< std::string, unsigned int >> &instance_names)
Definition: OutputRegistry.h:99
SourceXtractor::OutputRegistry::ColumnFromSource::operator()
Euclid::Table::Row::cell_type operator()(const SourceInterface &source)
Definition: OutputRegistry.h:171
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::OutputRegistry::getSourceToRowConverter
SourceToRowConverter getSourceToRowConverter(const std::vector< std::string > &enabled_optional)
Definition: OutputRegistry.cpp:36
SourceXtractor::OutputRegistry::ColInfo::unit
std::string unit
Definition: OutputRegistry.h:180
std::map::erase
T erase(T... args)
SourceXtractor::OutputRegistry::printPropertyColumnMap
void printPropertyColumnMap(const std::vector< std::string > &properties={})
Definition: OutputRegistry.cpp:70
std::copy
T copy(T... args)
Elements::Exception
std::map
STL class.
SourceXtractor::OutputRegistry::m_property_to_names_map
std::map< std::type_index, std::vector< std::string > > m_property_to_names_map
Definition: OutputRegistry.h:184
SourceXtractor::OutputRegistry::ColumnFromSource
Definition: OutputRegistry.h:163
SourceXtractor::OutputRegistry::ColumnFromSource::m_convert_func
std::function< Euclid::Table::Row::cell_type(const SourceInterface &, std::size_t index)> m_convert_func
Definition: OutputRegistry.h:176
std::map::count
T count(T... args)
SourceXtractor::OutputRegistry::getOutputPropertyNames
std::set< std::string > getOutputPropertyNames()
Definition: OutputRegistry.h:149
std::multimap< std::string, std::type_index >
SourceXtractor::SourceInterface::getProperty
const PropertyType & getProperty(unsigned int index=0) const
Convenience template method to call getProperty() with a more user-friendly syntax.
Definition: SourceInterface.h:57
std::size_t
Euclid::Table::Row
SourceXtractor::SourceInterface
The SourceInterface is an abstract "source" that has properties attached to it.
Definition: SourceInterface.h:46
SourceXtractor::OutputRegistry::registerPropertyInstances
void registerPropertyInstances(const std::vector< std::pair< std::string, unsigned int >> &instance_names)
Definition: OutputRegistry.h:63
SourceXtractor::OutputRegistry::enableOutput
void enableOutput(std::string alias_name, bool configurable_output=false)
Definition: OutputRegistry.h:141
SourceXtractor::OutputRegistry::registerColumnConverter
void registerColumnConverter(std::string column_name, ColumnConverter< PropertyType, OutType > converter, std::string column_unit="", std::string column_description="")
Definition: OutputRegistry.h:46
std::set
STL class.