Commit 1fed3882 authored by Håkan Wennlöf's avatar Håkan Wennlöf Committed by Simon Spannagel
Browse files

Tidied and tightened up the definition of Get

(cherry picked from commit b00b2541)
parent d31aa4fd
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -104,7 +104,6 @@ allpix::Units::UnitType Units::get(const std::string& str) {
    } else if(lst == '/') {
        ret_value = getSingleInverse(ret_value, std::move(unit));
    }
    std::cout << "Return value from Units: " << ret_value << std::endl;
    return ret_value;
}

+1 −2
Original line number Diff line number Diff line
@@ -88,8 +88,7 @@ namespace allpix {
         * @param str Name of that particular unit
         * @return Value in the base unit
         */
        template <typename T, std::enable_if_t<!std::is_integral_v<T>, bool> = true> static T get(T inp, const std::string& str);
        template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true> static T get(T inp, const std::string& str);
        template <typename T> static T get(T inp, const std::string& str);
        /**
         * @brief Get input parameter in the inverse of the base units
         * @param inp Value in a particular unit
+16 −16
Original line number Diff line number Diff line
@@ -17,33 +17,33 @@
namespace allpix {
    /**
     * @throws std::overflow_error If the converted unit overflows the requested type
     * @throws std::invalid_argument If the resulting product of value and unit is not an integral value
     *
     * The unit type is internally converted to the type \ref Units::UnitType. After multiplying the unit, the output is
     * checked for overflow problems before the type is converted back to the original type.
     */
    // std::enable_if<std::is_integral_v<T>>
    template <typename T, std::enable_if_t<!std::is_integral_v<T>, bool> = true>
    T Units::get(T inp, const std::string& str) {
    template <typename T> T Units::get(T inp, const std::string& str) {
        UnitType out = static_cast<UnitType>(inp) * get(str);
        if(out > static_cast<UnitType>(std::numeric_limits<T>::max()) ||
           out < static_cast<UnitType>(std::numeric_limits<T>::lowest())) {
            throw std::overflow_error("unit conversion overflows the type");
        }
        if constexpr(!std::is_integral_v<T>) {
            std::cout << "--- Regular one used! Input: " << inp << std::endl;
            std::cout << "--- Regular one used! Value: " << out << std::endl;
            return static_cast<T>(out);
    }

    template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true> T Units::get(T inp, const std::string& str) {
        UnitType out = static_cast<UnitType>(inp) * get(str);
        if(out > static_cast<UnitType>(std::numeric_limits<T>::max()) ||
           out < static_cast<UnitType>(std::numeric_limits<T>::lowest())) {
            throw std::overflow_error("unit conversion overflows the type");
        }
        } else {
            if constexpr(std::is_integral_v<T>) {
                std::cout << "--- INteger one used! Input: " << inp << std::endl;
                std::cout << "--- INteger one used! Value: " << out << std::endl;
                if(out != static_cast<T>(out)) {
                    throw std::invalid_argument("Cannot use integer value with non-integer internal unit; the combination " +
                                                std::to_string(inp) + " " + str + " is invalid.");
                }
                return static_cast<T>(out);
            }
        }
    }

    // Getters for single and inverse units
    template <typename T> T Units::getSingle(T inp, std::string str) {