1bb02e0acSNicolas Bonnefon /* 2bb02e0acSNicolas Bonnefon * Copyright (C) 2011 Nicolas Bonnefon and other contributors 3bb02e0acSNicolas Bonnefon * 4bb02e0acSNicolas Bonnefon * This file is part of glogg. 5bb02e0acSNicolas Bonnefon * 6bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9bb02e0acSNicolas Bonnefon * (at your option) any later version. 10bb02e0acSNicolas Bonnefon * 11bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15bb02e0acSNicolas Bonnefon * 16bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18bb02e0acSNicolas Bonnefon */ 19bb02e0acSNicolas Bonnefon 20bb02e0acSNicolas Bonnefon #ifndef PERSISTENTINFO_H 21bb02e0acSNicolas Bonnefon #define PERSISTENTINFO_H 22bb02e0acSNicolas Bonnefon 23*11582726SNicolas Bonnefon #include <memory> 24*11582726SNicolas Bonnefon 25bb02e0acSNicolas Bonnefon #include <QSettings> 26bb02e0acSNicolas Bonnefon #include <QHash> 27bb02e0acSNicolas Bonnefon 28bb02e0acSNicolas Bonnefon class Persistable; 29bb02e0acSNicolas Bonnefon 30bb02e0acSNicolas Bonnefon // Singleton class managing the saving of persistent data to permanent storage 31bb02e0acSNicolas Bonnefon // Clients must implement Persistable and register with this object, they can 32bb02e0acSNicolas Bonnefon // then be saved/loaded. 33bb02e0acSNicolas Bonnefon class PersistentInfo { 34bb02e0acSNicolas Bonnefon public: 35bb02e0acSNicolas Bonnefon // Initialise the storage backend for the Persistable, migrating the settings 36bb02e0acSNicolas Bonnefon // if needed. Must be called before any other function. 37bb02e0acSNicolas Bonnefon void migrateAndInit(); 38bb02e0acSNicolas Bonnefon // Register a Persistable 39*11582726SNicolas Bonnefon void registerPersistable( std::shared_ptr<Persistable> object, 40*11582726SNicolas Bonnefon const QString& name ); 41bb02e0acSNicolas Bonnefon // Get a Persistable (or NULL if it doesn't exist) 42*11582726SNicolas Bonnefon std::shared_ptr<Persistable> getPersistable( const QString& name ); 43bb02e0acSNicolas Bonnefon // Save a persistable to its permanent storage 44bb02e0acSNicolas Bonnefon void save( const QString& name ); 45bb02e0acSNicolas Bonnefon // Retrieve a persistable from permanent storage 46bb02e0acSNicolas Bonnefon void retrieve( const QString& name ); 47bb02e0acSNicolas Bonnefon 48bb02e0acSNicolas Bonnefon private: 49bb02e0acSNicolas Bonnefon // Can't be constructed or copied (singleton) 50bb02e0acSNicolas Bonnefon PersistentInfo(); 51bb02e0acSNicolas Bonnefon PersistentInfo( const PersistentInfo& ); 52bb02e0acSNicolas Bonnefon ~PersistentInfo(); 53bb02e0acSNicolas Bonnefon 54bb02e0acSNicolas Bonnefon // Has migrateAndInit() been called? 55bb02e0acSNicolas Bonnefon bool initialised_; 56bb02e0acSNicolas Bonnefon 57bb02e0acSNicolas Bonnefon // List of persistables 58*11582726SNicolas Bonnefon QHash<QString, std::shared_ptr<Persistable>> objectList_; 59bb02e0acSNicolas Bonnefon 60bb02e0acSNicolas Bonnefon // Qt setting object 61bb02e0acSNicolas Bonnefon QSettings* settings_; 62bb02e0acSNicolas Bonnefon 63bb02e0acSNicolas Bonnefon // allow this function to create one instance 64bb02e0acSNicolas Bonnefon friend PersistentInfo& GetPersistentInfo(); 65bb02e0acSNicolas Bonnefon }; 66bb02e0acSNicolas Bonnefon 67bb02e0acSNicolas Bonnefon PersistentInfo& GetPersistentInfo(); 68bb02e0acSNicolas Bonnefon 69*11582726SNicolas Bonnefon // Global function used to get a reference to an object 70*11582726SNicolas Bonnefon // from the PersistentInfo store 71bb02e0acSNicolas Bonnefon template<typename T> Persistent(const char * name)72*11582726SNicolas Bonnefonstd::shared_ptr<T> Persistent( const char* name ) 73bb02e0acSNicolas Bonnefon { 74*11582726SNicolas Bonnefon std::shared_ptr<Persistable> p = 75*11582726SNicolas Bonnefon GetPersistentInfo().getPersistable( QString( name ) ); 76*11582726SNicolas Bonnefon return std::dynamic_pointer_cast<T>(p); 77bb02e0acSNicolas Bonnefon } 78bb02e0acSNicolas Bonnefon 79*11582726SNicolas Bonnefon template<typename T> PersistentCopy(const char * name)80*11582726SNicolas Bonnefonstd::shared_ptr<T> PersistentCopy( const char* name ) 81*11582726SNicolas Bonnefon { 82*11582726SNicolas Bonnefon std::shared_ptr<Persistable> p = 83*11582726SNicolas Bonnefon GetPersistentInfo().getPersistable( QString( name ) ); 84*11582726SNicolas Bonnefon return std::make_shared<T>( *( std::dynamic_pointer_cast<T>(p) ) ); 85*11582726SNicolas Bonnefon } 86bb02e0acSNicolas Bonnefon #endif 87