From cf2495aaac319391dbed234e5328a59b42a0aab5 Mon Sep 17 00:00:00 2001 From: ponchio Date: Wed, 7 May 2014 09:04:34 +0000 Subject: [PATCH] fixed qgetoptz --- wrap/system/multithreading/mt.h | 2 +- wrap/system/multithreading/mutex.h | 51 +++++++++++---------- wrap/system/multithreading/thread.h | 1 + wrap/system/qgetopt.cpp | 70 +++++++++++++---------------- wrap/system/qgetopt.h | 5 ++- 5 files changed, 65 insertions(+), 64 deletions(-) diff --git a/wrap/system/multithreading/mt.h b/wrap/system/multithreading/mt.h index 5971fa89..03fb1943 100755 --- a/wrap/system/multithreading/mt.h +++ b/wrap/system/multithreading/mt.h @@ -8,7 +8,7 @@ #include #include -namespace mt{ +namespace mt { typedef QThread thread; typedef QMutex mutex; typedef QMutexLocker mutexlocker; diff --git a/wrap/system/multithreading/mutex.h b/wrap/system/multithreading/mutex.h index e211bc23..cf811d6e 100755 --- a/wrap/system/multithreading/mutex.h +++ b/wrap/system/multithreading/mutex.h @@ -12,38 +12,43 @@ class condition; class mutex { - MT_PREVENT_COPY(mutex) + MT_PREVENT_COPY(mutex) - public: + public: - typedef mutex this_type; - typedef void base_type; + typedef mutex this_type; + typedef void base_type; - mutex(void) - { - pthread_mutex_init(&(this->m), 0); - } + mutex(void) + { + pthread_mutex_init(&(this->m), 0); + } - ~mutex(void) - { - pthread_mutex_destroy(&(this->m)); - } + ~mutex(void) + { + pthread_mutex_destroy(&(this->m)); + } - void lock(void) - { - pthread_mutex_lock(&(this->m)); - } + void lock(void) + { + pthread_mutex_lock(&(this->m)); + } - void unlock(void) - { - pthread_mutex_unlock(&(this->m)); - } + void unlock(void) + { + pthread_mutex_unlock(&(this->m)); + } + bool tryLock(void) + { + int a = pthread_mutex_trylock(&(this->m)); + return a == 0; + } - private: + private: - friend class condition; + friend class condition; - pthread_mutex_t m; + pthread_mutex_t m; }; } diff --git a/wrap/system/multithreading/thread.h b/wrap/system/multithreading/thread.h index 11edd54b..c459551d 100755 --- a/wrap/system/multithreading/thread.h +++ b/wrap/system/multithreading/thread.h @@ -6,6 +6,7 @@ #include "base.h" #include +#include namespace mt { diff --git a/wrap/system/qgetopt.cpp b/wrap/system/qgetopt.cpp index 5bbb5523..eb4ca497 100644 --- a/wrap/system/qgetopt.cpp +++ b/wrap/system/qgetopt.cpp @@ -213,6 +213,28 @@ void GetOpt::parse() { } } +bool GetOpt::assignOption(Option &o, QString arg, QString &error) { + QVariant::Type type; + if(o.value) type = o.value->type(); + if(o.string_value) type = QVariant::String; + if(o.double_value) type = QVariant::Double; + if(o.int_value) type = QVariant::Int; + if(o.boolean_value) type = QVariant::Bool; + QVariant v(arg); + + if(!v.canConvert(type) || !v.convert(type)) { + error = "Error while parsing option " + o.name + ": cannot convert " + + arg + " to: " + v.typeName(); + return false; + } + if(o.value) *(o.value) = v; + if(o.string_value) *(o.string_value) = v.toString(); + if(o.double_value) *(o.double_value) = v.toDouble(); + if(o.int_value) *(o.int_value) = v.toInt(); + if(o.boolean_value) *(o.boolean_value) = v.toBool(); + return true; +} + bool GetOpt::parse(QString &error) { for(int i = 0; i < args.size(); i++) { QString arg = args[i]; @@ -245,7 +267,7 @@ bool GetOpt::parse(QString &error) { error = "Missing argument after option '" + arg + "'"; return false; } - if(!parseOption(o, arg)) + if(!assignOption(o, arg, error)) return false; } @@ -273,20 +295,24 @@ bool GetOpt::parse(QString &error) { error = "Missing argument after option '" + arg + "'"; return false; } + if(!assignOption(o, arg, error)) + return false; +/* QVariant v(arg); if(!v.canConvert(o.value->type()) || !v.convert(o.value->type())) { error = "Error while parsing option " + o.name + ": cannot convert " + arg + " to: " + o.value->typeName(); return false; } - *(o.value) = v; + *(o.value) = v; */ } //argument } else { arguments.push_back(arg); } } - //test arguments + + //regular arguments for(int i = 0; i < options.size(); i++) { Option &o = options[i]; if(o.type != Option::ARGUMENT) continue; @@ -294,25 +320,16 @@ bool GetOpt::parse(QString &error) { error = "Too few arguments, could not parse argument '" + o.name + "'"; return false; } - if(!parseOption(o, arguments.front())) + if(!assignOption(o, arguments.front(), error)) return false; arguments.pop_front(); } - //test arguments + //optional arguments for(int i = 0; i < options.size(); i++) { Option &o = options[i]; if(o.type != Option::OPTIONAL) continue; if(arguments.isEmpty()) break; - if(!parseOption(o, arguments.front())) - return false; - arguments.pop_front(); - } - //test arguments - for(int i = 0; i < options.size(); i++) { - Option &o = options[i]; - if(o.type != Option::ARGUMENT) continue; - if(arguments.isEmpty()) break; - if(!parseOption(o, arguments.front())) + if(!assignOption(o, arguments.front(), error)) return false; arguments.pop_front(); } @@ -364,26 +381,3 @@ QString GetOpt::formatDesc(QString desc, int len) { } return output; } - -bool GetOpt::parseOption(GetOpt::Option &o, const QString &arg) { - QVariant::Type type; - if(o.value) type = o.value->type(); - if(o.string_value) type = QVariant::String; - if(o.double_value) type = QVariant::Double; - if(o.int_value) type = QVariant::Int; - if(o.boolean_value) type = QVariant::Bool; - QVariant v(arg); - - if(!v.canConvert(type) || !v.convert(type)) { - cerr << "Error while parsing option " << qPrintable(o.name) << ": cannot convert " << - qPrintable(arg) << " to: " << qPrintable(v.typeName()) << endl; - return false; - } - if(o.value) - *(o.value) = v; - if(o.string_value) *(o.string_value) = v.toString(); - if(o.double_value) *(o.double_value) = v.toDouble(); - if(o.int_value) *(o.int_value) = v.toInt(); - if(o.boolean_value) *(o.boolean_value) = v.toBool(); - return true; -} diff --git a/wrap/system/qgetopt.h b/wrap/system/qgetopt.h index e3fefa9d..f9178587 100644 --- a/wrap/system/qgetopt.h +++ b/wrap/system/qgetopt.h @@ -113,7 +113,7 @@ class GetOpt { //return argv[0] QString &applicationName(); - protected: +protected: //parses and return true on success bool parse(QString &error); //return options or switch @@ -122,8 +122,9 @@ class GetOpt { bool findArg(const QString &name, Option &option); //split desc into n pieces of the right length TODO: check for newlines also QString formatDesc(QString desc, int len); + //manage conversion from string to option value + bool assignOption(Option &option, QString arg, QString &error); - bool parseOption(Option &option, const QString &arg); }; #endif