summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/Pluma/Pluma.hpp
blob: a9d614ee65034ee3906f78120a626cc76fe0375b (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
171
////////////////////////////////////////////////////////////

//

// Pluma - Plug-in Management Framework

// Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)

//

// This software is provided 'as-is', without any express or implied warranty.

// In no event will the authors be held liable for any damages arising from the use of this software.

//

// Permission is granted to anyone to use this software for any purpose,

// including commercial applications, and to alter it and redistribute it freely,

// subject to the following restrictions:

//

// 1. The origin of this software must not be misrepresented;

//    you must not claim that you wrote the original software.

//    If you use this software in a product, an acknowledgment

//    in the product documentation would be appreciated but is not required.

//

// 2. Altered source versions must be plainly marked as such,

//    and must not be misrepresented as being the original software.

//

// 3. This notice may not be removed or altered from any source distribution.

//

////////////////////////////////////////////////////////////


#ifndef PLUMA_PLUMA_HPP

#define PLUMA_PLUMA_HPP


////////////////////////////////////////////////////////////

// Headers

////////////////////////////////////////////////////////////

#include <Pluma/Config.hpp>

#include <Pluma/Provider.hpp>

#include <Pluma/PluginManager.hpp>


////////////////////////////////////////////////////////////

// Andy macro to convert parameter to string

////////////////////////////////////////////////////////////

#define PLUMA_2STRING(X) #X


////////////////////////////////////////////////////////////

// Macro that helps host applications defining

// their provider classes

////////////////////////////////////////////////////////////

#define PLUMA_PROVIDER_HEADER(TYPE)\

PLUMA_PROVIDER_HEADER_BEGIN(TYPE)\
virtual TYPE* create() const = 0;\
PLUMA_PROVIDER_HEADER_END

////////////////////////////////////////////////////////////

// Macro that generate first part of the provider definition

////////////////////////////////////////////////////////////

#define PLUMA_PROVIDER_HEADER_BEGIN(TYPE)\

class TYPE##Provider: public pluma::Provider{\

private:\
    friend class pluma::Pluma;\
    static const unsigned int PLUMA_INTERFACE_VERSION;\
    static const unsigned int PLUMA_INTERFACE_LOWEST_VERSION;\
    static const std::string PLUMA_PROVIDER_TYPE;\
    std::string plumaGetType() const{ return PLUMA_PROVIDER_TYPE; }\
public:\
    unsigned int getVersion() const{ return PLUMA_INTERFACE_VERSION; }

////////////////////////////////////////////////////////////

// Macro that generate last part of the provider definition

////////////////////////////////////////////////////////////

#define PLUMA_PROVIDER_HEADER_END };


////////////////////////////////////////////////////////////

// Macro that generate the provider declaration

////////////////////////////////////////////////////////////

#define PLUMA_PROVIDER_SOURCE(TYPE, Version, LowestVersion)\

const std::string TYPE##Provider::PLUMA_PROVIDER_TYPE = PLUMA_2STRING( TYPE );\

const unsigned int TYPE##Provider::PLUMA_INTERFACE_VERSION = Version;\

const unsigned int TYPE##Provider::PLUMA_INTERFACE_LOWEST_VERSION = LowestVersion;



////////////////////////////////////////////////////////////

// Macro that helps plugins generating their provider implementations

// PRE: SPECIALIZED_TYPE must inherit from BASE_TYPE

////////////////////////////////////////////////////////////

#define PLUMA_INHERIT_PROVIDER(SPECIALIZED_TYPE, BASE_TYPE)\

class SPECIALIZED_TYPE##Provider: public BASE_TYPE##Provider{\

public:\
    BASE_TYPE * create() const{ return new SPECIALIZED_TYPE (); }\
};


namespace pluma{

////////////////////////////////////////////////////////////

/// \brief Pluma plugins management

///

////////////////////////////////////////////////////////////

class Pluma: public PluginManager{

public:
    ////////////////////////////////////////////////////////////

    /// \brief Default Constructor

    ///

    ////////////////////////////////////////////////////////////

    Pluma();

    ////////////////////////////////////////////////////////////

    /// \brief Tell Pluma to accept a certain type of providers

    ///

    /// A Pluma object is able to accept multiple types of providers.

    /// When a plugin is loaded, it tries to register it's providers

    /// implementations. Those are only accepted by the host

    /// application if it's accepting providers of that kind.

    ///

    /// \tparam ProviderType type of provider.

    ///

    ////////////////////////////////////////////////////////////

    template<typename ProviderType>
    void acceptProviderType();

    ////////////////////////////////////////////////////////////

    /// \brief Get the stored providers of a certain type.

    ///

    /// Providers are added at the end of the \a providers vector.

    ///

    /// \tparam ProviderType type of provider to be returned.

    /// \param[out] providers Vector to fill with the existing

    /// providers.

    ///

    ////////////////////////////////////////////////////////////

    template<typename ProviderType>
    void getProviders(std::vector<ProviderType*>& providers);
};

#include <Pluma/Pluma.inl>


}


#endif // PLUMA_PLUMA_HPP



////////////////////////////////////////////////////////////

/// \class pluma::Pluma

///

/// Pluma is the main class of Pluma library. Allows hosting

/// applications to load/unload dlls in runtime (plugins), and

/// to get providers of shared interface objects.

///

/// Example:

/// \code

/// pluma::Pluma pluma;

/// // Tell it to accept providers of the type DeviceProvider

/// pluma.acceptProviderType<DeviceProvider>();

/// // Load some dll

/// pluma.load("plugins/standard_devices");

/// // Get device providers into a vector

/// std::vector<DeviceProvider*> providers;

/// pluma.getProviders(providers);

/// // create a Device from the first provider

/// if (!providers.empty()){

///     Device* myDevice = providers.first()->create();

///     // do something with myDevice

///     std::cout << device->getDescription() << std::endl;

///     // (...)

///     delete myDevice;

/// }

/// \endcode

///

/// It is also possible to add local providers, providers that

/// are defined directly on the host application. That can

/// be useful to provide and use default implementations of certain

/// interfaces, along with plugin implementations.

///

////////////////////////////////////////////////////////////