SourceXtractorPlusPlus 1.0.3
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 <map>
27#include <set>
28#include <string>
29#include <typeindex>
30#include <vector>
31
32#include "Table/Row.h"
34
35namespace SourceXtractor {
36
38
39public:
40
41 template <typename PropertyType, typename OutType>
42 using ColumnConverter = std::function<OutType(const PropertyType&)>;
43
45
46 template <typename PropertyType, typename OutType>
48 std::string column_unit="", std::string column_description="") {
49 m_property_to_names_map[typeid(PropertyType)].emplace_back(column_name);
50 std::type_index conv_out_type = typeid(OutType);
51 ColumnFromSource conv_func {converter};
52 m_name_to_converter_map.emplace(column_name,
53 std::pair<std::type_index, ColumnFromSource>(conv_out_type, conv_func));
54 m_name_to_col_info_map.emplace(column_name, ColInfo{column_unit, column_description});
55 }
56
63 template <typename PropertyType>
65 std::vector<std::string> new_names {};
66 for (auto& current_name : m_property_to_names_map[typeid(PropertyType)]) {
67 // Get the current converter
68 auto converter = m_name_to_converter_map.at(current_name);
69 auto col_info = m_name_to_col_info_map.at(current_name);
70 // Remove the old converter
71 // Do it *before*, because the new name may be the same!
72 m_name_to_converter_map.erase(current_name);
73 m_name_to_col_info_map.erase(current_name);
74
75 // Add the new ones
76 for (auto instance : instance_names) {
77 // Make a copy of the converter and set the index
78 auto new_converter = converter;
79 new_converter.second.index = instance.second;
80 // Register the new converter with the new name
81 auto& postfix = instance.first;
82 auto new_name = current_name + "_" + postfix;
83 m_name_to_converter_map.emplace(new_name, new_converter);
84 m_name_to_col_info_map.emplace(new_name, col_info);
85 new_names.push_back(new_name);
86 }
87 }
88 // Replace all the old names with the new ones
89 m_property_to_names_map[typeid(PropertyType)] = new_names;
90 }
91
99 template <typename PropertyType>
101 std::vector<std::string> new_names {};
102 // Get the current converter
103 auto converter = m_name_to_converter_map.at(current_name);
104 auto col_info = m_name_to_col_info_map.at(current_name);
105 // Remove the old converter
106 // Do it *before*, because the new name may be the same!
107 m_name_to_converter_map.erase(current_name);
108 m_name_to_col_info_map.erase(current_name);
109
110 // Add the new ones
111 for (auto instance : instance_names) {
112 // Make a copy of the converter and set the index
113 auto new_converter = converter;
114 new_converter.second.index = instance.second;
115 // Register the new converter with the new name
116 auto& new_name = instance.first;
117 m_name_to_converter_map.emplace(new_name, new_converter);
118 m_name_to_col_info_map.emplace(new_name, col_info);
119 new_names.push_back(new_name);
120 }
121
122 // Replace all the old names with the new ones
123 auto& names = m_property_to_names_map[typeid(PropertyType)];
124 names.erase(std::find(names.begin(), names.end(), current_name));
125 std::copy(new_names.begin(), new_names.end(), std::back_inserter(names));
126 }
127
141 template <typename PropertyType>
142 void enableOutput(std::string alias_name, bool configurable_output = false) {
143 if (m_property_to_names_map.count(typeid(PropertyType)) == 0 && !configurable_output) {
144 throw Elements::Exception() << "No registered ColumnConverters for"
145 << " property " << typeid(PropertyType).name();
146 }
147 m_output_properties.emplace(alias_name, typeid(PropertyType));
148 }
149
151 std::set<std::string> result {};
152 for (auto& pair : m_output_properties) {
153 result.emplace(pair.first);
154 }
155 return result;
156 }
157
159
160 void printPropertyColumnMap(const std::vector<std::string>& properties={});
161
162private:
163
165 public:
166 template <typename PropertyType, typename OutType>
168 m_convert_func = [converter](const SourceInterface& source, std::size_t i){
169 return converter(source.getProperty<PropertyType>(i));
170 };
171 }
176 private:
178 };
179
184
189
190};
191
192} /* namespace SourceXtractor */
193
194#endif /* _SEFRAMEWORK_OUTPUTREGISTRY_H */
195
T back_inserter(T... args)
T begin(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::function< OutType(const PropertyType &)> ColumnConverter
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 emplace(T... args)
T end(T... args)
T find(T... args)
T push_back(T... args)