/* -*- C++ -*- * Copyright ©2004 Hugo Mills * * This software is distributed under the terms of the GNU GPL v3 * For more information on the GPL, see the file COPYING or * visit http://www.gnu.org/ * * This software is distributed without warranty */ #ifndef VALUES_H #define VALUES_H #include #include #include #include #undef max template inline T defvalue() { return T(); } template <> inline double defvalue() { return std::numeric_limits::max(); } template <> inline long defvalue() { return std::numeric_limits::max(); } template <> inline int defvalue() { return std::numeric_limits::max(); } // Value conversion -- could be done with strstreams to avoid the // specialisations template inline void convert(T* var, const std::string& val) { *var = val; } template <> inline void convert(int* var, const std::string& val) { *var = atoi(val.c_str()); } template <> inline void convert(long* var, const std::string& val) { *var = atol(val.c_str()); } template <> inline void convert(double* var, const std::string& val) { *var = atof(val.c_str()); } // Set a value if currently unset template void setdef(T& var, const T& def) { if(var == defvalue()) var = def; } // Error checking functions template void required(const T& var) { if(var == defvalue()) { // Syntax error -- required value not set std::cerr << "Syntax error -- required value not set" << std::endl; exit(1); } } template void invalid(const T& var) { if(var != defvalue()) { // Syntax error -- value set when not valid std::cerr << "Value cannot be specified here" << std::endl; exit(1); } } // Units conversion const double DEG_TO_RAD = M_PI/180.0; const double RAD_TO_DEG = 180.0/M_PI; inline double degrad(double deg) { return deg*DEG_TO_RAD; } inline double raddeg(double rad) { return rad*RAD_TO_DEG; } #endif