File: extfield.h

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (84 lines) | stat: -rw-r--r-- 2,973 bytes parent folder | download | duplicates (4)
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
///////////////////////////////////////////////////////////////////////////////
// Name:        wx/private/extfield.h
// Purpose:     Declare wxExternalField helper
// Author:      Vadim Zeitlin
// Created:     2017-11-21
// Copyright:   (c) 2017 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_PRIVATE_EXTFIELD_H_
#define _WX_PRIVATE_EXTFIELD_H_

// ----------------------------------------------------------------------------
// wxExternalField: store object data outside of it
// ----------------------------------------------------------------------------

// This class allows to store some data without consuming space for the objects
// that don't need it and can be useful for avoiding to add rarely used fields
// to the classes that are used by many objects, e.g. wxWindow.
//
// Note that using this class costs in speed and convenience of access to the
// field, which requires a hash lookup instead of accessing it directly. It
// also only currently works for heap-allocated fields as it's probably never
// worth using it for fields of simple types.
//
// Template parameter Object is the class that "contains" the field, Field is
// the type of the field itself and FieldMap is the hash map, defined
// separately using WX_DECLARE_HASH_MAP(), with Object* as the key and Field*
// as the value type.
template <typename Object, typename Field, typename FieldMap>
class wxExternalField
{
public:
    typedef Object ObjectType;
    typedef Field FieldType;
    typedef FieldMap MapType;

    // Store the field object to be used for the given object, replacing the
    // existing one, if any.
    //
    // This method takes ownership of the field pointer which will be destroyed
    // by EraseForObject().
    static void StoreForObject(ObjectType* obj, FieldType* field)
    {
        const typename MapType::iterator it = ms_map.find(obj);
        if ( it != ms_map.end() )
        {
            delete it->second;
            it->second = field;
        }
        else
        {
            ms_map.insert(typename MapType::value_type(obj, field));
        }
    }

    // Find the object for the corresponding window.
    static FieldType* FromObject(ObjectType* obj)
    {
        const typename MapType::const_iterator it = ms_map.find(obj);
        return it == ms_map.end() ? NULL : it->second;
    }

    // Erase the object used for the corresponding window, return true if there
    // was one or false otherwise.
    static bool EraseForObject(ObjectType* obj)
    {
        const typename MapType::iterator it = ms_map.find(obj);
        if ( it == ms_map.end() )
            return false;

        delete it->second;
        ms_map.erase(it);
        return true;
    }

private:
    static FieldMap ms_map;
};

template <typename O, typename F, typename M>
M wxExternalField<O, F, M>::ms_map;

#endif // _WX_PRIVATE_EXTFIELD_H_