Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <rosmatlab/options.h>
00030 #include <limits>
00031
00032 #include <mex.h>
00033
00034 namespace rosmatlab {
00035
00036 bool Options::isString(const mxArray *value)
00037 {
00038 return mxIsChar(value);
00039 }
00040
00041 std::string Options::getString(const mxArray *value)
00042 {
00043 if (!isString(value)) return std::string();
00044 std::size_t len = mxGetNumberOfElements(value);
00045 char temp[len + 1];
00046 mxGetString(value, temp, sizeof(temp));
00047 return std::string(temp);
00048 }
00049
00050 bool Options::isDoubleScalar(const mxArray *value)
00051 {
00052 return mxIsDouble(value) && (mxGetNumberOfElements(value) == 1);
00053 }
00054
00055 double Options::getDoubleScalar(const mxArray *value)
00056 {
00057 if (!isDoubleScalar(value)) return std::numeric_limits<double>::quiet_NaN();
00058 return *mxGetPr(value);
00059 }
00060
00061 bool Options::isLogicalScalar(const mxArray *value)
00062 {
00063 return mxIsLogicalScalar(value);
00064 }
00065
00066 bool Options::getLogicalScalar(const mxArray *value)
00067 {
00068 if (isLogicalScalar(value)) return mxIsLogicalScalarTrue(value);
00069 if (isDoubleScalar(value)) return getDoubleScalar(value);
00070 return false;
00071 }
00072
00073 Options::Options(int nrhs, const mxArray *prhs[])
00074 {
00075 if (nrhs % 2 != 0) { nrhs--; prhs++; }
00076
00077 for(; nrhs > 0; nrhs -= 2, prhs += 2) {
00078 if (!isString(prhs[0])) continue;
00079 std::string key = getString(prhs[0]);
00080 if (isString(prhs[1])) strings_[key] = getString(prhs[1]);
00081 if (isDoubleScalar(prhs[1])) doubles_[key] = getDoubleScalar(prhs[1]);
00082 if (isLogicalScalar(prhs[1])) logicals_[key] = getLogicalScalar(prhs[1]);
00083 }
00084 }
00085
00086 Options::~Options()
00087 {
00088 }
00089
00090 bool Options::hasKey(const std::string& key) const
00091 {
00092 return strings_.count(key) || doubles_.count(key) || logicals_.count(key);
00093 }
00094
00095 const std::string& Options::getString(const std::string& key, const std::string& default_value)
00096 {
00097 used_[key] = true;
00098 if (strings_.count(key)) return strings_.at(key);
00099 return default_value;
00100 }
00101
00102 double Options::getDouble(const std::string& key, double default_value)
00103 {
00104 used_[key] = true;
00105 if (doubles_.count(key)) return doubles_.at(key);
00106 return default_value;
00107 }
00108
00109 bool Options::getBool(const std::string& key, bool default_value)
00110 {
00111 used_[key] = true;
00112 if (logicals_.count(key)) return logicals_.at(key);
00113 if (doubles_.count(key)) return doubles_.at(key);
00114 return default_value;
00115 }
00116
00117 void Options::warnUnused()
00118 {
00119 for(std::map<std::string,std::string>::iterator it = strings_.begin(); it != strings_.end(); ++it)
00120 if (!used_[it->first]) mexPrintf(std::string("Warning: unused string argument '" + it->first + "'\n").c_str());
00121 for(std::map<std::string,double>::iterator it = doubles_.begin(); it != doubles_.end(); ++it)
00122 if (!used_[it->first]) mexPrintf(std::string("Warning: unused double argument '" + it->first + "'\n").c_str());
00123 for(std::map<std::string,bool>::iterator it = logicals_.begin(); it != logicals_.end(); ++it)
00124 if (!used_[it->first]) mexPrintf(std::string("Warning: unused logical argument '" + it->first + "'\n").c_str());
00125 }
00126
00127 }