SourceXtractorPlusPlus 0.19
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
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
34namespace SourceXtractor {
35
37
38public:
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>
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
161private:
162
164 public:
165 template <typename PropertyType, typename OutType>
167 m_convert_func = [converter](const SourceInterface& source, std::size_t i){
168 return converter(source.getProperty<PropertyType>(i));
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
T back_inserter(T... args)
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
ColumnFromSource(ColumnConverter< PropertyType, OutType > converter)
std::function< Euclid::Table::Row::cell_type(const SourceInterface &, std::size_t index)> m_convert_func
Euclid::Table::Row::cell_type operator()(const SourceInterface &source)
std::map< std::string, ColInfo > m_name_to_col_info_map
void registerColumnConverter(std::string column_name, ColumnConverter< PropertyType, OutType > converter, std::string column_unit="", std::string column_description="")
void enableOutput(std::string alias_name, bool configurable_output=false)
SourceToRowConverter getSourceToRowConverter(const std::vector< std::string > &enabled_optional)
std::multimap< std::string, std::type_index > m_output_properties
std::set< std::string > getOutputPropertyNames()
void registerPropertyInstances(const std::string &current_name, const std::vector< std::pair< std::string, unsigned int > > &instance_names)
std::function< Euclid::Table::Row(const SourceInterface &)> SourceToRowConverter
std::map< std::string, std::pair< std::type_index, ColumnFromSource > > m_name_to_converter_map
std::map< std::type_index, std::vector< std::string > > m_property_to_names_map
void registerPropertyInstances(const std::vector< std::pair< std::string, unsigned int > > &instance_names)
void printPropertyColumnMap(const std::vector< std::string > &properties={})
The SourceInterface is an abstract "source" that has properties attached to it.
const PropertyType & getProperty(unsigned int index=0) const
Convenience template method to call getProperty() with a more user-friendly syntax.
T copy(T... args)
T count(T... args)
T emplace(T... args)
T erase(T... args)
T find(T... args)