h5cpp  0.6
A modern C++ wrapper for the HDF5 C library
utilities.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2021 eugen.wintersberger@gmail.com, DESY, ESS
3 //
4 // This file is part of h5cpp
5 //
6 // This library is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation; either version 2.1 of the License, or
9 // (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the
18 // Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
19 // Boston, MA 02110-1301 USA
20 //
21 // ===========================================================================
22 //
23 // Created on: Aug 8, 2021
24 // Author: Eugen Wintersberger <eugen.wintersberger@gmail.com>
25 //
26 #pragma once
27 
28 #include <limits>
29 #include <type_traits>
30 #include <utility>
31 #include <stdexcept>
32 
49 template<typename TType, typename SType>
50 TType unsigned2signed(SType &&source_value) {
51  using stripped_source_t = typename std::remove_reference<SType>::type;
52  using stripped_target_t = typename std::remove_reference<TType>::type;
53  // provide compile time errors if types are not appropriate
54  static_assert(std::is_integral<stripped_target_t>::value &&
55  std::is_signed<stripped_target_t>::value,
56  "target type must be a signed integral type");
57  static_assert(std::is_integral<stripped_source_t>::value &&
58  std::is_unsigned<stripped_source_t>::value,
59  "source type must be an unsigned integral type");
60 
61  using target_limits = std::numeric_limits<stripped_target_t>;
62  // if the source value is smaller than the maximum positive value of the
63  // signed target type
64  if(source_value <= static_cast<stripped_source_t>((target_limits::max)())) {
65 #ifdef __clang__
66 #pragma clang diagnostic push
67 #pragma clang diagnostic ignored "-Wsign-conversion"
68 #endif
69  return static_cast<TType>(std::forward<SType>(source_value));
70 #ifdef __clang__
71 #pragma clang diagnostic pop
72 #endif
73  } else {
74  throw std::range_error("unsigned source value is too large for signed type");
75  }
76 }
77 
78 template<typename TType, typename SType>
79 TType signed2unsigned(SType &&source_value) {
80  using stripped_source_t = typename std::remove_reference<SType>::type;
81  using stripped_target_t = typename std::remove_reference<TType>::type;
82  // provide compile time errors if types are not appropriate
83  static_assert(std::is_integral<stripped_source_t>::value &&
84  std::is_signed<stripped_source_t>::value,
85  "source type must be an unsigned integral type");
86  static_assert(std::is_integral<stripped_target_t>::value &&
87  std::is_unsigned<stripped_target_t>::value,
88  "target type must be a signed integral type");
89 
90  using target_limits = std::numeric_limits<stripped_target_t>;
91  if(source_value < 0) {
92  throw std::range_error("input value is < 0 -> cannot be converted to unsigned");
93  }
94 #ifdef __clang__
95 #pragma clang diagnostic push
96 #pragma clang diagnostic ignored "-Wsign-compare"
97 #pragma clang diagnostic ignored "-Wsign-conversion"
98 #endif
99  if(static_cast<stripped_target_t>(source_value) <= (target_limits::max)()) {
100  return static_cast<stripped_target_t>(std::forward<SType>(source_value));
101 #ifdef __clang__
102 #pragma clang diagnostic pop
103 #endif
104  } else {
105  throw std::range_error("signed input value does not fit into the requested unsigned target type");
106  }
107 
108 }
TType unsigned2signed(SType &&source_value)
convert unsigned to signed integers
Definition: utilities.hpp:50
TType signed2unsigned(SType &&source_value)
Definition: utilities.hpp:79