From 8642d5b695d95a703fd8dbb5bd8a16aa7f3bcabd Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Wed, 31 Jan 2018 14:18:33 +0100 Subject: [PATCH] Const correctness enforcement --- src/H5Format.cpp | 32 ++++++++++++++++---------------- src/H5Format.hpp | 34 +++++++++++++++++----------------- src/H5Writer.cpp | 8 ++++---- src/H5Writer.hpp | 14 +++++++------- src/WriterManager.cpp | 4 ++-- src/WriterManager.hpp | 6 +++--- src/h5_zmq_writer.cpp | 4 ++-- src/nxsas_format.cpp | 8 ++++---- 8 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/H5Format.cpp b/src/H5Format.cpp index 597e8e8..4a53a46 100644 --- a/src/H5Format.cpp +++ b/src/H5Format.cpp @@ -7,7 +7,7 @@ using namespace std; -hsize_t H5Format::expand_dataset(const H5::DataSet& dataset, hsize_t frame_index, hsize_t dataset_increase_step) +hsize_t H5Format::expand_dataset(H5::DataSet& dataset, hsize_t frame_index, hsize_t dataset_increase_step) { hsize_t dataset_rank = 3; hsize_t dataset_dimension[dataset_rank]; @@ -28,7 +28,7 @@ hsize_t H5Format::expand_dataset(const H5::DataSet& dataset, hsize_t frame_index return dataset_dimension[0]; } -void H5Format::compact_dataset(const H5::DataSet& dataset, hsize_t max_frame_index) +void H5Format::compact_dataset(H5::DataSet& dataset, hsize_t max_frame_index) { hsize_t dataset_rank = 3; hsize_t dataset_dimension[dataset_rank]; @@ -47,12 +47,12 @@ void H5Format::compact_dataset(const H5::DataSet& dataset, hsize_t max_frame_ind dataset.extend(dataset_dimension); } -H5::Group H5Format::create_group(H5::Group& target, std::string name) +H5::Group H5Format::create_group(H5::Group& target, const string& name) { return target.createGroup(name.c_str()); } -boost::any H5Format::get_value_from_reference(string& dataset_name, boost::any value_reference, map& values) +const boost::any& H5Format::get_value_from_reference(const string& dataset_name, const boost::any& value_reference, const map& values) { try { auto reference_string = boost::any_cast(value_reference); @@ -72,7 +72,7 @@ boost::any H5Format::get_value_from_reference(string& dataset_name, boost::any v } } -H5::PredType H5Format::get_dataset_data_type(string& type){ +H5::PredType H5Format::get_dataset_data_type(const string& type){ #ifdef DEBUG_OUTPUT cout << "[H5Format::get_dataset_data_type] Getting dataset type for received frame type " << type << endl; @@ -105,7 +105,7 @@ H5::PredType H5Format::get_dataset_data_type(string& type){ } } -H5::DataSet H5Format::write_dataset(H5::Group& target, h5_dataset& dataset, map& values){ +H5::DataSet H5Format::write_dataset(H5::Group& target, const h5_dataset& dataset, const map& values){ string name = dataset.name; boost::any value; @@ -162,7 +162,7 @@ H5::DataSet H5Format::write_dataset(H5::Group& target, h5_dataset& dataset, map< } } -H5::DataSet H5Format::write_dataset(H5::Group& target, string name, double value) +H5::DataSet H5Format::write_dataset(H5::Group& target, const string& name, double value) { H5::DataSpace att_space(H5S_SCALAR); auto data_type = H5::PredType::NATIVE_DOUBLE; @@ -173,7 +173,7 @@ H5::DataSet H5Format::write_dataset(H5::Group& target, string name, double value return dataset; } -H5::DataSet H5Format::write_dataset(H5::Group& target, string name, int value) +H5::DataSet H5Format::write_dataset(H5::Group& target, const string& name, int value) { H5::DataSpace att_space(H5S_SCALAR); auto data_type = H5::PredType::NATIVE_INT; @@ -184,7 +184,7 @@ H5::DataSet H5Format::write_dataset(H5::Group& target, string name, int value) return dataset; } -H5::DataSet H5Format::write_dataset(H5::Group& target, string name, string value) +H5::DataSet H5Format::write_dataset(H5::Group& target, const string& name, const string& value) { H5::DataSpace att_space(H5S_SCALAR); H5::DataType data_type = H5::StrType(0, H5T_VARIABLE); @@ -195,7 +195,7 @@ H5::DataSet H5Format::write_dataset(H5::Group& target, string name, string value return dataset; } -void H5Format::write_attribute(H5::H5Object& target, string name, string value) +void H5Format::write_attribute(H5::H5Object& target, const string& name, const string& value) { H5::DataSpace att_space(H5S_SCALAR); H5::DataType data_type = H5::StrType(H5::PredType::C_S1, H5T_VARIABLE); @@ -204,7 +204,7 @@ void H5Format::write_attribute(H5::H5Object& target, string name, string value) h5_attribute.write(data_type, &value); } -void H5Format::write_attribute(H5::H5Object& target, string name, int value) +void H5Format::write_attribute(H5::H5Object& target, const string& name, int value) { H5::DataSpace att_space(H5S_SCALAR); auto data_type = H5::PredType::NATIVE_INT; @@ -213,7 +213,7 @@ void H5Format::write_attribute(H5::H5Object& target, string name, int value) h5_attribute.write(data_type, &value); } -void H5Format::write_attribute(H5::H5Object& target, h5_attr& attribute, map& values) +void H5Format::write_attribute(H5::H5Object& target, const h5_attr& attribute, const map& values) { string name = attribute.name; boost::any value; @@ -259,10 +259,10 @@ void H5Format::write_attribute(H5::H5Object& target, h5_attr& attribute, map& values) { +void H5Format::write_format_data(H5::Group& file_node, const h5_parent& format_node, const std::map& values) { auto node_group = H5Format::create_group(file_node, format_node.name); - for (auto item : format_node.items) { + for (const auto item : format_node.items) { if (item->node_type == GROUP) { auto sub_group = dynamic_cast(item); @@ -276,7 +276,7 @@ void H5Format::write_format_data(H5::Group& file_node, h5_parent& format_node, s auto sub_dataset = dynamic_cast(item); auto current_dataset = H5Format::write_dataset(node_group, *sub_dataset, values); - for (auto dataset_attr : sub_dataset->items) { + for (const auto dataset_attr : sub_dataset->items) { // You can specify only attributes inside a dataset. if (dataset_attr->node_type != ATTRIBUTE) { @@ -294,7 +294,7 @@ void H5Format::write_format_data(H5::Group& file_node, h5_parent& format_node, s } } -void H5Format::write_format(H5::H5File& file, std::map& input_values, string frames_dataset_name){ +void H5Format::write_format(H5::H5File& file, const std::map& input_values, const string& frames_dataset_name){ auto format = get_format_definition(); auto values = get_default_values(); diff --git a/src/H5Format.hpp b/src/H5Format.hpp index 555c085..a62aca4 100644 --- a/src/H5Format.hpp +++ b/src/H5Format.hpp @@ -67,33 +67,33 @@ struct h5_attr : public h5_base, public h5_data_base { }; namespace H5Format { - hsize_t expand_dataset(const H5::DataSet& dataset, hsize_t frame_index, hsize_t dataset_increase_step); - void compact_dataset(const H5::DataSet& dataset, hsize_t max_frame_index); + 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, std::string name); - H5::PredType get_dataset_data_type(std::string& type); + H5::Group create_group(H5::Group& target, const std::string& name); + H5::PredType get_dataset_data_type(const std::string& type); - H5::DataSet write_dataset(H5::Group& target, h5_dataset& dataset, std::map& values); - H5::DataSet write_dataset(H5::Group& target, std::string name, double value); - H5::DataSet write_dataset(H5::Group& target, std::string name, int value); - H5::DataSet write_dataset(H5::Group& target, std::string name, std::string value); + H5::DataSet write_dataset(H5::Group& target, const h5_dataset& dataset, const std::map& 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, h5_attr& attribute, std::map& values); - void write_attribute(H5::H5Object& target, std::string name, std::string value); - void write_attribute(H5::H5Object& target, std::string name, int value); + void write_attribute(H5::H5Object& target, const h5_attr& attribute, const std::map& 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); - boost::any get_value_from_reference(std::string& dataset_name, boost::any value_reference, std::map& values); + const boost::any& get_value_from_reference(const std::string& dataset_name, const boost::any& value_reference, const std::map& values); - void write_format_data(H5::Group& file_node, h5_parent& format_node, std::map& values); - void write_format(H5::H5File& file, std::map& input_values, std::string frames_dataset_name); + void write_format_data(H5::Group& file_node, const h5_parent& format_node, const std::map& values); + void write_format(H5::H5File& file, const std::map& input_values, const std::string& frames_dataset_name); }; // Move this somewhere else. -std::map* get_input_value_type(); +const std::map* get_input_value_type(); std::map* get_default_values(); -h5_group* get_format_definition(); +const h5_group* get_format_definition(); void add_calculated_values(std::map& values); -void add_input_values(std::map& values, std::map& input_values); +void add_input_values(std::map& values, const std::map& input_values); std::string get_frames_dataset_name(); #endif \ No newline at end of file diff --git a/src/H5Writer.cpp b/src/H5Writer.cpp index 0bf6cca..5d3fe4a 100644 --- a/src/H5Writer.cpp +++ b/src/H5Writer.cpp @@ -12,7 +12,7 @@ extern "C" using namespace std; -H5Writer::H5Writer(const std::string filename, const std::string dataset_name, +H5Writer::H5Writer(const std::string& filename, const std::string& dataset_name, hsize_t frames_per_file, hsize_t initial_dataset_size, hsize_t dataset_increase_step) : filename(filename), dataset_name(dataset_name), frames_per_file(frames_per_file), initial_dataset_size(initial_dataset_size), dataset_increase_step(dataset_increase_step) @@ -74,7 +74,7 @@ void H5Writer::close_file() max_frame_index = 0; } -void H5Writer::write_frame_data(size_t frame_index, size_t* frame_shape, size_t data_bytes_size, char* data, string data_type, string endianness) +void H5Writer::write_frame_data(size_t frame_index, const size_t* frame_shape, size_t data_bytes_size, const char* data, const string& data_type, const string& endianness) { // Define the ofset of the currently received image in the file. hsize_t relative_frame_index = prepare_storage_for_frame(frame_index, frame_shape, data_type, endianness); @@ -92,7 +92,7 @@ void H5Writer::write_frame_data(size_t frame_index, size_t* frame_shape, size_t } } -void H5Writer::create_file(size_t* frame_shape, hsize_t frame_chunk, string& type, string& endianness) +void H5Writer::create_file(const size_t* frame_shape, hsize_t frame_chunk, const string& type, const string& endianness) { if (file.getId() != -1) { @@ -162,7 +162,7 @@ bool H5Writer::is_file_open() return (file.getId() != -1); } -hsize_t H5Writer::prepare_storage_for_frame(size_t frame_index, size_t* frame_shape, string& data_type, string& endianness) +hsize_t H5Writer::prepare_storage_for_frame(size_t frame_index, const size_t* frame_shape, const string& data_type, const string& endianness) { hsize_t relative_frame_index = frame_index; diff --git a/src/H5Writer.hpp b/src/H5Writer.hpp index 85aaac6..8090301 100644 --- a/src/H5Writer.hpp +++ b/src/H5Writer.hpp @@ -7,8 +7,8 @@ class H5Writer { // Initialized in constructor. - std::string filename; - std::string dataset_name; + const std::string filename; + const std::string dataset_name; hsize_t frames_per_file; hsize_t initial_dataset_size; hsize_t dataset_increase_step = 0; @@ -21,17 +21,17 @@ class H5Writer H5::H5File file; H5::DataSet dataset; - hsize_t prepare_storage_for_frame(size_t frame_index, size_t* frame_shape, std::string& data_type, std::string& endianness); - void create_file(size_t* frame_shape, hsize_t frame_chunk, std::string& data_type, std::string& endianness); + hsize_t prepare_storage_for_frame(size_t frame_index, const size_t* frame_shape, const std::string& data_type, const std::string& endianness); + void create_file(const size_t* frame_shape, hsize_t frame_chunk, const std::string& data_type, const std::string& endianness); public: - H5Writer(const std::string filename, const std::string dataset_name, + H5Writer(const std::string& filename, const std::string& dataset_name, hsize_t frames_per_file=0, hsize_t initial_dataset_size=1000, hsize_t dataset_increase_step=1000); ~H5Writer(); bool is_file_open(); void close_file(); - void write_frame_data(size_t frame_index, size_t* frame_shape, size_t data_bytes_size, - char* data, std::string data_type, std::string endianness); + void write_frame_data(size_t frame_index, const size_t* frame_shape, size_t data_bytes_size, + const char* data, const std::string& data_type, const std::string& endianness); H5::H5File& get_h5_file(); }; diff --git a/src/WriterManager.cpp b/src/WriterManager.cpp index ec1bdef..9a505ad 100644 --- a/src/WriterManager.cpp +++ b/src/WriterManager.cpp @@ -5,7 +5,7 @@ using namespace std; -WriterManager::WriterManager(map* parameters_type, uint64_t n_frames): +WriterManager::WriterManager(const map* parameters_type, uint64_t n_frames): parameters_type(parameters_type), n_frames(n_frames), running_flag(true), killed_flag(false), n_received_frames(0), n_written_frames(0) { #ifdef DEBUG_OUTPUT @@ -84,7 +84,7 @@ void WriterManager::set_parameters(map& new_parameters) #endif } -map* WriterManager::get_parameters_type() { +const map* WriterManager::get_parameters_type() { return parameters_type; } diff --git a/src/WriterManager.hpp b/src/WriterManager.hpp index fcc8614..85217c9 100644 --- a/src/WriterManager.hpp +++ b/src/WriterManager.hpp @@ -16,7 +16,7 @@ class WriterManager std::mutex parameters_mutex; // Initialize in constructor. - std::map* parameters_type; + const std::map* parameters_type; size_t n_frames; std::atomic_bool running_flag; std::atomic_bool killed_flag; @@ -24,7 +24,7 @@ class WriterManager std::atomic n_written_frames; public: - WriterManager(std::map* parameters_type, uint64_t n_frames=0); + WriterManager(const std::map* parameters_type, uint64_t n_frames=0); void stop(); void kill(); bool is_running(); @@ -32,7 +32,7 @@ class WriterManager std::string get_status(); bool are_all_parameters_set(); - std::map* get_parameters_type(); + const std::map* get_parameters_type(); std::map get_parameters(); void set_parameters(std::map& new_parameters); diff --git a/src/h5_zmq_writer.cpp b/src/h5_zmq_writer.cpp index c196956..86c911d 100644 --- a/src/h5_zmq_writer.cpp +++ b/src/h5_zmq_writer.cpp @@ -196,10 +196,10 @@ int main (int argc, char *argv[]) { if (argc != 6) { cout << endl; - cout << "Usage: h5_zmq_writer [connection_address] [output_file] [n_frames] [rest_port]" << endl; + cout << "Usage: h5_zmq_writer [connection_address] [output_file] [n_frames] [rest_port] [user_id]" << endl; cout << "\tconnection_address: Address to connect to the stream (PULL). Example: tcp://127.0.0.1:40000" << endl; cout << "\toutput_file: Name of the output file." << endl; - cout << "\n_frames: Number of images to acquire. 0 for infinity (untill /stop is called)." << endl; + cout << "\t_frames: Number of images to acquire. 0 for infinity (untill /stop is called)." << endl; cout << "\trest_port: Port to start the REST Api on." << endl; cout << "\tuser_id: uid under which to run the writer. -1 to leave it as it is." << endl; cout << endl; diff --git a/src/nxsas_format.cpp b/src/nxsas_format.cpp index 54df871..aa1eb1a 100644 --- a/src/nxsas_format.cpp +++ b/src/nxsas_format.cpp @@ -11,7 +11,7 @@ string get_frames_dataset_name() { return "entry/plottable_data/data"; } -h5_group* get_format_definition(){ +const h5_group* get_format_definition(){ auto format = new h5_group("entry", { @@ -883,7 +883,7 @@ void add_calculated_values(map& values){ } } -void add_input_values(map& values, map& input_values) { +void add_input_values(map& values, const map& input_values) { map> input_mapping = { {"sl2wv", {"slit_2/y_gap"}}, {"sl0ch", {"slit_0/x_translation"}}, @@ -956,12 +956,12 @@ void add_input_values(map& values, map& for (auto input : input_mapping) { for (auto destination_name : input.second) { - values[destination_name] = input_values[input.first]; + values[destination_name] = input_values.at(input.first); } } } -std::map* get_input_value_type() { +const std::map* get_input_value_type() { auto input_value_types = new std::map({ {"sl2wv", NX_FLOAT}, {"sl0ch", NX_FLOAT},