netlist: fix some error/warnings. (nw)

This commit is contained in:
couriersud 2019-04-22 14:49:28 +02:00
parent f3e15ce818
commit 7e0524b8a2
4 changed files with 84 additions and 74 deletions

View File

@ -88,6 +88,7 @@ namespace netlist
PERRMSGV(MF_MODEL_PARAMETERS_NOT_UPPERCASE_1_2, 2, "Model parameters should be uppercase:{1} {2}")
PERRMSGV(MF_ENTITY_1_NOT_FOUND_IN_MODEL_2, 2, "Entity {1} not found in model {2}")
PERRMSGV(MF_UNKNOWN_NUMBER_FACTOR_IN_1, 1, "Unknown number factor in: {1}")
PERRMSGV(MF_MODEL_NUMBER_CONVERSION_ERROR, 4, "Can't convert {1}={2} to {3} for model {4}")
PERRMSGV(MF_NOT_FOUND_IN_SOURCE_COLLECTION, 1, "unable to find {1} in sources collection")
PERRMSGV(MW_OVERWRITING_PARAM_1_OLD_2_NEW_3, 3, "Overwriting {1} old <{2}> new <{3}>")

View File

@ -955,7 +955,11 @@ nl_double models_t::value(const pstring &model, const pstring &entity)
tmp = plib::left(tmp, tmp.size() - 1);
// FIXME: check for errors
//printf("%s %s %e %e\n", entity.c_str(), tmp.c_str(), plib::pstonum<nl_double>(tmp), factor);
return plib::pstonum<nl_double>(tmp) * factor;
bool err(false);
nl_double val = plib::pstonum_ne<nl_double>(tmp, err);
if (err)
throw nl_exception(MF_MODEL_NUMBER_CONVERSION_ERROR(entity, tmp, "double", model));
return val * factor;
}
class logic_family_std_proxy_t : public logic_family_desc_t
@ -1118,8 +1122,9 @@ void setup_t::prepare_to_run()
if (p != m_param_values.end())
{
//FIXME: check for errors ...
auto v = plib::pstonum<double>(p->second);
if (std::abs(v - std::floor(v)) > 1e-6 )
bool err(false);
auto v = plib::pstonum_ne<double>(p->second, err);
if (err || std::abs(v - std::floor(v)) > 1e-6 )
log().fatal(MF_HND_VAL_NOT_SUPPORTED(p->second));
d.second->set_hint_deactivate(v == 0.0);
}

View File

@ -510,76 +510,6 @@ namespace plib
return pwstring(std::to_wstring(v));
}
template <typename T, typename E = void>
struct pstonum_helper;
template<typename T>
struct pstonum_helper<T, typename std::enable_if<std::is_integral<T>::value
&& std::is_signed<T>::value>::type>
{
template <typename S>
long long operator()(const S &arg, std::size_t *idx)
{
return std::stoll(arg, idx);
}
};
template<typename T>
struct pstonum_helper<T, typename std::enable_if<std::is_integral<T>::value
&& !std::is_signed<T>::value>::type>
{
template <typename S>
unsigned long long operator()(const S &arg, std::size_t *idx)
{
return std::stoull(arg, idx);
}
};
template<typename T>
struct pstonum_helper<T, typename std::enable_if<std::is_floating_point<T>::value>::type>
{
template <typename S>
long double operator()(const S &arg, std::size_t *idx)
{
return std::stold(arg, idx);
}
};
template<typename T, typename S>
T pstonum(const S &arg)
{
decltype(arg.c_str()) cstr = arg.c_str();
std::size_t idx(0);
auto ret = pstonum_helper<T>()(cstr, &idx);
using ret_type = decltype(ret);
if (ret >= static_cast<ret_type>(std::numeric_limits<T>::lowest())
&& ret <= static_cast<ret_type>(std::numeric_limits<T>::max()))
//&& (ret == T(0) || std::abs(ret) >= std::numeric_limits<T>::min() ))
{
if (cstr[idx] != 0)
throw std::invalid_argument(std::string("Continuation after numeric value ends: ") + cstr);
}
else
{
throw std::out_of_range(std::string("Out of range: ") + cstr);
}
return static_cast<T>(ret);
}
template<typename R, typename T>
R pstonum_ne(const T &str, bool &err) noexcept
{
try
{
err = false;
return pstonum<R>(str);
}
catch (...)
{
err = true;
return R(0);
}
}
template<typename T>
typename T::size_type find_first_not_of(const T &str, const T &no)

View File

@ -9,7 +9,7 @@
#define PUTIL_H_
#include "pstring.h"
#include "pexception.h"
#include <algorithm>
#include <initializer_list>
#include <vector>
@ -124,6 +124,80 @@ namespace plib
const std::string &token,
const std::size_t maxsplit);
// ----------------------------------------------------------------------------------------
// number conversions
// ----------------------------------------------------------------------------------------
template <typename T, typename E = void>
struct pstonum_helper;
template<typename T>
struct pstonum_helper<T, typename std::enable_if<std::is_integral<T>::value
&& std::is_signed<T>::value>::type>
{
template <typename S>
long long operator()(const S &arg, std::size_t *idx)
{
return std::stoll(arg, idx);
}
};
template<typename T>
struct pstonum_helper<T, typename std::enable_if<std::is_integral<T>::value
&& !std::is_signed<T>::value>::type>
{
template <typename S>
unsigned long long operator()(const S &arg, std::size_t *idx)
{
return std::stoull(arg, idx);
}
};
template<typename T>
struct pstonum_helper<T, typename std::enable_if<std::is_floating_point<T>::value>::type>
{
template <typename S>
long double operator()(const S &arg, std::size_t *idx)
{
return std::stold(arg, idx);
}
};
template<typename T, typename S>
T pstonum(const S &arg)
{
decltype(arg.c_str()) cstr = arg.c_str();
std::size_t idx(0);
auto ret = pstonum_helper<T>()(cstr, &idx);
using ret_type = decltype(ret);
if (ret >= static_cast<ret_type>(std::numeric_limits<T>::lowest())
&& ret <= static_cast<ret_type>(std::numeric_limits<T>::max()))
//&& (ret == T(0) || std::abs(ret) >= std::numeric_limits<T>::min() ))
{
if (cstr[idx] != 0)
throw pexception(pstring("Continuation after numeric value ends: ") + cstr);
}
else
{
throw pexception(pstring("Out of range: ") + cstr);
}
return static_cast<T>(ret);
}
template<typename R, typename T>
R pstonum_ne(const T &str, bool &err) noexcept
{
try
{
err = false;
return pstonum<R>(str);
}
catch (...)
{
err = true;
return R(0);
}
}
//============================================================
// penum - strongly typed enumeration