/* (C) 2011 Viktor Lofgren * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifdef GETOPTPP_H /* This file contains template voodoo, and due to the unique way GCC handles * templates, it needs to be included as a header (and it -is-). Do not attempt to * compile this file directly! */ template T &ParameterSet::add(char shortName, const char* longName, const char* description) { T* p = new T(shortName, longName, description); parameters.push_back(p); return *p; } /* * * Class CommonParameter implementation * * */ template CommonParameter::~CommonParameter() {} template CommonParameter::CommonParameter(char shortOption, const char *longOption, const char* description) : Parameter(shortOption, longOption, description) {} template bool CommonParameter::isSet() const{ return SwitchingBehavior::isSet(); } template std::string CommonParameter::usageLine() const { std::stringstream strstr; if (hasShortOption()) { strstr << "-" << shortOption() << ", "; } else { strstr << " "; } strstr.width(20); strstr << std::left << "--" + longOption(); return strstr.str(); } /* * * Class SwitchParameter * * */ template SwitchParameter::SwitchParameter(char shortOption, const char *longOption, const char* description) : CommonParameter(shortOption, longOption, description) {} template SwitchParameter::~SwitchParameter() {} template int SwitchParameter::receive(ParserState& state) throw(Parameter::ParameterRejected) { const std::string arg = state.get(); if(arg.at(0) != '-') return false; if ((arg.at(1) == '-' && arg.substr(2) == this->longOption()) || (this->hasShortOption() && arg.at(1) == this->shortOption() && arg.length() == 2)) { this->set(); return 1; } return 0; } /* * PODParameter stuff * */ template PODParameter::PODParameter(char shortOption, const char *longOption, const char* description) : CommonParameter(shortOption, longOption, description) {} template PODParameter::~PODParameter() {} template PODParameter::operator T() const { return getValue(); } template void PODParameter::setDefault(T value) { PresettableUniquelySwitchable::preset(); this->value = value; } template T PODParameter::getValue() const { if(!this->isSet()) { throw std::runtime_error( std::string("Attempting to retreive the argument of parameter") + this->longOption() + " but it hasn't been set!"); } return value; } template std::string PODParameter::usageLine() const { std::stringstream strstr; if (this->hasShortOption()) { strstr << "-" << this->shortOption() << ", "; } else { strstr << " "; } strstr.width(20); strstr << std::left << "--" + this->longOption() + " "; return strstr.str(); } template int PODParameter::receive(ParserState& state) throw(Parameter::ParameterRejected) { const std::string arg = state.get(); if(arg.at(0) != '-') return false; if((arg.at(1) == '-' && arg.substr(2) == this->longOption()) || (this->hasShortOption() && arg.at(1) == this->shortOption() && arg.length() == 2)) { // retrieve the argument std::string arg1 = state.peek(); if (arg1.length() == 0) { throw Parameter::ExpectedArgument(arg + ": expected an argument"); return 1; } this->set(); value = this->validate(arg1); return 2; } return 0; } #endif