Skip to content

Commit

Permalink
Use [[nodiscard]] to make sure optional parameters have been set
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAce committed Oct 31, 2020
1 parent afba3af commit a81367b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
34 changes: 17 additions & 17 deletions h5pp/include/h5pp/details/h5ppHdf5.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,11 @@ namespace h5pp::hdf5 {
}

template<typename T>
std::tuple<std::type_index, std::string, size_t> getCppType() {
[[nodiscard]] std::tuple<std::type_index, std::string, size_t> getCppType() {
return {typeid(T), std::string(h5pp::type::sfinae::type_name<T>()), sizeof(T)};
}

inline std::tuple<std::type_index, std::string, size_t> getCppType(const hid::h5t &type) {
[[nodiscard]] inline std::tuple<std::type_index, std::string, size_t> getCppType(const hid::h5t &type) {
using namespace h5pp::type::compound;
/* clang-format off */
if(H5Tequal(type, H5T_NATIVE_SHORT)) return getCppType<short>();
Expand Down Expand Up @@ -733,7 +733,7 @@ namespace h5pp::hdf5 {
return getCppType<std::nullopt_t>();
}

inline TypeInfo getTypeInfo(std::optional<std::string> objectPath,
[[nodiscard]]inline TypeInfo getTypeInfo(std::optional<std::string> objectPath,
std::optional<std::string> objectName,
const hid::h5s & h5Space,
const hid::h5t & h5Type) {
Expand All @@ -749,31 +749,31 @@ namespace h5pp::hdf5 {
return typeInfo;
}

inline TypeInfo getTypeInfo(const hid::h5d &dataset) {
[[nodiscard]]inline TypeInfo getTypeInfo(const hid::h5d &dataset) {
auto dsetPath = h5pp::hdf5::getName(dataset);
h5pp::logger::log->trace("Collecting type info about dataset [{}]", dsetPath);
return getTypeInfo(dsetPath, std::nullopt, H5Dget_space(dataset), H5Dget_type(dataset));
}

template<typename h5x, typename = h5pp::type::sfinae::enable_if_is_h5_loc_or_hid_t<h5x>>
// enable_if so the compiler doesn't think it can use overload with std::string on first argument
inline TypeInfo getTypeInfo(const h5x & loc,
[[nodiscard]]inline TypeInfo getTypeInfo(const h5x & loc,
std::string_view dsetName,
std::optional<bool> dsetExists = std::nullopt,
const hid::h5p & dsetAccess = H5P_DEFAULT) {
auto dataset = openLink<hid::h5d>(loc, dsetName, dsetExists, dsetAccess);
return getTypeInfo(dataset);
}

inline TypeInfo getTypeInfo(const hid::h5a &attribute) {
[[nodiscard]]inline TypeInfo getTypeInfo(const hid::h5a &attribute) {
auto attrName = getAttributeName(attribute);
auto linkPath = getName(attribute); // Returns the name of the link which has the attribute
h5pp::logger::log->trace("Collecting type info about attribute [{}] in link [{}]", attrName, linkPath);
return getTypeInfo(linkPath, attrName, H5Aget_space(attribute), H5Aget_type(attribute));
}

template<typename h5x>
inline TypeInfo getTypeInfo(const h5x & loc,
[[nodiscard]] inline TypeInfo getTypeInfo(const h5x & loc,
std::string_view linkPath,
std::string_view attrName,
std::optional<bool> linkExists = std::nullopt,
Expand All @@ -795,7 +795,7 @@ namespace h5pp::hdf5 {
}

template<typename h5x>
std::vector<TypeInfo> getTypeInfo_allAttributes(const h5x &link) {
[[nodiscard]] std::vector<TypeInfo> getTypeInfo_allAttributes(const h5x &link) {
static_assert(h5pp::type::sfinae::is_h5_link_or_hid_v<h5x>,
"Template function [h5pp::hdf5::getTypeInfo_allAttributes(const h5x & link)] requires type h5x to be: "
"[h5pp::hid::h5d], [h5pp::hid::h5g], [h5pp::hid::h5o] or [hid_t]");
Expand All @@ -813,7 +813,7 @@ namespace h5pp::hdf5 {
}

template<typename h5x>
inline std::vector<TypeInfo> getTypeInfo_allAttributes(const h5x & loc,
[[nodiscard]] inline std::vector<TypeInfo> getTypeInfo_allAttributes(const h5x & loc,
std::string_view linkPath,
std::optional<bool> linkExists = std::nullopt,
const hid::h5p & linkAccess = H5P_DEFAULT) {
Expand Down Expand Up @@ -1473,7 +1473,7 @@ namespace h5pp::hdf5 {
}

template<H5O_type_t ObjType>
inline constexpr std::string_view getObjTypeName() {
[[nodiscard]] inline constexpr std::string_view getObjTypeName() {
if constexpr(ObjType == H5O_type_t::H5O_TYPE_DATASET)
return "dataset";
else if constexpr(ObjType == H5O_type_t::H5O_TYPE_GROUP)
Expand Down Expand Up @@ -1523,7 +1523,7 @@ namespace h5pp::hdf5 {
}

template<H5O_type_t ObjType, typename h5x>
inline std::vector<std::string> findLinks(const h5x & loc,
[[nodiscard]] inline std::vector<std::string> findLinks(const h5x & loc,
std::string_view searchKey = "",
std::string_view searchRoot = "/",
long maxHits = -1,
Expand All @@ -1548,7 +1548,7 @@ namespace h5pp::hdf5 {
}

template<H5O_type_t ObjType, typename h5x>
inline std::vector<std::string>
[[nodiscard]] inline std::vector<std::string>
getContentsOfLink(const h5x &loc, std::string_view linkPath, long maxDepth = 1, const hid::h5p &linkAccess = H5P_DEFAULT) {
std::vector<std::string> contents;
internal::maxHits = -1;
Expand Down Expand Up @@ -1611,7 +1611,7 @@ namespace h5pp::hdf5 {
}

template<typename DataType>
std::vector<const char *> getCharPtrVector(const DataType &data) {
[[nodiscard]] std::vector<const char *> getCharPtrVector(const DataType &data) {
std::vector<const char *> sv;
if constexpr(h5pp::type::sfinae::is_text_v<DataType> and h5pp::type::sfinae::has_data_v<DataType>) // Takes care of std::string
sv.push_back(data.data());
Expand Down Expand Up @@ -1967,7 +1967,7 @@ namespace h5pp::hdf5 {
}
}

inline bool fileIsValid(const fs::path &filePath) { return fs::exists(filePath) and H5Fis_hdf5(filePath.string().c_str()) > 0; }
[[nodiscard]] inline bool fileIsValid(const fs::path &filePath) { return fs::exists(filePath) and H5Fis_hdf5(filePath.string().c_str()) > 0; }

[[nodiscard]] inline fs::path getAvailableFileName(const fs::path &filePath) {
int i = 1;
Expand All @@ -1985,7 +1985,7 @@ namespace h5pp::hdf5 {
return newFilePath;
}

inline h5pp::FilePermission convertFileAccessFlags(unsigned int H5F_ACC_FLAGS) {
[[nodiscard]] inline h5pp::FilePermission convertFileAccessFlags(unsigned int H5F_ACC_FLAGS) {
h5pp::FilePermission permission = h5pp::FilePermission::RENAME;
if((H5F_ACC_FLAGS & (H5F_ACC_TRUNC | H5F_ACC_EXCL)) == (H5F_ACC_TRUNC | H5F_ACC_EXCL))
throw std::runtime_error("File access modes H5F_ACC_EXCL and H5F_ACC_TRUNC are mutually exclusive");
Expand All @@ -1996,7 +1996,7 @@ namespace h5pp::hdf5 {
return permission;
}

inline unsigned int convertFileAccessFlags(h5pp::FilePermission permission) {
[[nodiscard]] inline unsigned int convertFileAccessFlags(h5pp::FilePermission permission) {
unsigned int H5F_ACC_MODE = H5F_ACC_RDONLY;
if(permission == h5pp::FilePermission::COLLISION_FAIL) H5F_ACC_MODE |= H5F_ACC_EXCL;
if(permission == h5pp::FilePermission::REPLACE) H5F_ACC_MODE |= H5F_ACC_TRUNC;
Expand All @@ -2006,7 +2006,7 @@ namespace h5pp::hdf5 {
return H5F_ACC_MODE;
}

inline fs::path
[[nodiscard]] inline fs::path
createFile(const h5pp::fs::path &filePath_, const h5pp::FilePermission &permission, const PropertyLists &plists = PropertyLists()) {
fs::path filePath = fs::absolute(filePath_);
fs::path fileName = filePath_.filename();
Expand Down
24 changes: 12 additions & 12 deletions h5pp/include/h5pp/details/h5ppScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace h5pp::scan {
* @param plists (optional) access property for the file. Used to determine link access property when searching for the dataset.
*/
template<typename h5x>
inline h5pp::DsetInfo readDsetInfo(const h5x &loc, const Options &options, const PropertyLists &plists = PropertyLists()) {
[[nodiscard]] inline h5pp::DsetInfo readDsetInfo(const h5x &loc, const Options &options, const PropertyLists &plists = PropertyLists()) {
if(not options.linkPath) throw std::runtime_error("Could not read dataset info: No dataset path was given in options");
h5pp::DsetInfo info;
readDsetInfo(info, loc, options, plists);
Expand Down Expand Up @@ -192,7 +192,7 @@ namespace h5pp::scan {
* @param plists (optional) access property for the file. Used to determine link access property when searching for the dataset.
*/
template<typename DataType, typename h5x>
inline h5pp::DsetInfo inferDsetInfo(const h5x & loc,
[[nodiscard]] inline h5pp::DsetInfo inferDsetInfo(const h5x & loc,
const DataType & data,
const Options & options = Options(),
const PropertyLists &plists = PropertyLists()) {
Expand Down Expand Up @@ -404,7 +404,7 @@ namespace h5pp::scan {

/*! \brief Creates and returns a populated AttrInfo object with properties read from file */
template<typename h5x>
inline h5pp::AttrInfo readAttrInfo(const h5x &loc, const Options &options, const PropertyLists &plists = PropertyLists()) {
[[nodiscard]] inline h5pp::AttrInfo readAttrInfo(const h5x &loc, const Options &options, const PropertyLists &plists = PropertyLists()) {
h5pp::AttrInfo info;
readAttrInfo(info, loc, options, plists);
return info;
Expand Down Expand Up @@ -484,7 +484,7 @@ namespace h5pp::scan {
* If the attribute exists properties are read from file.
* Otherwise properties are inferred from given data. */
template<typename DataType, typename h5x>
inline h5pp::AttrInfo
[[nodiscard]] inline h5pp::AttrInfo
inferAttrInfo(const h5x &loc, const DataType &data, const Options &options, const PropertyLists &plists = PropertyLists()) {
h5pp::AttrInfo info;
inferAttrInfo(info, loc, data, options, plists);
Expand Down Expand Up @@ -540,7 +540,7 @@ namespace h5pp::scan {

/*! \brief Creates and returns a populated AttrInfo object based entirely on given options */
template<typename h5x>
inline h5pp::AttrInfo makeAttrInfo(const h5x &loc, const Options &options, const PropertyLists &plists = PropertyLists()) {
[[nodiscard]] inline h5pp::AttrInfo makeAttrInfo(const h5x &loc, const Options &options, const PropertyLists &plists = PropertyLists()) {
h5pp::AttrInfo info;
inferAttrInfo(info, loc, options, plists);
return info;
Expand Down Expand Up @@ -572,9 +572,9 @@ namespace h5pp::scan {
}

if(info.attrExists.value())
return readAttrInfo(info,loc,options,plists); // Table exists so we can read properties from file
readAttrInfo(info,loc,options,plists); // Table exists so we can read properties from file
else
return makeAttrInfo(info,loc,options,plists);
makeAttrInfo(info,loc,options,plists);
}


Expand Down Expand Up @@ -685,7 +685,7 @@ namespace h5pp::scan {
}

template<typename h5x>
inline TableInfo readTableInfo(const h5x &loc, const Options &options, const PropertyLists &plists = PropertyLists()) {
[[nodiscard]] inline TableInfo readTableInfo(const h5x &loc, const Options &options, const PropertyLists &plists = PropertyLists()) {
TableInfo info;
readTableInfo(info, loc, options, plists);
return info;
Expand All @@ -698,7 +698,7 @@ namespace h5pp::scan {
if(info.tableExists.value()) return;
h5pp::logger::log->debug("Creating metadata for new table [{}]", options.linkPath.value());
if(not options.h5Type and not info.h5Type)
throw std::runtime_error("Could not make table info: No hdf5 compound type path was given");
throw std::runtime_error("Could not make table info: No hdf5 compound type was given");

if(not info.tablePath) info.tablePath = h5pp::util::safe_str(options.linkPath.value());

Expand Down Expand Up @@ -758,7 +758,7 @@ namespace h5pp::scan {


template<typename h5x>
inline h5pp::TableInfo
[[nodiscard]] inline h5pp::TableInfo
makeTableInfo(const h5x &loc, const Options &options, std::string_view tableTitle, const PropertyLists &plists = PropertyLists()) {
TableInfo info;
makeTableInfo(info, loc, options, tableTitle, plists);
Expand All @@ -779,9 +779,9 @@ namespace h5pp::scan {
info.tableExists = h5pp::hdf5::checkIfLinkExists(info.getLocId(), info.tablePath.value(), plists.linkAccess);

if(info.tableExists.value())
return readTableInfo(info,loc,options,plists); // Table exists so we can read properties from file
readTableInfo(info,loc,options,plists); // Table exists so we can read properties from file
else if(not info.tableExists.value() and info.tableTitle)
return makeTableInfo(info,loc,options, info.tableTitle.value(),plists);
makeTableInfo(info,loc,options, info.tableTitle.value(),plists);
else if(not info.tableTitle)
throw std::runtime_error(h5pp::format("Could not infer table info for new table [{}]: No table title given", info.tablePath.value()));
}
Expand Down
10 changes: 5 additions & 5 deletions h5pp/include/h5pp/details/h5ppUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
namespace h5pp::util {

inline std::string safe_str(std::string_view str) {
[[nodiscard]] inline std::string safe_str(std::string_view str) {
// This function removes null-terminating characters inside of strings. For instance
// "This is \0 a string with\0 embedded null characters"
// This happens sometimes for instance when concatenating strings that are "non-standard", i.e.
Expand All @@ -32,7 +32,7 @@ namespace h5pp::util {
}

template<typename PtrType, typename DataType>
inline PtrType getVoidPointer(DataType &data) {
[[nodiscard]] inline PtrType getVoidPointer(DataType &data) {
// Get the memory address to a data buffer

if constexpr(h5pp::type::sfinae::has_data_v<DataType>)
Expand Down Expand Up @@ -605,7 +605,7 @@ namespace h5pp::util {
// enable_if so the compiler doesn't think it can use overload with fs::path those arguments
typename = h5pp::type::sfinae::enable_if_is_h5_loc_or_hid_t<h5xa>,
typename = h5pp::type::sfinae::enable_if_is_h5_loc_or_hid_t<h5xb>>
inline bool onSameFile(const h5xa & loca, const h5xb & locb, LocationMode locMode = LocationMode::DETECT){
[[nodiscard]] inline bool onSameFile(const h5xa & loca, const h5xb & locb, LocationMode locMode = LocationMode::DETECT){
switch(locMode){
case LocationMode::SAME_FILE: return true;
case LocationMode::OTHER_FILE: return false;
Expand All @@ -621,7 +621,7 @@ namespace h5pp::util {
}
}

inline bool onSameFile(const h5pp::fs::path & patha, const h5pp::fs::path & pathb, LocationMode locMode = LocationMode::DETECT){
[[nodiscard]] inline bool onSameFile(const h5pp::fs::path & patha, const h5pp::fs::path & pathb, LocationMode locMode = LocationMode::DETECT){
switch(locMode){
case LocationMode::SAME_FILE: return true;
case LocationMode::OTHER_FILE: return false;
Expand All @@ -631,7 +631,7 @@ namespace h5pp::util {
}
}

inline LocationMode getLocationMode(const h5pp::fs::path & patha, const h5pp::fs::path & pathb){
[[nodiscard]] inline LocationMode getLocationMode(const h5pp::fs::path & patha, const h5pp::fs::path & pathb){
if(h5pp::fs::equivalent(patha,pathb)) return LocationMode::SAME_FILE;
else return LocationMode::OTHER_FILE;
}
Expand Down

0 comments on commit a81367b

Please sign in to comment.