logic: add name delimiter to NameHierarchy
* NameHierarchy now looks like this: <delimiter>\tm<all the rest> * made new baseclasses for SettingsMigration and SettingsMigrator * Use the migration system for SqliteBookmarkStorage as well
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#ifndef MIGRATION_H
|
||||
#define MIGRATION_H
|
||||
|
||||
template <typename MigratableType>
|
||||
class Migration
|
||||
{
|
||||
public:
|
||||
virtual ~Migration();
|
||||
virtual void apply(MigratableType* migratable) const = 0;
|
||||
};
|
||||
|
||||
template <typename MigratableType>
|
||||
Migration<MigratableType>::~Migration()
|
||||
{
|
||||
}
|
||||
|
||||
#endif // MIGRATION_H
|
||||
@@ -0,0 +1,87 @@
|
||||
#ifndef MIGRATOR_H
|
||||
#define MIGRATOR_H
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
#include "utility/migration/Migration.h"
|
||||
|
||||
template <typename MigratableType>
|
||||
class Migrator
|
||||
{
|
||||
public:
|
||||
virtual ~Migrator();
|
||||
|
||||
void addMigration(size_t targetVersion, std::shared_ptr<Migration<MigratableType>> migration);
|
||||
|
||||
bool willMigrate(const MigratableType* migratable, size_t targetVersion) const;
|
||||
bool migrate(MigratableType* migratable, size_t targetVersion) const;
|
||||
|
||||
private:
|
||||
typedef std::multimap<size_t, std::shared_ptr<Migration<MigratableType>>> MigrationMap;
|
||||
MigrationMap m_migrations;
|
||||
};
|
||||
|
||||
|
||||
template <typename MigratableType>
|
||||
Migrator<MigratableType>::~Migrator()
|
||||
{
|
||||
}
|
||||
|
||||
template <typename MigratableType>
|
||||
void Migrator<MigratableType>::addMigration(size_t targetVersion, std::shared_ptr<Migration<MigratableType>> migration)
|
||||
{
|
||||
if (migration)
|
||||
{
|
||||
m_migrations.emplace(targetVersion, migration);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename MigratableType>
|
||||
bool Migrator<MigratableType>::willMigrate(const MigratableType* migratable, size_t targetVersion) const
|
||||
{
|
||||
size_t sourceVersion = migratable->getVersion();
|
||||
|
||||
if (sourceVersion < targetVersion)
|
||||
{
|
||||
for (; sourceVersion <= targetVersion; sourceVersion++)
|
||||
{
|
||||
std::pair<MigrationMap::const_iterator, MigrationMap::const_iterator> ret;
|
||||
ret = m_migrations.equal_range(sourceVersion);
|
||||
|
||||
for (MigrationMap::const_iterator it = ret.first; it != ret.second; it++)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename MigratableType>
|
||||
bool Migrator<MigratableType>::migrate(MigratableType* migratable, size_t targetVersion) const
|
||||
{
|
||||
size_t sourceVersion = migratable->getVersion();
|
||||
|
||||
if (sourceVersion < targetVersion)
|
||||
{
|
||||
for (; sourceVersion <= targetVersion; sourceVersion++)
|
||||
{
|
||||
std::pair<MigrationMap::const_iterator, MigrationMap::const_iterator> ret;
|
||||
ret = m_migrations.equal_range(sourceVersion);
|
||||
|
||||
for (MigrationMap::const_iterator it = ret.first; it != ret.second; it++)
|
||||
{
|
||||
it->second->apply(migratable);
|
||||
}
|
||||
}
|
||||
|
||||
migratable->setVersion(targetVersion);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif // MIGRATOR_H
|
||||
Reference in New Issue
Block a user