diff options
author | 2019-10-31 11:22:49 -0700 | |
---|---|---|
committer | 2019-10-31 11:22:49 -0700 | |
commit | 6a6ac6d4604b846959b56cd07fdf7935dc9c48d1 (patch) | |
tree | fd5db73702fb3d93c17fec52049475b018687772 /Source/QED/QedTableParserHelperFunctions.H | |
parent | eb47baaba039722df73a08f15c38c12535179bc4 (diff) | |
parent | 7cf08f6e336457b4a48874985712087faa984b64 (diff) | |
download | WarpX-6a6ac6d4604b846959b56cd07fdf7935dc9c48d1.tar.gz WarpX-6a6ac6d4604b846959b56cd07fdf7935dc9c48d1.tar.zst WarpX-6a6ac6d4604b846959b56cd07fdf7935dc9c48d1.zip |
merging with changes in the dev. Specifically changing boosted to back-transformed
Diffstat (limited to 'Source/QED/QedTableParserHelperFunctions.H')
-rw-r--r-- | Source/QED/QedTableParserHelperFunctions.H | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Source/QED/QedTableParserHelperFunctions.H b/Source/QED/QedTableParserHelperFunctions.H new file mode 100644 index 000000000..9f9f37017 --- /dev/null +++ b/Source/QED/QedTableParserHelperFunctions.H @@ -0,0 +1,90 @@ +#ifndef WARPX_amrex_qed_table_parser_helper_functions_h_ +#define WARPX_amrex_qed_table_parser_helper_functions_h_ + +/** + * This header contains helper functions to safely extract data + * (e.g. integers, floating point numbers) from raw binary data + * (i.e. a char*) and to convert arrays into raw binary data. + */ + +#include <AMReX_Vector.H> +#include <tuple> + +namespace QedUtils{ + /** + * This function safely extracts an amrex::Vector<T> from raw binary data. + * T must be a simple datatype (e.g. an int, a float, a double...). + * + * @param[in] p_char a pointer to the binary stream + * @param[in] how_many how many T should be read from stream + * @param[in] p_last a pointer to the last element of the char* array + * @return {a tuple containing + * 1) flag (which is false if p_last is exceeded) + * 2) a Vector of T + * 3) a pointer to a new location of the binary data (after having read how_many T)} + */ + template <class T> + std::tuple<bool, amrex::Vector<T>, const char*>parse_raw_data_vec( + const char* p_data, size_t how_many, const char* const p_last) + { + amrex::Vector<T> res; + if(p_data + sizeof(T)*how_many > p_last) + return std::make_tuple(false, res, nullptr); + + auto r_data = reinterpret_cast<const T*>(p_data); + + res.assign(r_data, r_data + how_many); + + p_data += sizeof(T)*how_many; + return std::make_tuple(true, res, p_data); + } + + /** + * This function safely extracts a T from raw binary data. + * T must be a simple datatype (e.g. an int, a float, a double...). + * + * @param[in] p_char a pointer to the binary stream + * @param[in] p_last a pointer to the last element of the char* array + * @return {a tuple containing + * 1) flag (which is false if p_last is exceeded) + * 2) a T + * 3) a pointer to a new location of the binary data (after having read 1 T)} + */ + template <class T> + std::tuple<bool, T, const char*> parse_raw_data( + const char* p_data, const char* const p_last) + { + T res; + if(p_data + sizeof(T) > p_last) + return std::make_tuple(false, res, nullptr); + + auto r_data = reinterpret_cast<const T*>(p_data); + + res = *r_data; + + p_data += sizeof(T); + return std::make_tuple(true, res, p_data); + } + + /** + * This function converts a C-style array of T into + * a Vector<char> (i.e. raw binary data) and adds it + * to an existing Vector<char> passed by reference + * @param[in] p_data a pointer to the beginning of the array + * @param[in] how_many number of elements of type T in the array + * @param[in,out] raw_data data will be appended to this vector + */ + template <class T> + void add_data_to_vector_char ( + const T* p_data, size_t how_many, amrex::Vector<char>& raw_data) + { + raw_data.insert( + raw_data.end(), + reinterpret_cast<const char*>(p_data), + reinterpret_cast<const char*>(p_data) + + sizeof(T)*how_many + ); + } +}; + +#endif //WARPX_amrex_qed_table_parser_helper_functions_h_ |