summaryrefslogtreecommitdiffstats
path: root/tdegtk/tdegtk-datamap.h
blob: e82cdc0d93d4fa0dd6ba3922e66f7220973c1512 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef oxygendatamap_h
#define oxygendatamap_h
/*
* this file was largely taken from the oxygen gtk engine
* Copyright (c) 2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
*
* This  library is free  software; you can  redistribute it and/or
* modify it  under  the terms  of the  GNU Lesser  General  Public
* License  as published  by the Free  Software  Foundation; either
* version 2 of the License, or(at your option ) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License  along  with  this library;  if not,  write to  the Free
* Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

#include <cassert>
#include <map>
#include <gtk/gtk.h>

    //! generic class to map data to widgets
    /*
    Note: I'm not sure about memory management. At some point one might need to allocate the registered
    data on the heap rather than on the stack, to be able to safely pass the data pointer around via callbacks.
    The current implementation should make that possible without external code change, provided that the map
    content is properly deleted (as opposed to erased) in destructor and 'unregister' method.
    */
    template <typename T>
    class DataMap
    {

        public:

        //! constructor
        DataMap( void ):
            _lastWidget( 0L ),
            _lastData( 0L )
        {}

        virtual ~DataMap(){}

        //! insert new widget
        inline T& registerWidget( GtkWidget* widget )
        {
            T& data( _map.insert( std::make_pair( widget, T() ) ).first->second );
            _lastWidget = widget;
            _lastData = &data;
            return data;
        }

        //! true if widget is in list
        virtual bool contains( GtkWidget* widget )
        {

            // check against last widget
            if( widget == _lastWidget ) return true;

            // find in map, returns false if not found
            typename Map::iterator iter = _map.find( widget );
            if( iter == _map.end() ) return false;

            // store as last widget/last data, to speed up lookup.
            _lastWidget = widget;
            _lastData = &iter->second;
            return true;

        }

        //! return value
        virtual T& value( GtkWidget* widget )
        {

            // check against last widget
            if( widget == _lastWidget ) return *_lastData;

            // find in map, abort if not found
            typename Map::iterator iter(  _map.find( widget ) );
            assert( iter != _map.end() );

            // store as last widget/last data, to speed up lookup.
            _lastWidget = widget;
            _lastData = &iter->second;
            return iter->second;

        }

        //! erase
        virtual void erase( GtkWidget* widget )
        {

            // clear last widget and data, if match
            if( _lastWidget == widget )
            {
                _lastWidget = 0L;
                _lastData = 0L;
            }

            // erase from map
            _map.erase( widget );

        }

        //! connect all widgets in map
        void connectAll( void )
        {
            for( typename Map::iterator iter = _map.begin(); iter != _map.end(); iter++ )
            { iter->second.connect( iter->first ); }
        }


        //! connect all widgets in map
        void disconnectAll( void )
        {
            for( typename Map::iterator iter = _map.begin(); iter != _map.end(); iter++ )
            { iter->second.disconnect( iter->first ); }
        }

        //! erase
        virtual void clear( void )
        {

            _lastWidget = 0L;
            _lastData = 0L;
            _map.clear();

        }

        //! retrieve internal map
        typedef std::map<GtkWidget*, T> Map;
        Map& map( void )
        { return _map; }

        //! retrieve internal map
        const Map& map( void ) const
        { return _map; }

        protected:

        //! copy constructor is private
        DataMap( const DataMap& )
        { assert( false ); }

        //! assignment operator
        DataMap& operator = ( const DataMap& )
        {
            assert( false );
            return *this;
        }

        private:

        //! pointer to last inquired widget
        GtkWidget* _lastWidget;

        //! pointer to last retrieved data
        T* _lastData;

        //! internal map between widget and data
        Map _map;

    };

#endif