File: persistentinfo.h

package info (click to toggle)
glogg 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 832 kB
  • sloc: cpp: 6,649; sh: 66; perl: 32; sed: 25; makefile: 11
file content (73 lines) | stat: -rw-r--r-- 2,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (C) 2011 Nicolas Bonnefon and other contributors
 *
 * This file is part of glogg.
 *
 * glogg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * glogg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef PERSISTENTINFO_H
#define PERSISTENTINFO_H

#include <QSettings>

class Persistable;

// Singleton class managing the saving of persistent data to permanent storage
// Clients must implement Persistable and register with this object, they can
// then be saved/loaded.
class PersistentInfo {
  public:
    // Initialise the storage backend for the Persistable, migrating the settings
    // if needed. Must be called before any other function.
    void migrateAndInit();
    // Register a Persistable
    void registerPersistable( Persistable* object, const QString& name );
    // Get a Persistable (or NULL if it doesn't exist)
    Persistable* getPersistable( const QString& name );
    // Save a persistable to its permanent storage
    void save( const QString& name );
    // Retrieve a persistable from permanent storage
    void retrieve( const QString& name );

  private:
    // Can't be constructed or copied (singleton)
    PersistentInfo();
    PersistentInfo( const PersistentInfo& );
    ~PersistentInfo();

    // Has migrateAndInit() been called?
    bool initialised_;

    // List of persistables
    QHash<QString, Persistable*> objectList_;

    // Qt setting object
    QSettings* settings_;

    // allow this function to create one instance
    friend PersistentInfo& GetPersistentInfo();
};

PersistentInfo& GetPersistentInfo();

// Global function used to get an object from the PersistentInfo store
template<typename T>
T& Persistent( const char* name )
{
    Persistable* p = GetPersistentInfo().getPersistable( QString( name ) );
    return dynamic_cast<T&>(*p);
}

#endif