~mc-return/compiz/compiz.merge-src-header-files-cleanup

« back to all changes in this revision

Viewing changes to src/pluginclasshandler/include/core/pluginclasshandler.h

  • Committer: MC Return
  • Date: 2013-06-20 05:56:13 UTC
  • mfrom: (3724.2.16 0.9.10)
  • Revision ID: mc.return@gmx.net-20130620055613-8dp00nmw2jpm3ptu
Merged latest lp:compiz and fixed conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
 */
46
46
extern unsigned int pluginClassHandlerIndex;
47
47
 
 
48
namespace compiz
 
49
{
 
50
namespace plugin
 
51
{
 
52
namespace internal
 
53
{
 
54
class PluginKey;
 
55
/**
 
56
 * LoadedPluginClassBridge
 
57
 *
 
58
 * This template essentially exists so that we can reduce the
 
59
 * scope of functions which are allowed to mark plugin classes
 
60
 * as instantiatable as we can't really inject the interface
 
61
 * from PluginClassHandler to do that anywhere. We also can't
 
62
 * forward declare a nested class declaration, but we can forward
 
63
 * declare PluginKey, which users should inherit from and take
 
64
 * it by reference. If the class we're depending on can only be
 
65
 * defined in one other place along with a private constructor
 
66
 * accessible only to a friend it means that we've effectively
 
67
 * limited the scope of users of this class.
 
68
 */
 
69
template <class Tp, class Tb, int ABI = 0>
 
70
class LoadedPluginClassBridge
 
71
{
 
72
    public:
 
73
 
 
74
        static void
 
75
        allowInstantiations (const PluginKey &);
 
76
 
 
77
        static void
 
78
        disallowInstantiations (const PluginKey &);
 
79
};
 
80
}
 
81
}
 
82
}
 
83
 
48
84
template<class Tp, class Tb, int ABI = 0>
49
 
class PluginClassHandler {
 
85
class PluginClassHandler
 
86
{
50
87
    public:
 
88
 
 
89
        typedef Tp ClassPluginType;
 
90
        typedef Tb ClassPluginBaseType;
 
91
 
51
92
        PluginClassHandler (Tb *);
52
93
        ~PluginClassHandler ();
53
94
 
90
131
        static bool initializeIndex (Tb *base);
91
132
        static inline Tp * getInstance (Tb *base);
92
133
 
 
134
        static void allowInstantiations ();
 
135
        static void disallowInstantiations ();
 
136
 
 
137
        template <class Plugin, class Base, int PluginABI>
 
138
        friend class compiz::plugin::internal::LoadedPluginClassBridge;
 
139
 
93
140
    private:
94
141
        bool mFailed;
95
142
        Tb   *mBase;
96
143
 
97
144
        static PluginClassIndex mIndex;
 
145
        static bool             mPluginLoaded;
98
146
};
99
147
 
 
148
namespace compiz
 
149
{
 
150
namespace plugin
 
151
{
 
152
namespace internal
 
153
{
 
154
template <class Tp, class Tb, int ABI>
 
155
void
 
156
LoadedPluginClassBridge <Tp, Tb, ABI>::allowInstantiations (const PluginKey &)
 
157
{
 
158
    PluginClassHandler <Tp, Tb, ABI>::allowInstantiations ();
 
159
}
 
160
 
 
161
template <class Tp, class Tb, int ABI>
 
162
void
 
163
LoadedPluginClassBridge <Tp, Tb, ABI>::disallowInstantiations (const PluginKey &)
 
164
{
 
165
    PluginClassHandler <Tp, Tb, ABI>::disallowInstantiations ();
 
166
}
 
167
}
 
168
}
 
169
}
 
170
 
100
171
template<class Tp, class Tb, int ABI>
101
172
PluginClassIndex PluginClassHandler<Tp,Tb,ABI>::mIndex;
102
173
 
 
174
template<class Tp, class Tb, int ABI>
 
175
bool PluginClassHandler<Tp,Tb,ABI>::mPluginLoaded = false;
 
176
 
103
177
/**
104
178
 * Attaches a unique instance of the specified plugin class to a
105
179
 * unique instance of a specified base class
253
327
Tp *
254
328
PluginClassHandler<Tp,Tb,ABI>::get (Tb *base)
255
329
{
 
330
    /* Never instantiate an instance of this class
 
331
     * if the relevant plugin has not been loaded
 
332
     */
 
333
    if (!mPluginLoaded)
 
334
        return NULL;
 
335
 
256
336
    /* Always ensure that the index is initialized before
257
337
     * calls to ::get */
258
338
    if (!mIndex.initiated)
259
339
        initializeIndex (base);
 
340
 
260
341
    /* If pluginClassHandlerIndex == mIndex.pcIndex it means that our
261
342
     * mIndex.index is fresh and can be used directly without needing
262
343
     * to fetch it from ValueHolder */
263
344
    if (mIndex.initiated && pluginClassHandlerIndex == mIndex.pcIndex)
264
345
        return getInstance (base);
 
346
 
265
347
    /* If allocating or getting the updated index failed at any point
266
348
     * then just return NULL we don't know where our private data is stored */
267
349
    if (mIndex.failed && pluginClassHandlerIndex == mIndex.pcIndex)
285
367
    }
286
368
}
287
369
 
 
370
template<class Tp, class Tb, int ABI>
 
371
void
 
372
PluginClassHandler<Tp, Tb, ABI>::allowInstantiations ()
 
373
{
 
374
    mPluginLoaded = true;
 
375
}
 
376
 
 
377
template<class Tp, class Tb, int ABI>
 
378
void
 
379
PluginClassHandler<Tp, Tb, ABI>::disallowInstantiations ()
 
380
{
 
381
    mPluginLoaded = false;
 
382
}
 
383
 
288
384
#endif