nopt.hpp

00001 // LICENSE: (Please see the file COPYING for details)
00002 //
00003 // NUS - Nemesis Utilities System: A C++ application development framework 
00004 // Copyright (C) 2006, 2008 Otavio Rodolfo Piske
00005 //
00006 //  This file is part of NUS
00007 //
00008 //  This library is free software; you can redistribute it and/or
00009 //  modify it under the terms of the GNU Lesser General Public
00010 //  License as published by the Free Software Foundation version 2.1
00011 //  of the License.
00012 //
00013 //  This library is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //  Lesser General Public License for more details.
00017 //
00018 //  You should have received a copy of the GNU Lesser General Public
00019 //  License along with this library; if not, write to the Free Software
00020 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021 //
00022 #ifndef NOPT_HPP
00023 #define NOPT_HPP
00024 
00025 #include "noutput.h"
00026 
00036 template <typename HandlerType>
00037 class NOpt: public NObject {
00038       public:
00042             typedef bool (HandlerType::*Handler)(const NOpt &);
00043       
00047             NOpt(void);
00048             
00049             
00059             NOpt(const NString &opt_short, const NString &opt_long, bool opt_opt, 
00060                   const NString &help, Handler handler);
00061             
00065             ~NOpt();
00066             
00071             void setShort(const NString &opt_short);
00072             
00077             NString getShort(void) const;
00078             
00083             void setLong(const NString &opt_long);
00084             
00089             NString getLong(void) const;
00090             
00091             
00097             void setOpt(bool opt_opt);
00098             
00103             bool hasOpt(void) const;
00104             
00105             
00110             void setHelp(const NString &help);
00111             
00116             NString getHelp(void) const;
00117             
00122             void addValue(const NString &value);
00123             
00124             
00129             void addValues(const NList<NString> &values);
00130             
00131             
00136             NList<NString> getValues(void) const;
00137             
00138             // uses NOpt<HandlerType>::Handler instead of Handler to avoid 
00139             // warnings when generating the documentation
00145             void setHandler(NOpt<HandlerType>::Handler handle);
00146             
00151             Handler getHandler(void) const;
00152             
00157             bool isNull(void) const;
00158             
00159             
00164             void setRequired(bool required);
00165             
00166             
00171             bool getRequired(void) const;
00172       
00178             bool operator==(const NOpt &rhs) const;
00179             
00180             
00186             bool operator!=(const NOpt &rhs) const;
00187       private:
00188             NString m_short;
00189             NString m_long;
00190             bool m_opt;
00191             NString m_help;
00192             NList<NString> m_values;
00193             Handler m_handler;
00194 
00195             NException maxCountExceeded(void) const;
00196 };
00197 
00198 
00199 template <typename HandlerType>
00200 NOpt<HandlerType>::NOpt(void)
00201             : NObject(),
00202             m_short(),
00203             m_long(),
00204             m_opt(),
00205             m_help(),
00206             m_values(),
00207             m_handler(NULL)
00208 {
00209 
00210 }
00211 
00212 
00213 template <typename HandlerType>
00214 NOpt<HandlerType>::NOpt(const NString &opt_short, const NString &opt_long, 
00215                         bool opt_opt, const NString &help, Handler handler)
00216                   : NObject(), 
00217                   m_short(opt_short), 
00218                   m_long(opt_long),
00219                   m_opt(opt_opt),
00220                   m_help(help),
00221                   m_values(),
00222                   m_handler(handler)
00223 {
00224 
00225 }
00226 
00227 
00228 template <typename HandlerType>
00229 NOpt<HandlerType>::~NOpt() {
00230 
00231 }
00232 
00233 
00234 template <typename HandlerType>
00235 void NOpt<HandlerType>::setShort(const NString &opt_short) {
00236       m_short = opt_short;
00237 }
00238 
00239 
00240 template <typename HandlerType>
00241 NString NOpt<HandlerType>::getShort(void) const {
00242       return m_short;
00243 }
00244 
00245 
00246 template <typename HandlerType>
00247 void NOpt<HandlerType>::setLong(const NString &opt_long) {
00248       m_long = opt_long;
00249 }
00250 
00251 
00252 template <typename HandlerType>
00253 NString NOpt<HandlerType>::getLong(void) const {
00254       return m_long;
00255 }
00256 
00257 
00258 template <typename HandlerType>
00259 void NOpt<HandlerType>::setOpt(bool opt_opt) {
00260       m_opt = opt_opt;
00261 }
00262 
00263 
00264 template <typename HandlerType>
00265 bool NOpt<HandlerType>::hasOpt(void) const {
00266       return m_opt;
00267 }
00268 
00269 
00270 template <typename HandlerType>
00271 void NOpt<HandlerType>::setHelp(const NString &help) {
00272       m_help = help;
00273 }
00274 
00275 
00276 template <typename HandlerType>
00277 NString NOpt<HandlerType>::getHelp(void) const {
00278       return m_help;
00279 }
00280 
00281 
00282 
00283 template <typename HandlerType>
00284 void NOpt<HandlerType>::addValue(const NString &value) {
00285       m_values.append(value); 
00286 }
00287 
00288 
00289 template <typename HandlerType>
00290 void NOpt<HandlerType>::addValues(const NList<NString> &values) {
00291       m_values.copy(values);
00292 }
00293 
00294 
00295 template <typename HandlerType>
00296 NList<NString> NOpt<HandlerType>::getValues(void) const {
00297       return m_values;
00298 }
00299 
00300 
00301 template <typename HandlerType>
00302 void NOpt<HandlerType>::setHandler(typename NOpt<HandlerType>::Handler handler) {
00303       m_handler = handler;
00304 }
00305 
00306 
00307 template <typename HandlerType>
00308 typename NOpt<HandlerType>::Handler NOpt<HandlerType>::getHandler(void) const {
00309       return m_handler;
00310 }
00311 
00312 
00313 template <typename HandlerType>
00314 bool NOpt<HandlerType>::isNull(void) const {
00315       if ((m_short == 0) && (m_long == "")) {
00316             return true;
00317       }
00318       
00319       return false;
00320 }
00321 
00322 
00323 template <typename HandlerType>
00324 bool NOpt<HandlerType>::operator==(const NOpt &rhs) const {
00325       if (m_short == "") { 
00326             if (m_long == rhs.getLong()) {
00327                   return true;
00328             }
00329             
00330             return false;
00331       }
00332 
00333       if (m_short == rhs.getShort()) {
00334             return true;
00335       }
00336       
00337       if (m_long == rhs.getLong()) {
00338             return true;
00339       }
00340       
00341 
00342       return false; 
00343 }
00344 
00345 
00346 template <typename HandlerType>
00347 bool NOpt<HandlerType>::operator!=(const NOpt &rhs) const {
00348       if (m_short == "") { 
00349             if (m_long != rhs.getLong()) {
00350                   return true;
00351             }
00352 
00353             return false;
00354       }
00355 
00356 
00357       if (m_short != rhs.getShort()) {
00358             if (m_long != rhs.getLong()) {
00359                   return true;
00360             }
00361       }
00362 
00363       return false; 
00364 }
00365 
00366 #endif // NOPT_HPP

Generated on Wed Mar 5 23:10:35 2008 for NemesisUtilitiesSystem by  doxygen 1.5.4