options.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 } // namespace rosmatlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


rosmatlab
Author(s): Johannes Meyer
autogenerated on Tue Jan 8 2013 17:31:00