Program Listing for File util.hpp¶
↰ Return to documentation for file (include/nix/util/util.hpp)
// Copyright (c) 2013, German Neuroinformatics Node (G-Node)
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the terms of the BSD License. See
// LICENSE file in the root of the Project.
#ifndef NIX_UTIL_H
#define NIX_UTIL_H
#include <nix/Exception.hpp>
#include <nix/Platform.hpp>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <cmath>
#include <type_traits>
#include <iterator>
#include <boost/optional.hpp>
#include <boost/none_t.hpp>
#include <nix/None.hpp>
namespace nix {
enum class DimensionType : unsigned int;
namespace util {
NIXAPI void checkNameOrId(const std::string &name_or_id);
template <typename T> bool checkEntityInput(const T &entity, bool raise_exception = true) {
if (entity && entity.isValidEntity()) {
return true;
}
if (raise_exception) {
throw UninitializedEntity();
}
return false;
}
NIXAPI void deblankString(std::string &str);
NIXAPI std::string deblankString(const std::string &str);
NIXAPI std::string nameSanitizer(const std::string &name);
NIXAPI bool nameCheck(const std::string &name);
NIXAPI void checkEntityName(const std::string &name);
NIXAPI void checkEntityType(const std::string &str);
NIXAPI void checkEmptyString(const std::string &str, const std::string &field_name = "");
NIXAPI void checkEntityNameAndType(const std::string &name, const std::string &type);
NIXAPI std::string createId();
NIXAPI std::string timeToStr(time_t time);
NIXAPI time_t strToTime(const std::string &time);
NIXAPI time_t getTime();
template<typename T>
std::string toId(const T &entity) {
return entity.id();
}
template<typename T>
std::string toName(const T &entity) {
return entity.name();
}
NIXAPI std::string unitSanitizer(const std::string &unit);
NIXAPI bool isSIUnit(const std::string &unit);
NIXAPI bool isAtomicSIUnit(const std::string &unit);
NIXAPI bool isCompoundSIUnit(const std::string &unit);
NIXAPI bool isScalable(const std::string &unitA, const std::string &unitB);
NIXAPI bool isScalable(const std::vector<std::string> &unitsA, const std::vector<std::string> &unitsB);
NIXAPI bool isSetAtSamePos(const std::vector<std::string> &stringsA, const std::vector<std::string> &stringsB);
NIXAPI double getSIScaling(const std::string &originUnit, const std::string &destinationUnit);
NIXAPI void splitUnit(const std::string &fullUnit, std::string &prefix, std::string &unit, std::string &power);
NIXAPI void splitCompoundUnit(const std::string &compoundUnit, std::vector<std::string> &atomicUnits);
template<typename T>
T convertToSeconds(const std::string &unit, T value) {
T seconds;
if (unit == "min") {
seconds = value * 60;
} else if (unit == "h") {
std::string new_unit = "min";
seconds = convertToSeconds(new_unit, value * 60);
} else if (unit == "s" || unit == "sec") {
seconds = value;
} else if (isScalable(unit, "s")) {
double scaled = value * getSIScaling(unit, "s");
seconds = static_cast<T>(std::is_integral<T>::value ? std::round(scaled) : scaled);
} else {
std::cerr << "[nix::util::convertToSeconds]";
std::cerr << " Warning: given unit [" + unit + "] is not supported!" << std::endl;
seconds = value;
}
return seconds;
}
template<typename T>
T convertToKelvin(const std::string &unit, T value) {
if (unit == "°K" || unit == "K") {
return value;
}
double temperature;
if (unit == "°C" || unit == "C") {
temperature = value + 273.15;
} else if (unit == "°F" || unit == "F") {
temperature = (value - 32) * 5.0/9 + 273.15;
} else if (isScalable(unit, "K")) {
temperature = value * getSIScaling(unit, "K");
} else {
std::cerr << "[nix::util::convertToKelvin]" << std::endl;
std::cerr << " Warning: given unit [" + unit + "] is not supported!" << std::endl;
return value;
}
return static_cast<T>(std::is_integral<T>::value ? std::round(temperature) : temperature);
}
template<typename T>
std::string numToStr(T number) {
std::stringstream s;
s << number;
return s.str();
}
NIXAPI std::string dimTypeToStr(const DimensionType &dtype);
template<typename T>
T strToNum(const std::string &str) {
std::stringstream s(str);
T number;
return s >> number ? number : 0;
}
/*
* Check whether a given type is of type "boost::optional"
*/
template<typename>
struct is_optional : std::false_type {};
template<typename T>
struct is_optional<boost::optional<T>> : std::true_type {};
/*
* Optional de-referencing:
* De-reference boost optional type if such given, returned var
* unchanged otherwise.
*/
template<typename T>
T deRef(T var) {
return var;
}
template<typename R>
R deRef(boost::optional<R> var) {
if (var) return *var;
else return R();
}
NIXAPI void applyPolynomial(const std::vector<double> &coefficients,
double origin,
const double *input,
double *output,
size_t n);
bool looksLikeUUID(const std::string &id);
} // namespace util
} // namespace nix
#endif // NIX_UTIL_H