summaryrefslogtreecommitdiffstats
path: root/src/connection-pool.h
blob: 4637a3cc1b5c0227759f2633d85bf9fee15bc233 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Author:
 *   mderezynski <mderezynski@users.sourceforge.net>
 *
 * Copyright (C) 2006 Author
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#ifndef CONNECTION_POOL_H
#define CONNECTION_POOL_H

#include <glib-object.h>
#include <sigc++/sigc++.h>

#include <map>
#include <stddef.h>
#include <string>
#include <exception>

namespace Inkscape {

/**
 * @class ConnectionPool 
 *  an auxilliary class to manage sigc::connections as if the referred object
 *  were a GObject; in that way, the class that holds a ConnectionPool does not
 *  need to also hold several sigc::connection objects
 */
class ConnectionPool {
public:
    typedef std::map<std::string, sigc::connection*> ConnectionMap;

    struct NameExistsException : public std::exception {
        virtual const char* what() const throw() { return "Inkscape::ConnectionPool: name exists"; }
    };
    struct NameDoesNotExistException : public std::exception {
        virtual const char* what() const throw() { return "Inkscape::ConnectionPool: name doesn't exist"; }
    };

    void add_connection(std::string name, sigc::connection* connection) {
        if (_map.find(name) != _map.end()) {
            throw NameExistsException();
        }
        _map.insert(std::make_pair(name, connection)); 
    }

    void del_connection(std::string name) {
        ConnectionMap::iterator iter = _map.find(name); 
        if (iter == _map.end()) {
            throw NameDoesNotExistException();
        }
        sigc::connection* connection = (*iter).second;
        connection->disconnect();
        delete connection;
    }


    static Inkscape::ConnectionPool* new_connection_pool(std::string name) {
        return new ConnectionPool(name); 
    }

    static void del_connection_pool(ConnectionPool* pool) {
        delete pool;
    }

    static void connect_destroy(GObject *obj, ConnectionPool *pool) {
        g_object_connect (obj, "swapped-signal::destroy", G_CALLBACK(del_connection_pool), pool, NULL);
    }

    operator std::string() {
        return _name;
    }

private:
    ConnectionPool(std::string name) : _name(name) {}
    virtual ~ConnectionPool() {
        for (ConnectionMap::iterator iter = _map.begin(), end = _map.end(); iter != end; ++iter) {
            sigc::connection* connection = (*iter).second;
            connection->disconnect();
            delete connection;
        }
    }

    ConnectionMap _map;
    std::string _name;
};
}

#endif

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8 :