Commits

Wei Cheng Pan committed 54a92f4

Change setting tool.

Comments (0)

Files changed (5)

Krapture/core/include/xTitan/Utility/Logger.hpp

 
 class XTITAN_CORE_DLL Logger {
 public :
+	static void initialize();
 	static Logger & instance();
 
 	void setFileName( const QString & filename );

Krapture/core/include/xTitan/Utility/Setting.hpp

 
 #include <memory>
 
-#include <QtCore/QString>
+#include <QtCore/QVariant>
 
 #include "xTitan/Utility/CoreConfig.hpp"
 
 	static void initialize();
 	static Setting & instance();
 
-	QString get( const QString & key ) const;
-	void set( const QString & key, const QString & value );
+	QVariant get( const QString & key, const QVariant & defaultValue = QVariant() ) const;
+	void set( const QString & key, const QVariant & value );
 
 	void save() const;
 

Krapture/core/src/Utility/Logger.cpp

 
 Logger::Private::Private():
 lock(),
-tcDir( Setting::instance().get( "TestCasePath" ) ),
+tcDir( Setting::instance().get( "TestCasePath" ).toString() ),
 fout( this->tcDir.filePath( QDateTime::currentDateTime().toString( "yyyyMMdd_hhmmss" ) + ".log" ) ),
 stream() {
 	if( !this->fout.isOpen() ) {
 	this->stream.setCodec( QTextCodec::codecForName( "UTF-8" ) );
 }
 
+void Logger::initialize() {
+	if( Private::self ) {
+		return;
+	}
+	Private::self.reset( new Logger, Private::destroy );
+}
+
 /**
  * @warning This function is NOT thread-safe
  */
 Logger & Logger::instance() {
 	if( !Private::self ) {
-		Private::self.reset( new Logger, Private::destroy );
+		// FIXME should throw exception
+		Logger::initialize();
 	}
 	return *Private::self;
 }

Krapture/core/src/Utility/Setting.cpp

 	QReadLocker locker( &this->lock );
 
 	QSettings s( this->filePath, QSettings::IniFormat );
-	std::for_each( this->settings.begin(), this->settings.end(), [&s]( const std::pair< QString, QString > & p )->void {
-		s.setValue( p.first, p.second );
-	} );
+	for( auto it = this->settings.begin(); it != this->settings.end(); ++it ) {
+		s.setValue( it->first, it->second );
+	}
 
 	locker.unlock();
 }
 
 Setting::Setting():
 p_( new Private ) {
-	QDir appDir( QCoreApplication::applicationDirPath() );
+	QDir appDir = QCoreApplication::applicationDirPath();
 	this->setFilePath( appDir.filePath( SETTING_FILENAME ) );
 }
 
 }
 
 void Setting::setFilePath( const QString & path ) {
-	QWriteLocker locker( &this->p_->lock );
+	QWriteLocker locker = &this->p_->lock;
+	Q_UNUSED( locker );
 
 	this->p_->filePath = path;
 	this->p_->settings.clear();
 	std::for_each( keys.begin(), keys.end(), [this, &s]( const QString & key )->void {
 		this->p_->settings.insert( std::make_pair( key, s.value( key ).toString() ) );
 	} );
-
-	locker.unlock();
 }
 
 void Setting::save() const {
 	this->p_->save();
 }
 
-QString Setting::get( const QString & key ) const {
-	QReadLocker locker( &this->p_->lock );
+QVariant Setting::get( const QString & key, const QVariant & defaultValue /*= QVariant()*/ ) const {
+	QReadLocker locker = &this->p_->lock;
+	Q_UNUSED( locker );
 
-	std::map< QString, QString >::const_iterator it = this->p_->settings.find( key );
-	assert( it != this->p_->settings.end() );
+	auto it = this->p_->settings.find( key );
+	if( it == this->p_->settings.end() ) {
+		if( defaultValue.isValid() ) {
+			return defaultValue;
+		}
+		return QVariant();
+	}
 
-	locker.unlock();
 	return it->second;
 }
 
-void Setting::set( const QString & key, const QString & value ) {
-	QWriteLocker locker( &this->p_->lock );
+void Setting::set( const QString & key, const QVariant & value ) {
+	QWriteLocker locker = &this->p_->lock;
+	Q_UNUSED( locker );
 
-	std::map< QString, QString >::iterator it( this->p_->settings.find( key ) );
+	auto it( this->p_->settings.find( key ) );
 	if( it != this->p_->settings.end() ) {
 		it->second = value;
 	} else {
 		this->p_->settings.insert( std::make_pair( key, value ) );
 	}
-
-	locker.unlock();
 }

Krapture/core/src/Utility/Setting_p.hpp

 
 public:
 	QReadWriteLock lock;
-	std::map< QString, QString > settings;
+	std::map< QString, QVariant > settings;
 	QString filePath;
 };