Skip to content

Commit

Permalink
Split object.hpp into multiple header files
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoemi09 authored and Wentzell committed Apr 24, 2024
1 parent 0b446ec commit 619d471
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 49 deletions.
59 changes: 59 additions & 0 deletions c++/h5/complex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2019-2022 Simons Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Authors: Olivier Parcollet, Nils Wentzell

/**
* @file
* @brief Provides a compound type and type traits for complex numbers.
*/

#ifndef LIBH5_COMPLEX_HPP
#define LIBH5_COMPLEX_HPP

#include <complex>
#include <type_traits>

namespace h5 {

/**
* @brief A complex compound type consisting of two doubles to represent a complex number.
* @details This type can be used to read/write complex numbers from/to HDF5 files.
*/
struct dcplx_t {
/// Real part.
double r;

/// Imaginary part.
double i;
};

// Type trait to check if a type is std::complex.
template <typename T>
struct _is_complex : std::false_type {};

// Specialization of h5::_is_complex for std::complex.
template <typename T>
struct _is_complex<std::complex<T>> : std::true_type {};

/**
* @brief Boolean type trait set to true for std::complex types.
* @tparam T Type to check.
*/
template <typename T>
constexpr bool is_complex_v = _is_complex<T>::value;

} // namespace h5

#endif // LIBH5_COMPLEX_HPP
7 changes: 5 additions & 2 deletions c++/h5/h5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@

#include <concepts>

#include "./array_interface.hpp"
#include "./complex.hpp"
#include "./file.hpp"
#include "./group.hpp"
#include "./format.hpp"
#include "./generic.hpp"
#include "./group.hpp"
#include "./object.hpp"
#include "./scalar.hpp"
#include "./stl/string.hpp"
#include "./stl/array.hpp"
Expand All @@ -36,7 +40,6 @@
#include "./stl/tuple.hpp"
#include "./stl/optional.hpp"
#include "./stl/variant.hpp"
#include "./generic.hpp"

// Define this so cpp2py modules know whether hdf5 was included
#define H5_INTERFACE_INCLUDED
Expand Down
1 change: 1 addition & 0 deletions c++/h5/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//
// Authors: Olivier Parcollet, Nils Wentzell

#include "./complex.hpp"
#include "./macros.hpp"
#include "./object.hpp"

Expand Down
45 changes: 0 additions & 45 deletions c++/h5/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@
#ifndef LIBH5_OBJECT_HPP
#define LIBH5_OBJECT_HPP

#include <complex>
#include <cstdint>
#include <stdexcept>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>

namespace h5 {
Expand Down Expand Up @@ -56,47 +52,6 @@ namespace h5 {
/// Vector of h5::hsize_t used throughout the h5 library.
using v_t = std::vector<hsize_t>;

/**
* @brief A complex compound type consisting of two doubles to represent a complex number.
* @details This type can be used to read/write complex numbers from/to HDF5 files.
*/
struct dcplx_t {
/// Real part.
double r;

/// Imaginary part.
double i;
};

// Type trait to check if a type is std::complex.
template <typename T>
struct _is_complex : std::false_type {};

// Specialization of h5::_is_complex for std::complex.
template <typename T>
struct _is_complex<std::complex<T>> : std::true_type {};

/**
* @brief Boolean type trait set to true for std::complex types.
* @tparam T Type to check.
*/
template <typename T>
constexpr bool is_complex_v = _is_complex<T>::value;

/**
* @brief Create a std::runtime_error with an error message constructed from the given arguments.
*
* @tparam Ts Types of the arguments.
* @param ts Arguments streamed into the error message string.
* @return std::runtime_error.
*/
template <typename... Ts>
[[nodiscard]] std::runtime_error make_runtime_error(Ts const &...ts) {
std::stringstream ss;
(ss << ... << ts);
return std::runtime_error{ss.str()};
}

/**
* @brief A generic handle for HDF5 objects.
*
Expand Down
1 change: 1 addition & 0 deletions c++/h5/scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define LIBH5_SCALAR_HPP

#include "./array_interface.hpp"
#include "./complex.hpp"
#include "./group.hpp"
#include "./macros.hpp"
#include "./object.hpp"
Expand Down
1 change: 1 addition & 0 deletions c++/h5/stl/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define LIBH5_STL_ARRAY_HPP

#include "../array_interface.hpp"
#include "../complex.hpp"
#include "../macros.hpp"

#include <algorithm>
Expand Down
3 changes: 2 additions & 1 deletion c++/h5/stl/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
//
// Authors: Olivier Parcollet, Nils Wentzell

#include "../macros.hpp"
#include "./string.hpp"
#include "../macros.hpp"
#include "../utils.hpp"

#include <hdf5.h>
#include <hdf5_hl.h>
Expand Down
4 changes: 3 additions & 1 deletion c++/h5/stl/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
#ifndef LIBH5_STL_VECTOR_HPP
#define LIBH5_STL_VECTOR_HPP

#include "./string.hpp"
#include "../complex.hpp"
#include "../format.hpp"
#include "../group.hpp"
#include "../scalar.hpp"
#include "./string.hpp"
#include "../utils.hpp"

#include <string>
#include <vector>
Expand Down
46 changes: 46 additions & 0 deletions c++/h5/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2019-2022 Simons Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Authors: Olivier Parcollet, Nils Wentzell

/**
* @file
* @brief Provides some utility functions for h5.
*/

#ifndef LIBH5_UTILS_HPP
#define LIBH5_UTILS_HPP

#include <stdexcept>
#include <sstream>

namespace h5 {

/**
* @brief Create a std::runtime_error with an error message constructed from the given arguments.
*
* @tparam Ts Types of the arguments.
* @param ts Arguments streamed into the error message string.
* @return std::runtime_error.
*/
template <typename... Ts>
[[nodiscard]] std::runtime_error make_runtime_error(Ts const &...ts) {
std::stringstream ss;
(ss << ... << ts);
return std::runtime_error{ss.str()};
}

} // namespace h5

#endif // LIBH5_UTILS_HPP

0 comments on commit 619d471

Please sign in to comment.