~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/include/projectloader_hooks.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
 
3
 * http://www.gnu.org/licenses/lgpl-3.0.html
 
4
 */
 
5
 
 
6
#ifndef PROJECTLOADER_HOOKS_H
 
7
#define PROJECTLOADER_HOOKS_H
 
8
 
 
9
#include "settings.h"
 
10
 
 
11
class TiXmlElement;
 
12
class cbProject;
 
13
 
 
14
/** Provides static functions to add hooks to the project loading/saving procedure. */
 
15
namespace ProjectLoaderHooks
 
16
{
 
17
    /** Abstract base hook functor interface. */
 
18
    class DLLIMPORT HookFunctorBase
 
19
    {
 
20
        public:
 
21
            virtual ~HookFunctorBase(){}
 
22
            virtual void Call(cbProject*, TiXmlElement*, bool) const = 0;
 
23
    };
 
24
 
 
25
    /** Functor class for use as a project loading/saving hook.
 
26
      * Passed as the first parameter in RegisterHook() and
 
27
      * UnregisterHook().
 
28
      *
 
29
      * example:
 
30
      * ProjectLoaderHooks::HookFunctorBase* myhook = new ProjectLoaderHooks::HookFunctor<MyClass>(this, &MyClass::OnHookCalled);
 
31
      * int id = ProjectLoaderHooks::RegisterHook(myhook);
 
32
      * ...
 
33
      * (and before your class' destruction - or earlier):
 
34
      * ProjectLoaderHooks::UnregisterHook(id, true);
 
35
      *
 
36
      * Member functions used as hook callbacks must have the following signature:
 
37
      * void YourFunctionName(cbProject*, TiXmlElement*, bool)
 
38
      *
 
39
      * Use normal TinyXML procedures to work with the TiXmlElement* argument.
 
40
      * The isLoading argument is true if your hook is called when the project is being loaded,
 
41
      * and false when the project is saved.
 
42
      */
 
43
    template<class T> class DLLIMPORT HookFunctor : public HookFunctorBase
 
44
    {
 
45
        public:
 
46
            typedef void (T::*Func)(cbProject*, TiXmlElement*, bool);
 
47
            HookFunctor(T* obj, Func func)
 
48
                : m_pObj(obj),
 
49
                m_pFunc(func)
 
50
            {}
 
51
            virtual void Call(cbProject* project, TiXmlElement* elem, bool isLoading) const
 
52
            {
 
53
                if (m_pObj && m_pFunc)
 
54
                    (m_pObj->*m_pFunc)(project, elem, isLoading);
 
55
            }
 
56
        protected:
 
57
            T* m_pObj;
 
58
            Func m_pFunc;
 
59
    };
 
60
 
 
61
    /** Register a project loading/saving hook.
 
62
      * @param functor The functor to use as a callback.
 
63
      * @return An ID. Use this to unregister your hook later.
 
64
      */
 
65
    extern DLLIMPORT int RegisterHook(HookFunctorBase* functor);
 
66
    /** Unregister a previously registered project loading/saving hook.
 
67
      * @param id The hook's ID. You should have the ID from when RegisterHook() was called.
 
68
      * @param deleteHook If true, the hook will be deleted (default). If not, it's
 
69
      * up to you to delete it.
 
70
      * @return The functor. If @c deleteHook was true, it always returns NULL.
 
71
      */
 
72
    extern DLLIMPORT HookFunctorBase* UnregisterHook(int id, bool deleteHook = true);
 
73
    /** Are there any hooks registered?
 
74
      * @return True if any hooks are registered, false if none.
 
75
      */
 
76
    extern DLLIMPORT bool HasRegisteredHooks();
 
77
    /** Call all registered hooks using the supplied parameters.
 
78
      * This is called by ProjectLoader.
 
79
      * @param project The project in question.
 
80
      * @param elem The XML element under which the called hook can read/write.
 
81
      * @param isLoading True if the project is being loaded, false if being saved.
 
82
      */
 
83
    extern DLLIMPORT void CallHooks(cbProject* project, TiXmlElement* elem, bool isLoading);
 
84
};
 
85
 
 
86
#endif // PROJECTLOADER_HOOKS_H