mirror of
https://github.com/paulscherrerinstitute/sf_daq_buffer.git
synced 2026-05-01 13:52:23 +02:00
Refactoring to hash map
This commit is contained in:
+13
-7
@@ -58,7 +58,8 @@ H5::Group H5FormatUtils::create_group(H5::Group& target, const string& name)
|
||||
return target.createGroup(name.c_str());
|
||||
}
|
||||
|
||||
const boost::any& H5FormatUtils::get_value_from_reference(const string& dataset_name, const boost::any& value_reference, const map<string, boost::any>& values)
|
||||
const boost::any& H5FormatUtils::get_value_from_reference(const string& dataset_name,
|
||||
const boost::any& value_reference, const unordered_map<string, boost::any>& values)
|
||||
{
|
||||
try {
|
||||
auto reference_string = boost::any_cast<string>(value_reference);
|
||||
@@ -78,7 +79,8 @@ const boost::any& H5FormatUtils::get_value_from_reference(const string& dataset_
|
||||
|
||||
} catch (const out_of_range& exception){
|
||||
stringstream error_message;
|
||||
error_message << "Dataset " << dataset_name << " value reference " << boost::any_cast<string>(value_reference) << " not present in values map." << endl;
|
||||
error_message << "Dataset " << dataset_name << " value reference " << boost::any_cast<string>(value_reference);
|
||||
error_message << " not present in values map." << endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
}
|
||||
@@ -123,7 +125,8 @@ const H5::PredType& H5FormatUtils::get_dataset_data_type(const string& type)
|
||||
}
|
||||
}
|
||||
|
||||
H5::DataSet H5FormatUtils::write_dataset(H5::Group& target, const h5_dataset& dataset, const map<string, boost::any>& values)
|
||||
H5::DataSet H5FormatUtils::write_dataset(H5::Group& target, const h5_dataset& dataset,
|
||||
const unordered_map<string, boost::any>& values)
|
||||
{
|
||||
const string& name = dataset.name;
|
||||
boost::any value;
|
||||
@@ -232,7 +235,8 @@ void H5FormatUtils::write_attribute(H5::H5Object& target, const string& name, in
|
||||
h5_attribute.write(data_type, &value);
|
||||
}
|
||||
|
||||
void H5FormatUtils::write_attribute(H5::H5Object& target, const h5_attr& attribute, const map<string, boost::any>& values)
|
||||
void H5FormatUtils::write_attribute(H5::H5Object& target, const h5_attr& attribute,
|
||||
const unordered_map<string, boost::any>& values)
|
||||
{
|
||||
string name = attribute.name;
|
||||
boost::any value;
|
||||
@@ -278,7 +282,8 @@ void H5FormatUtils::write_attribute(H5::H5Object& target, const h5_attr& attribu
|
||||
}
|
||||
}
|
||||
|
||||
void H5FormatUtils::write_format_data(H5::Group& file_node, const h5_parent& format_node, const std::map<std::string, h5_value>& values)
|
||||
void H5FormatUtils::write_format_data(H5::Group& file_node, const h5_parent& format_node,
|
||||
const std::unordered_map<std::string, h5_value>& values)
|
||||
{
|
||||
H5::Group node_group = file_node;
|
||||
|
||||
@@ -322,12 +327,13 @@ void H5FormatUtils::write_format_data(H5::Group& file_node, const h5_parent& for
|
||||
}
|
||||
}
|
||||
|
||||
void H5FormatUtils::write_format(H5::H5File& file, const H5Format& format, const std::map<std::string, h5_value>& input_values)
|
||||
void H5FormatUtils::write_format(H5::H5File& file, const H5Format& format,
|
||||
const std::unordered_map<std::string, h5_value>& input_values)
|
||||
{
|
||||
auto format_definition = format.get_format_definition();
|
||||
auto default_values = format.get_default_values();
|
||||
|
||||
map<string, boost::any> format_values(default_values);
|
||||
auto format_values(default_values);
|
||||
|
||||
format.add_input_values(format_values, input_values);
|
||||
format.add_calculated_values(format_values);
|
||||
|
||||
+23
-11
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <H5Cpp.h>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
@@ -89,16 +89,16 @@ class H5Format
|
||||
public:
|
||||
virtual ~H5Format(){};
|
||||
|
||||
virtual const std::map<std::string, DATA_TYPE>& get_input_value_type() const = 0;
|
||||
virtual const std::unordered_map<std::string, DATA_TYPE>& get_input_value_type() const = 0;
|
||||
|
||||
virtual const std::map<std::string, boost::any>& get_default_values() const = 0;
|
||||
virtual const std::unordered_map<std::string, boost::any>& get_default_values() const = 0;
|
||||
|
||||
virtual const h5_group& get_format_definition() const = 0;
|
||||
|
||||
virtual void add_calculated_values(std::map<std::string, boost::any>& values) const = 0;
|
||||
virtual void add_calculated_values(std::unordered_map<std::string, boost::any>& values) const = 0;
|
||||
|
||||
virtual void add_input_values(std::map<std::string, boost::any>& values,
|
||||
const std::map<std::string, boost::any>& input_values) const = 0;
|
||||
virtual void add_input_values(std::unordered_map<std::string, boost::any>& values,
|
||||
const std::unordered_map<std::string, boost::any>& input_values) const = 0;
|
||||
|
||||
virtual std::string get_frames_dataset_name() const = 0;
|
||||
};
|
||||
@@ -106,24 +106,36 @@ class H5Format
|
||||
namespace H5FormatUtils
|
||||
{
|
||||
hsize_t expand_dataset(H5::DataSet& dataset, hsize_t frame_index, hsize_t dataset_increase_step);
|
||||
|
||||
void compact_dataset(H5::DataSet& dataset, hsize_t max_frame_index);
|
||||
|
||||
H5::Group create_group(H5::Group& target, const std::string& name);
|
||||
const H5::PredType& get_dataset_data_type(const std::string& type);
|
||||
|
||||
H5::DataSet write_dataset(H5::Group& target, const h5_dataset& dataset, const std::map<std::string, boost::any>& values);
|
||||
H5::DataSet write_dataset(H5::Group& target, const h5_dataset& dataset,
|
||||
const std::unordered_map<std::string, boost::any>& values);
|
||||
|
||||
H5::DataSet write_dataset(H5::Group& target, const std::string& name, double value);
|
||||
|
||||
H5::DataSet write_dataset(H5::Group& target, const std::string& name, int value);
|
||||
|
||||
H5::DataSet write_dataset(H5::Group& target, const std::string& name, const std::string& value);
|
||||
|
||||
void write_attribute(H5::H5Object& target, const h5_attr& attribute, const std::map<std::string, boost::any>& values);
|
||||
void write_attribute(H5::H5Object& target, const h5_attr& attribute,
|
||||
const std::unordered_map<std::string, boost::any>& values);
|
||||
|
||||
void write_attribute(H5::H5Object& target, const std::string& name, const std::string& value);
|
||||
|
||||
void write_attribute(H5::H5Object& target, const std::string& name, int value);
|
||||
|
||||
const boost::any& get_value_from_reference(const std::string& dataset_name, const boost::any& value_reference, const std::map<std::string, boost::any>& values);
|
||||
const boost::any& get_value_from_reference(const std::string& dataset_name,
|
||||
const boost::any& value_reference, const std::unordered_map<std::string, boost::any>& values);
|
||||
|
||||
void write_format_data(H5::Group& file_node, const h5_parent& format_node, const std::map<std::string, h5_value>& values);
|
||||
void write_format(H5::H5File& file, const H5Format& format, const std::map<std::string, h5_value>& input_values);
|
||||
void write_format_data(H5::Group& file_node, const h5_parent& format_node,
|
||||
const std::unordered_map<std::string, h5_value>& values);
|
||||
|
||||
void write_format(H5::H5File& file, const H5Format& format,
|
||||
const std::unordered_map<std::string, h5_value>& input_values);
|
||||
};
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -97,7 +97,7 @@ void RestApi::start_rest_api(WriterManager& writer_manager, uint16_t port)
|
||||
return result;
|
||||
} else {
|
||||
auto request_parameters = crow::json::load(req.body);
|
||||
std::map<std::string, boost::any> new_parameters;
|
||||
std::unordered_map<std::string, boost::any> new_parameters;
|
||||
|
||||
for (const auto& item : request_parameters) {
|
||||
string parameter_name = item.key();
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
WriterManager::WriterManager(const map<string, DATA_TYPE>& parameters_type, const string& output_file, uint64_t n_frames):
|
||||
parameters_type(parameters_type), output_file(output_file), n_frames(n_frames), running_flag(true), killed_flag(false),
|
||||
n_received_frames(0), n_written_frames(0), n_lost_frames(0)
|
||||
WriterManager::WriterManager(const unordered_map<string, DATA_TYPE>& parameters_type,
|
||||
const string& output_file, uint64_t n_frames):
|
||||
parameters_type(parameters_type), output_file(output_file), n_frames(n_frames),
|
||||
running_flag(true), killed_flag(false), n_received_frames(0), n_written_frames(0), n_lost_frames(0)
|
||||
{
|
||||
#ifdef DEBUG_OUTPUT
|
||||
cout << "[WriterManager::WriterManager] Writer manager for n_frames " << n_frames << endl;
|
||||
@@ -54,9 +55,9 @@ string WriterManager::get_output_file() const
|
||||
return output_file;
|
||||
}
|
||||
|
||||
map<string, uint64_t> WriterManager::get_statistics() const
|
||||
unordered_map<string, uint64_t> WriterManager::get_statistics() const
|
||||
{
|
||||
map<string, uint64_t> result = {{"n_received_frames", n_received_frames.load()},
|
||||
unordered_map<string, uint64_t> result = {{"n_received_frames", n_received_frames.load()},
|
||||
{"n_written_frames", n_written_frames.load()},
|
||||
{"n_lost_frames", n_lost_frames.load()},
|
||||
{"total_expected_frames", n_frames}};
|
||||
@@ -64,14 +65,14 @@ map<string, uint64_t> WriterManager::get_statistics() const
|
||||
return result;
|
||||
}
|
||||
|
||||
map<string, boost::any> WriterManager::get_parameters()
|
||||
unordered_map<string, boost::any> WriterManager::get_parameters()
|
||||
{
|
||||
lock_guard<mutex> lock(parameters_mutex);
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
void WriterManager::set_parameters(const map<string, boost::any>& new_parameters)
|
||||
void WriterManager::set_parameters(const unordered_map<string, boost::any>& new_parameters)
|
||||
{
|
||||
lock_guard<mutex> lock(parameters_mutex);
|
||||
|
||||
@@ -96,7 +97,7 @@ void WriterManager::set_parameters(const map<string, boost::any>& new_parameters
|
||||
#endif
|
||||
}
|
||||
|
||||
const map<string, DATA_TYPE>& WriterManager::get_parameters_type() const
|
||||
const unordered_map<string, DATA_TYPE>& WriterManager::get_parameters_type() const
|
||||
{
|
||||
return parameters_type;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef WRITERMANAGER_H
|
||||
#define WRITERMANAGER_H
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
@@ -12,11 +12,11 @@
|
||||
class WriterManager
|
||||
{
|
||||
|
||||
std::map<std::string, boost::any> parameters = {};
|
||||
std::unordered_map<std::string, boost::any> parameters = {};
|
||||
std::mutex parameters_mutex;
|
||||
|
||||
// Initialize in constructor.
|
||||
const std::map<std::string, DATA_TYPE>& parameters_type;
|
||||
const std::unordered_map<std::string, DATA_TYPE>& parameters_type;
|
||||
std::string output_file;
|
||||
size_t n_frames;
|
||||
std::atomic_bool running_flag;
|
||||
@@ -26,7 +26,7 @@ class WriterManager
|
||||
std::atomic<uint64_t> n_lost_frames;
|
||||
|
||||
public:
|
||||
WriterManager(const std::map<std::string, DATA_TYPE>& parameters_type, const std::string& output_file, uint64_t n_frames=0);
|
||||
WriterManager(const std::unordered_map<std::string, DATA_TYPE>& parameters_type, const std::string& output_file, uint64_t n_frames=0);
|
||||
virtual ~WriterManager();
|
||||
|
||||
void stop();
|
||||
@@ -37,11 +37,11 @@ class WriterManager
|
||||
bool are_all_parameters_set();
|
||||
std::string get_output_file() const;
|
||||
|
||||
const std::map<std::string, DATA_TYPE>& get_parameters_type() const;
|
||||
std::map<std::string, boost::any> get_parameters();
|
||||
void set_parameters(const std::map<std::string, boost::any>& new_parameters);
|
||||
const std::unordered_map<std::string, DATA_TYPE>& get_parameters_type() const;
|
||||
std::unordered_map<std::string, boost::any> get_parameters();
|
||||
void set_parameters(const std::unordered_map<std::string, boost::any>& new_parameters);
|
||||
|
||||
std::map<std::string, uint64_t> get_statistics() const;
|
||||
std::unordered_map<std::string, uint64_t> get_statistics() const;
|
||||
void received_frame(size_t frame_index);
|
||||
void written_frame(size_t frame_index);
|
||||
void lost_frame(size_t frame_index);
|
||||
|
||||
+12
-11
@@ -11,8 +11,8 @@ using s_ptr = shared_ptr<h5_base>;
|
||||
|
||||
class NXmxFormat : public H5Format
|
||||
{
|
||||
shared_ptr<map<string, DATA_TYPE>> input_value_type = NULL;
|
||||
shared_ptr<map<string, boost::any>> default_values = NULL;
|
||||
shared_ptr<unordered_map<string, DATA_TYPE>> input_value_type = NULL;
|
||||
shared_ptr<unordered_map<string, boost::any>> default_values = NULL;
|
||||
shared_ptr<h5_group> file_format = NULL;
|
||||
|
||||
public:
|
||||
@@ -23,7 +23,7 @@ class NXmxFormat : public H5Format
|
||||
// Input values definition type.
|
||||
// Which type should be the parameters you receive over the REST api.
|
||||
input_value_type.reset(
|
||||
new map<string, DATA_TYPE>({
|
||||
new unordered_map<string, DATA_TYPE>({
|
||||
{"sl2wv", NX_FLOAT},
|
||||
{"sl0ch", NX_FLOAT},
|
||||
{"sl2wh", NX_FLOAT},
|
||||
@@ -94,7 +94,7 @@ class NXmxFormat : public H5Format
|
||||
}));
|
||||
|
||||
// Default values used in the file format.
|
||||
default_values.reset(new std::map<string, boost::any>(
|
||||
default_values.reset(new std::unordered_map<string, boost::any>(
|
||||
{
|
||||
{"filter_set/description", "The filter set consists of 4 linear stages, each with five filter positions. Additionally each one allows for an out position to allow no filtering."},
|
||||
{"XBPM4/calibration_date", "???"},
|
||||
@@ -905,12 +905,12 @@ class NXmxFormat : public H5Format
|
||||
return *file_format;
|
||||
}
|
||||
|
||||
const map<string, boost::any>& get_default_values() const override {
|
||||
const unordered_map<string, boost::any>& get_default_values() const override {
|
||||
return *default_values;
|
||||
}
|
||||
|
||||
void add_calculated_values(map<string, boost::any>& values) const override {
|
||||
map<string, string> input_mapping = {
|
||||
void add_calculated_values(unordered_map<string, boost::any>& values) const override {
|
||||
unordered_map<string, string> input_mapping = {
|
||||
{"source/distance", "input/samz"},
|
||||
{"slit_0/distance", "input/samz"},
|
||||
{"slit_1/distance", "input/samz"},
|
||||
@@ -924,7 +924,7 @@ class NXmxFormat : public H5Format
|
||||
{"filter_set/attenuator_transmission", "input/ftrans"},
|
||||
};
|
||||
|
||||
map<string, function<double(double)>> functions = {
|
||||
unordered_map<string, function<double(double)>> functions = {
|
||||
{"source/distance", [](double x) -> double { return -33800 - x;}},
|
||||
{"slit_0/distance", [](double x) -> double { return -33800 + 12100 - x;}},
|
||||
{"slit_1/distance", [](double x) -> double { return -33800 + 26000 - x;}},
|
||||
@@ -969,8 +969,9 @@ class NXmxFormat : public H5Format
|
||||
}
|
||||
}
|
||||
|
||||
void add_input_values(map<string, boost::any>& values, const map<string, boost::any>& input_values) const override {
|
||||
map<string, list<string>> input_mapping = {
|
||||
void add_input_values(unordered_map<string, boost::any>& values,
|
||||
const unordered_map<string, boost::any>& input_values) const override {
|
||||
unordered_map<string, list<string>> input_mapping = {
|
||||
{"sl2wv", {"slit_2/y_gap"}},
|
||||
{"sl0ch", {"slit_0/x_translation"}},
|
||||
{"sl2wh", {"slit_2/x_gap"}},
|
||||
@@ -1047,7 +1048,7 @@ class NXmxFormat : public H5Format
|
||||
}
|
||||
}
|
||||
|
||||
const std::map<string, DATA_TYPE>& get_input_value_type() const override {
|
||||
const std::unordered_map<string, DATA_TYPE>& get_input_value_type() const override {
|
||||
return *input_value_type;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user