diff options
author | Ben Boeckel <MathStuf@gmail.com> | 2009-03-17 19:36:35 -0400 |
---|---|---|
committer | Ben Boeckel <MathStuf@gmail.com> | 2009-03-17 19:36:35 -0400 |
commit | 99c87ee39e32117ce5a9652ef5b50a1d5760bdf7 (patch) | |
tree | 203e64058ccd55a8a8b0d1211855cf0197ddf23a /sigscript | |
parent | d40b7586c3d1f1020a9fefd52bec75161edc63e8 (diff) | |
download | sigen-99c87ee39e32117ce5a9652ef5b50a1d5760bdf7.tar.gz sigen-99c87ee39e32117ce5a9652ef5b50a1d5760bdf7.tar.xz sigen-99c87ee39e32117ce5a9652ef5b50a1d5760bdf7.zip |
Move the map for the values into its own thread-safe class
Diffstat (limited to 'sigscript')
-rw-r--r-- | sigscript/CMakeLists.txt | 2 | ||||
-rw-r--r-- | sigscript/Config.cpp | 55 | ||||
-rw-r--r-- | sigscript/Config.h | 39 | ||||
-rw-r--r-- | sigscript/ConfigOptions.h | 40 | ||||
-rw-r--r-- | sigscript/ValueMap.cpp | 77 | ||||
-rw-r--r-- | sigscript/ValueMap.h | 56 |
6 files changed, 206 insertions, 63 deletions
diff --git a/sigscript/CMakeLists.txt b/sigscript/CMakeLists.txt index 0a95b0eb..c297588f 100644 --- a/sigscript/CMakeLists.txt +++ b/sigscript/CMakeLists.txt @@ -2,6 +2,7 @@ project(sigscript) set(sigscript_HEADERS Config.h + ConfigOptions.h AbilityWrapper.h AuthorWrapper.h BadgeWrapper.h @@ -39,6 +40,7 @@ set(sigscript_HEADERS ) set(sigscript_SRCS Config.cpp + ValueMap.cpp AbilityWrapper.cpp AuthorWrapper.cpp BadgeWrapper.cpp diff --git a/sigscript/Config.cpp b/sigscript/Config.cpp index 370f1b4a..f5924056 100644 --- a/sigscript/Config.cpp +++ b/sigscript/Config.cpp @@ -25,28 +25,26 @@ using namespace Sigscript; Config::Config(Config* parent) : QObject(parent), - m_lock(QReadWriteLock::Recursive), m_parent(parent) { } bool Config::addValue(const QString& name, const QVariant& value) { - if (!m_values.contains(name) || (m_values[name].second & Deleted)) + if (!m_values.contains(name) || (m_values.options(name) & Deleted)) return setValue(name, value); return false; } bool Config::setValue(const QString& name, const QVariant& value) { - if (m_values.contains(name) && (m_values[name].second & ReadOnly)) + if (m_values.contains(name) && (m_values.options(name) & ReadOnly)) return false; if (!m_values.contains(name)) emit(valueAdded(name, value)); - if (m_values[name].second & Deleted) + if (m_values.options(name) & Deleted) unsetOptions(name, Deleted); - QWriteLocker locker(&m_lock); - m_values[name] = Value(value, 0); + m_values.setValue(name, value); emit(valueChanged(name, value)); return true; } @@ -56,10 +54,7 @@ bool Config::removeValue(const QString& name, const bool shadow) if (shadow) setOptions(name, Deleted); else if (m_values.contains(name)) - { - QWriteLocker locker(&m_lock); - m_values.remove(name); - } + m_values.unsetValue(name); else return false; emit(valueRemoved(name)); @@ -68,12 +63,11 @@ bool Config::removeValue(const QString& name, const bool shadow) QVariant Config::value(const QString& name, const bool recursive) const { - QReadLocker locker(&m_lock); if (m_values.contains(name)) { - if (m_values[name].second & (Deleted | Hidden)) + if (m_values.options(name) & (Deleted | Hidden)) return QVariant(); - return m_values[name].first; + return m_values.value(name); } if (recursive && m_parent) return m_parent->value(name, true); @@ -82,9 +76,8 @@ QVariant Config::value(const QString& name, const bool recursive) const bool Config::hasValue(const QString& name, const bool recursive) const { - QReadLocker locker(&m_lock); if (m_values.contains(name)) - return !(m_values[name].second & (Deleted | Hidden)); + return !(m_values.options(name) & (Deleted | Hidden)); if (recursive && m_parent) return m_parent->hasValue(name, true); return false; @@ -92,48 +85,44 @@ bool Config::hasValue(const QString& name, const bool recursive) const QStringList Config::values(const bool recursive) const { - QReadLocker locker(&m_lock); QStringList values = m_values.keys(); if (recursive && m_parent) values += m_parent->values(true); const QStringList keys = m_values.keys(); foreach (const QString& key, keys) { - if (m_values[key].second & (Deleted | Hidden)) + if (m_values.options(key) & (Deleted | Hidden)) values.removeAll(key); } return values.toSet().toList(); } -bool Config::setOptions(const QString& name, const Options options) +bool Config::setOptions(const QString& name, const ConfigOptions options) { - if ((m_values.contains(name) && (~m_values[name].second & options)) || addValue(name, QVariant())) + if ((m_values.contains(name) && (~m_values.options(name) & options)) || addValue(name, QVariant())) { - QWriteLocker locker(&m_lock); - m_values[name].second |= options; + m_values.setOptions(name, options); emit(optionsChanged(name, options)); return true; } return false; } -bool Config::unsetOptions(const QString& name, const Options options) +bool Config::unsetOptions(const QString& name, const ConfigOptions options) { - if (m_values.contains(name) && (m_values[name].second & options)) + if (m_values.contains(name) && (m_values.options(name) & options)) { - QWriteLocker locker(&m_lock); - m_values[name].second &= ~options; + m_values.unsetOptions(name, options); emit(optionsChanged(name, options)); return true; } return false; } -Config::Options Config::options(const QString& name, const bool recursive) const +ConfigOptions Config::options(const QString& name, const bool recursive) const { - QReadLocker locker(&m_lock); if (m_values.contains(name)) - return m_values[name].second; + return m_values.options(name); if (recursive && m_parent) return m_parent->options(name, recursive); return 0; @@ -141,15 +130,15 @@ Config::Options Config::options(const QString& name, const bool recursive) const void Config::clean() { - for (QMutableMapIterator<QString, Value> i(m_values); i.hasNext(); i.next()) + QStringList values = m_values.keys(); + foreach (const QString& value, values) { - unsetOptions(i.key(), Hidden); - if (i.value().second & Temporary) - removeValue(i.key(), false); + unsetOptions(value, Hidden); + if (m_values.options(value) & Temporary) + removeValue(value, false); } } void Config::writeBack() { - QWriteLocker locker(&m_lock); } diff --git a/sigscript/Config.h b/sigscript/Config.h index af2aad09..812c3652 100644 --- a/sigscript/Config.h +++ b/sigscript/Config.h @@ -19,36 +19,20 @@ #define SIGSCRIPT_CONFIG // Sigscript includes +#include "ConfigOptions.h" #include "Global.h" +#include "ValueMap.h" // Qt includes -#include <QtCore/QMap> #include <QtCore/QObject> -#include <QtCore/QPair> -#include <QtCore/QReadWriteLock> -#include <QtCore/QString> -#include <QtCore/QStringList> -#include <QtCore/QVariant> namespace Sigscript { class SIGSCRIPT_EXPORT Config : public QObject { Q_OBJECT - Q_ENUMS(Option) public: - enum Option - { - Temporary = 1, - Deleted = 2, - ReadOnly = 4, - Hidden = 8 - }; - Q_DECLARE_FLAGS(Options, Option) - - typedef QPair<QVariant, Options> Value; - Config(Config* parent); Q_SCRIPTABLE QVariant value(const QString& name, const bool recursive = true) const; @@ -57,36 +41,34 @@ class SIGSCRIPT_EXPORT Config : public QObject template<typename T> bool hasValueOfType(const QString& name, const bool recursive = true) const; Q_SCRIPTABLE QStringList values(const bool recursive = false) const; - Q_SCRIPTABLE Options options(const QString& name, const bool recursive = true) const; + Q_SCRIPTABLE ConfigOptions options(const QString& name, const bool recursive = true) const; signals: void valueAdded(const QString& name, const QVariant& value); void valueChanged(const QString& name, const QVariant& newValue); void valueRemoved(const QString& name); - void optionsChanged(const QString& name, const Sigscript::Config::Options newOptions); + void optionsChanged(const QString& name, const Sigscript::ConfigOptions newOptions); public slots: bool addValue(const QString& name, const QVariant& value); bool setValue(const QString& name, const QVariant& value); bool removeValue(const QString& name, const bool shadow = false); - bool setOptions(const QString& name, const Sigscript::Config::Options options); - bool unsetOptions(const QString& name, const Sigscript::Config::Options options); + bool setOptions(const QString& name, const Sigscript::ConfigOptions options); + bool unsetOptions(const QString& name, const Sigscript::ConfigOptions options); void clean(); virtual void writeBack(); private: - mutable QReadWriteLock m_lock; Config* m_parent; - QMap<QString, Value> m_values; + ValueMap m_values; }; template<typename T> bool Config::valueOfType(const QString& name, T* value, const bool recursive) const { - QReadLocker locker(&m_lock); if (hasValueOfType<T>(name)) { - *value = m_values[name].first.value<T>(); + *value = m_values.value(name).value<T>(); return true; } if (recursive) @@ -104,8 +86,7 @@ template<typename T> bool Config::valueOfType(const QString& name, T* value, con template<typename T> bool Config::hasValueOfType(const QString& name, const bool recursive) const { - QReadLocker locker(&m_lock); - if (hasValue(name) && m_values[name].first.canConvert<T>()) + if (hasValue(name) && m_values.value(name).canConvert<T>()) return true; if (recursive) return m_parent->hasValueOfType<T>(name, true); @@ -114,7 +95,5 @@ template<typename T> bool Config::hasValueOfType(const QString& name, const bool } Q_DECLARE_METATYPE(Sigscript::Config*) -Q_DECLARE_METATYPE(Sigscript::Config::Options) -Q_DECLARE_OPERATORS_FOR_FLAGS(Sigscript::Config::Options) #endif diff --git a/sigscript/ConfigOptions.h b/sigscript/ConfigOptions.h new file mode 100644 index 00000000..ac011b48 --- /dev/null +++ b/sigscript/ConfigOptions.h @@ -0,0 +1,40 @@ +/* + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> + * + * 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 + * (at your option) 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef SIGSCRIPT_CONFIGOPTIONS +#define SIGSCRIPT_CONFIGOPTIONS + +// Qt includes +#include <QtCore/QMetaType> + +namespace Sigscript +{ +enum ConfigOption +{ + Temporary = 1, + Deleted = 2, + ReadOnly = 4, + Hidden = 8 +}; +Q_DECLARE_FLAGS(ConfigOptions, ConfigOption) + +} + +Q_DECLARE_METATYPE(Sigscript::ConfigOptions) +Q_DECLARE_OPERATORS_FOR_FLAGS(Sigscript::ConfigOptions) + +#endif diff --git a/sigscript/ValueMap.cpp b/sigscript/ValueMap.cpp new file mode 100644 index 00000000..1b10462d --- /dev/null +++ b/sigscript/ValueMap.cpp @@ -0,0 +1,77 @@ +/* +* Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> +* +* 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 +* (at your option) 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 <http://www.gnu.org/licenses/>. +*/ + +// Header include +#include "ValueMap.h" + +// Qt includes +#include <QtCore/QReadLocker> +#include <QtCore/QWriteLocker> + +using namespace Sigscript; + +bool ValueMap::contains(const QString& name) const +{ + QReadLocker locker(&m_mutex); + return m_map.contains(name); +} + +QStringList ValueMap::keys() const +{ + QReadLocker locker(&m_mutex); + return m_map.keys(); +} + +QVariant ValueMap::value(const QString& name) const +{ + QReadLocker locker(&m_mutex); + if (m_map.contains(name)) + return m_map[name].first; + return QVariant(); +} + +void ValueMap::setValue(const QString& name, const QVariant& value) +{ + QWriteLocker locker(&m_mutex); + m_map[name].first = value; +} + +void ValueMap::unsetValue(const QString& name) +{ + QWriteLocker locker(&m_mutex); + m_map.remove(name); +} + +ConfigOptions ValueMap::options(const QString& name) const +{ + QReadLocker locker(&m_mutex); + if (m_map.contains(name)) + return m_map[name].second; + return 0; +} + +void ValueMap::setOptions(const QString& name, const ConfigOptions options) +{ + QWriteLocker locker(&m_mutex); + m_map[name].second |= options; +} + +void ValueMap::unsetOptions(const QString& name, const ConfigOptions options) +{ + QWriteLocker locker(&m_mutex); + m_map[name].second &= ~options; +} diff --git a/sigscript/ValueMap.h b/sigscript/ValueMap.h new file mode 100644 index 00000000..9f05ca33 --- /dev/null +++ b/sigscript/ValueMap.h @@ -0,0 +1,56 @@ +/* + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> + * + * 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 + * (at your option) 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef SIGSCRIPT_ACTIONMAP +#define SIGSCRIPT_ACTIONMAP + +// Sigscript includes +#include "ConfigOptions.h" +#include "Global.h" + +// Qt includes +#include <QtCore/QMap> +#include <QtCore/QPair> +#include <QtCore/QReadWriteLock> +#include <QtCore/QString> +#include <QtCore/QStringList> +#include <QtCore/QVariant> + +namespace Sigscript +{ +class SIGSCRIPT_NO_EXPORT ValueMap +{ + public: + bool contains(const QString& name) const; + + QStringList keys() const; + + QVariant value(const QString& name) const; + void setValue(const QString& name, const QVariant& value); + void unsetValue(const QString& name); + + ConfigOptions options(const QString& name) const; + void setOptions(const QString& name, const ConfigOptions options); + void unsetOptions(const QString& name, const ConfigOptions options); + private: + typedef QPair<QVariant, ConfigOptions> Value; + QMap<QString, Value> m_map; + mutable QReadWriteLock m_mutex; +}; +} + +#endif |