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