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 <memory>
24 
25 #include <QSettings>
26 #include <QHash>
27 
28 class Persistable;
29 
30 // Singleton class managing the saving of persistent data to permanent storage
31 // Clients must implement Persistable and register with this object, they can
32 // then be saved/loaded.
33 class PersistentInfo {
34   public:
35     // Initialise the storage backend for the Persistable, migrating the settings
36     // if needed. Must be called before any other function.
37     void migrateAndInit();
38     // Register a Persistable
39     void registerPersistable( std::shared_ptr<Persistable> object,
40             const QString& name );
41     // Get a Persistable (or NULL if it doesn't exist)
42     std::shared_ptr<Persistable> getPersistable( const QString& name );
43     // Save a persistable to its permanent storage
44     void save( const QString& name );
45     // Retrieve a persistable from permanent storage
46     void retrieve( const QString& name );
47 
48   private:
49     // Can't be constructed or copied (singleton)
50     PersistentInfo();
51     PersistentInfo( const PersistentInfo& );
52     ~PersistentInfo();
53 
54     // Has migrateAndInit() been called?
55     bool initialised_;
56 
57     // List of persistables
58     QHash<QString, std::shared_ptr<Persistable>> objectList_;
59 
60     // Qt setting object
61     QSettings* settings_;
62 
63     // allow this function to create one instance
64     friend PersistentInfo& GetPersistentInfo();
65 };
66 
67 PersistentInfo& GetPersistentInfo();
68 
69 // Global function used to get a reference to an object
70 // from the PersistentInfo store
71 template<typename T>
Persistent(const char * name)72 std::shared_ptr<T> Persistent( const char* name )
73 {
74     std::shared_ptr<Persistable> p =
75         GetPersistentInfo().getPersistable( QString( name ) );
76     return std::dynamic_pointer_cast<T>(p);
77 }
78 
79 template<typename T>
PersistentCopy(const char * name)80 std::shared_ptr<T> PersistentCopy( const char* name )
81 {
82     std::shared_ptr<Persistable> p =
83         GetPersistentInfo().getPersistable( QString( name ) );
84     return std::make_shared<T>( *( std::dynamic_pointer_cast<T>(p) ) );
85 }
86 #endif
87