From: Enar Väikene Date: Wed, 18 May 2011 10:35:52 +0000 (+0300) Subject: Added global utility functions. X-Git-Url: https://vaikene.ee/gitweb/overview.html?a=commitdiff_plain;h=814d12e0a340ae11fa4a22077b37316aa41716d7;p=evaf Added global utility functions. --- diff --git a/src/libs/Common/CMakeLists.txt b/src/libs/Common/CMakeLists.txt index 20eca77..085749e 100644 --- a/src/libs/Common/CMakeLists.txt +++ b/src/libs/Common/CMakeLists.txt @@ -22,6 +22,7 @@ set(SRCS globals.cpp logger.cpp registry.cpp + util.cpp ) # Header files for the meta-object compiler diff --git a/src/libs/Common/Util b/src/libs/Common/Util new file mode 100644 index 0000000..408a76a --- /dev/null +++ b/src/libs/Common/Util @@ -0,0 +1 @@ +#include "util.h" diff --git a/src/libs/Common/globals.cpp b/src/libs/Common/globals.cpp index dfb1595..e494908 100644 --- a/src/libs/Common/globals.cpp +++ b/src/libs/Common/globals.cpp @@ -36,7 +36,7 @@ bool eVaf::Common::init() return false; } - EVAF_INFO("Initializing %s.Globals", VER_MODULE_NAME_STR); + EVAF_INFO("Initializing %s-Globals", VER_MODULE_NAME_STR); // Initialize all the common interface implementations in the proper sequence @@ -59,7 +59,7 @@ bool eVaf::Common::init() return false; } - EVAF_INFO("%s.Globals initialized", VER_MODULE_NAME_STR); + EVAF_INFO("%s-Globals initialized", VER_MODULE_NAME_STR); return true; } diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp new file mode 100644 index 0000000..ccabdab --- /dev/null +++ b/src/libs/Common/util.cpp @@ -0,0 +1,81 @@ +/** + * @file Common/util.cpp + * @brief Global utility functions for eVaf + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#include "util.h" + + +//------------------------------------------------------------------- + +QVariant eVaf::Common::toVariant(QString const & value, QVariant const & defaultValue) +{ + // If the default value is not valid, return the string value without conversions + if (!defaultValue.isValid()) + return QVariant(value); + + // Convert to the expected type + switch (defaultValue.type()) { + case QVariant::UInt: { + bool ok; + uint v = value.toUInt(&ok, 0); + if (ok) + return QVariant(v); + else + return defaultValue; + break; + } + case QVariant::Int: { + bool ok; + int v = value.toInt(&ok, 0); + if (ok) + return QVariant(v); + else + return defaultValue; + break; + } + case QVariant::Double: { + bool ok; + double v = value.toDouble(&ok); + if (ok) + return QVariant(v); + else + return defaultValue; + break; + } + case QVariant::Bool: { + bool ok; + uint v = value.toUInt(&ok, 0); + if (eVaf::Common::isTrue(value)) + return QVariant(true); + else if (eVaf::Common::isFalse(value)) + return QVariant(false); + else + return QVariant(v); + break; + } + case QVariant::Char: { + if (value.size() > 0) + return QVariant(value.at(0)); + else + return defaultValue; + break; + } + default: + return QVariant(value); + } +} diff --git a/src/libs/Common/util.h b/src/libs/Common/util.h new file mode 100644 index 0000000..799f8aa --- /dev/null +++ b/src/libs/Common/util.h @@ -0,0 +1,74 @@ +/** + * @file Common/util.h + * @brief Global utility functions for eVaf + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#ifndef __COMMON_UTIL_H +# define __COMMON_UTIL_H + +#include "libcommon.h" + +#include +#include + +namespace eVaf { +namespace Common { + +/** + * Tests if the string means 'true' + * @param str The string + * @return True if the string is 'yes', 'true', 'on' or '1'. + * + * This function tests if the string means 'true'. Use this function when the default value + * should be 'false'. + */ +inline bool isTrue(QString const & str) +{ + return str == "1" || str.toLower() == "yes" || str.toLower() == "true" || str.toLower() == "on"; +} + +/** + * Tests if the string means 'false' + * @param str The string + * @return True if the string is 'no', 'false', 'off' or '0'. + * + * This function tests if the string means 'false'. Use this function when the default value + * should be 'true'. + */ +inline bool isFalse(QString const & str) +{ + return str == "0" || str.toLower() == "no" || str.toLower() == "false" || str.toLower() == "off"; +} + +/** + * Converts strings to variant values + * @param value The string + * @param defaultValue The default value + * @param Variant value converted from the string or the default if failed + * + * This function converts strings to variant values. The type of the expected value is determined + * from the default value. If the string fails to convert to the expected type, returns the + * default value. + */ + +COMMON_EXPORT QVariant toVariant(QString const & value, QVariant const & defaultValue); + + +} // namespace eVaf::Common +} // namespace eVaf + +#endif // util.h