/** * @file helper.hpp * @author Julian Iseringhausen * @date Tue Apr 5 21:03:47 2011 * * @brief Provides various helper functions. * */ #ifndef HELPER_HPP_ #define HELPER_HPP_ #include #include #include #include #include namespace VMG { class Grid; class Vector; namespace Helper { char* GetCharArray(const std::string& str); std::string ReplaceWhitespaces(const char* buffer, const char* replace); template std::string ToString(const T& val) { std::stringstream str; str << std::scientific << val; return str.str(); } template T ToVal(const char* val_str) { T val; std::stringstream str; str << val_str; str >> val; return val; } template T ToValWithDefault(const char* val_str, const T& def) { T val; std::stringstream str(val_str); str >> val; if (str.fail() || str.bad() || !str.eof()) { #ifdef DEBUG_VERBOSE std::printf("VMG::Helper::ToValWithDefault: Using default value.\n"); #endif val = def; } return val; } /** * Checks a number for validity, i.e. it is neither nan nor inf. */ inline bool CheckNumber(const vmg_float& number) { bool valid = true; if (std::numeric_limits::has_quiet_NaN) { valid &= number != std::numeric_limits::quiet_NaN(); assert(number != std::numeric_limits::quiet_NaN()); } if (std::numeric_limits::has_signaling_NaN) { valid &= number != std::numeric_limits::signaling_NaN(); assert(number != std::numeric_limits::signaling_NaN()); } if (std::numeric_limits::has_infinity) { valid &= number != std::numeric_limits::infinity(); valid &= number != -1 * std::numeric_limits::infinity(); assert(number != std::numeric_limits::infinity()); assert(number != -1 * std::numeric_limits::infinity()); } return valid; } inline int intpow(int base, unsigned int power) { int result = 1; while (power != 0) { if (power & 1) result *= base; base *= base; power >>= 1; } return result; } inline vmg_float pow(vmg_float base, unsigned int power) { vmg_float result = 1.0; while (power != 0) { if (power & 1) result *= base; base *= base; power >>= 1; } return result; } inline unsigned int fact(unsigned int number) { unsigned int result = 1; for (unsigned int i=2; i<=number; ++i) result *= i; return result; } inline vmg_float pow_3(vmg_float val) { return val*val*val; } /** * Tests two arbitrary objects for equality and prints * a warning if they differ. */ template bool IsEq(const T& val1, const T& val2, const char name[]) { bool rval = (val1 == val2); #ifdef DEBUG_OUTPUT if (!rval) printf("WARNING: Values are not equal (%s)\n", name); #endif /* DEBUG_OUTPUT */ assert(rval); return rval; } vmg_float InterpolateTrilinear(const Vector& point, const Grid& grid); } } #endif /* HELPER_HPP_ */