Commit 807f506b authored by Håkan Wennlöf's avatar Håkan Wennlöf
Browse files

Separated template function for getting into not-int and int.

Throwing when it's not okay.
parent a38fef70
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@
#include <string>
#include <type_traits>

#include <iostream>

#include "text.h"

using namespace allpix;
@@ -102,6 +104,7 @@ 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;
}

+2 −1
Original line number Diff line number Diff line
@@ -88,7 +88,8 @@ namespace allpix {
         * @param str Name of that particular unit
         * @return Value in the base unit
         */
        template <typename T> 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, std::enable_if_t<std::is_integral_v<T>, bool> = true> 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
+26 −3
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
#include "text.h"

#include <cmath>
#include <iostream>

namespace allpix {
    /**
@@ -20,15 +21,37 @@ namespace allpix {
     * 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.
     */
    template <typename T> T Units::get(T inp, const std::string& str) {
    //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) {
        std::cout << "Input value: " << inp << std::endl;
        UnitType out = static_cast<UnitType>(inp) * get(str);
        std::cout << "Middle value: " << out << std::endl;
        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>) {
            out = std::round(out);
        std::cout << "Output value 1: " << out << std::endl;
        std::cout << "Output value final: " << static_cast<T>(out) << std::endl;
        return static_cast<T>(out);
        //return out;
    }
    
    template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true> T Units::get(T inp, const std::string& str) {
        std::cout << "Input value int: " << inp << std::endl;
        UnitType out = static_cast<UnitType>(inp) * get(str);
        std::cout << "Middle value int: " << out << std::endl;
        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");
        }
        std::cout << "--------------- Output value assert: " << out << ", " << static_cast<T>(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.");
        }
        assert(out == static_cast<T>(out));
        //std::cout << "--------------- Output value assert: " << out << ", " << static_cast<T>(out) << std::endl;
        std::cout << "Output value 1 int: " << out << std::endl;
        std::cout << "Output value final int: " << static_cast<T>(out) << std::endl;
        return static_cast<T>(out);
    }