timestamp: fix currentTime() problem
[profile/ivi/automotive-message-broker.git] / lib / ambplugin.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef _AMBPLUGIN_H_
20 #define _AMBPLUGIN_H_
21
22 #include <abstractsource.h>
23 #include "ambpluginimpl.h"
24 #include <string>
25
26 /*!
27  * \file ambplugin.h
28  * \brief Contains common code used in plugins for Automotive message broker(AMB).
29  */
30
31 /*!
32  * AmbPlugin class contains common code used in plugins for Automotive message broker(AMB).
33  * For the AMB library API please visit \ref libamb</a>.
34  *
35  * \class AmbPlugin
36  *
37  * Example of the minimal code to write a new plugin using AmbPlugin:
38  *
39  * \code
40  * #include "ambpluginimpl.h"
41  *
42  * class MyPlugin: public AmbPluginImpl {
43  *
44  * public:
45  *     MyPlugin(AbstractRoutingEngine* re, const std::map<std::string, std::string>& config, AbstractSource& parent);
46  *     ~MyPlugin();
47  *
48  *     const std::string uuid() const { return "plugin_uuid"; }
49  * };
50  *
51  * // library exported function for plugin loader
52  * extern "C" void create(AbstractRoutingEngine* routingengine, std::map<std::string, std::string> config)
53  * {
54  *     new AmbPlugin<MyPlugin>(routingengine, config);
55  * }
56  * \endcode
57  * \addtogroup libamb
58  *  @{
59  */
60
61 template<class T>
62 class AmbPlugin : public AbstractSource {
63
64         /*!
65          * Compile time check
66          * \internal
67          */
68         static_assert(std::is_base_of<AmbPluginImpl, T>::value, "AmbPluginImpl has to be a base of T");
69
70 public:
71         /*!
72          * \param re AbstractRoutingEngine
73          * \param config Map of the configuration string values loaded on startup from AMB configuration file
74          */
75         AmbPlugin(AbstractRoutingEngine* re, const std::map<std::string, std::string>& config);
76         virtual ~AmbPlugin() {}
77
78         // from AbstractSource:
79 public:
80
81         /*!
82          * \brief getPropertyAsync is called when a sink requests the value for given property.
83          * This is only called if the source supports the Get operation.
84          * \param reply the reply variable.  @see AsyncPropertyReply
85          */
86         virtual void getPropertyAsync(AsyncPropertyReply *reply);
87
88         /*!
89          * \brief getRangePropertyAsync is called when a sink requests a series of values for a given
90          * property within a specified time or sequencial range.  This will only be called if the source
91          * support the Ranged Operation.
92          * \param reply is the reply variable.  @see AsyncRangePropertyReply
93          */
94         virtual void getRangePropertyAsync(AsyncRangePropertyReply *reply);
95
96         /*!
97          * \brief setProperty is called when a sink requests to set a value for a given property.
98          * This is only called if the source supports the Set Operation.
99          * \param request the requested property to set.
100          * \return returns a pointer to the new value for the property.
101          */
102         virtual AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request);
103
104         /*!
105          * \brief subscribeToPropertyChanges is called when a sink requests a subscription. Source plugins
106          * can keep track of subscriptions and may wish to sleep if there are no subscriptions.
107          * \param property the property that is being subscribed.
108          */
109         virtual void subscribeToPropertyChanges(VehicleProperty::Property property);
110
111         /*!
112          * \brief unsubscribeToPropertyChanges is called when a sink requests to unsubscribe from a given property's changes.
113          * \param property the property to unsubscribe to
114          */
115         virtual void unsubscribeToPropertyChanges(VehicleProperty::Property property);
116
117         /*!
118          * \brief supported is called by the routingEngine to understand what properties this source supports
119          * \return returns a list of supported properties. If the the supported properties changed, the source should call AbstractRoutingEngine::setSupported.
120          */
121         virtual PropertyList supported();
122
123         /*!
124          * \brief supportedOperations
125          * \return returns the supported operations.
126          */
127         virtual int supportedOperations();
128
129         /*!
130          * \brief getPropertyInfo used to return specific information about a property.
131          * The source should override this otherwise a PropertyInfo::invalid() will be returned for the property.
132          * \param property the property to get info for.
133          * \return a PropertyInfo object.
134          */
135         virtual PropertyInfo getPropertyInfo(const VehicleProperty::Property & property);
136
137         // from AbstractSink
138 public:
139
140         /*! uuid() is a unique identifier
141           * \return A guid-style unique identifier
142           */
143         virtual const string uuid();
144
145         /*! propertyChanged is called when a subscribed to property changes.
146           * \param value value of the property that changed. this is a temporary pointer that will be destroyed.
147           * Do not destroy it.  If you need to store the value use value.anyValue(), value.value<T>() or
148           * value->copy() to copy.
149           */
150         virtual void propertyChanged(AbstractPropertyType* value);
151
152         /*! supportedChanged() is called when the supported properties changes
153          * \param supportedProperties the new list of supported properties.
154          */
155         virtual void supportedChanged(const PropertyList & supportedProperties);
156
157
158         // AmbPlugin's own methods
159 public:
160
161         /*!
162          * Second phase of the plugin initialization.
163          * \fn init
164          */
165         void init();
166
167 private:
168
169         /**
170         * \brief AmbPlugin class private implementation
171         */
172         std::unique_ptr<T> d;
173 };
174
175 //----------------------------------------------------------------------------
176 // Function implementations
177 //----------------------------------------------------------------------------
178
179 //----------------------------------------------------------------------------
180 // AmbPlugin
181 //----------------------------------------------------------------------------
182
183 template<typename T>
184 AmbPlugin<T>::AmbPlugin(AbstractRoutingEngine* re, const std::map<std::string, std::string>& config) :
185         AbstractSource(re, config),
186         d(new T(re, config, *this))
187 {
188
189 }
190
191 template<typename T>
192 void AmbPlugin<T>::getPropertyAsync(AsyncPropertyReply *reply)
193 {
194         if(d)
195                 d->getPropertyAsync(reply);
196 }
197
198 template<typename T>
199 void AmbPlugin<T>::getRangePropertyAsync(AsyncRangePropertyReply *reply)
200 {
201         if(d)
202                 d->getRangePropertyAsync(reply);
203 }
204
205 template<typename T>
206 AsyncPropertyReply* AmbPlugin<T>::setProperty(AsyncSetPropertyRequest request)
207 {
208         if(d)
209                 return d->setProperty(request);
210         return nullptr;
211 }
212
213 template<typename T>
214 void AmbPlugin<T>::subscribeToPropertyChanges(VehicleProperty::Property property)
215 {
216         if(d)
217                 d->subscribeToPropertyChanges(property);
218 }
219
220 template<typename T>
221 void AmbPlugin<T>::unsubscribeToPropertyChanges(VehicleProperty::Property property)
222 {
223         if(d)
224                 return d->unsubscribeToPropertyChanges(property);
225 }
226
227 template<typename T>
228 PropertyList AmbPlugin<T>::supported()
229 {
230         return d ? d->supported() : PropertyList();
231 }
232
233 template<typename T>
234 int AmbPlugin<T>::supportedOperations()
235 {
236         return d ? d->supportedOperations() : 0;
237 }
238
239 template<typename T>
240 PropertyInfo AmbPlugin<T>::getPropertyInfo(const VehicleProperty::Property &property)
241 {
242         return d ? d->getPropertyInfo(property) : PropertyInfo::invalid();
243 }
244
245 template<typename T>
246 const string AmbPlugin<T>::uuid()
247 {
248         return d ? d->uuid() : "";
249 }
250
251 template<typename T>
252 void AmbPlugin<T>::propertyChanged(AbstractPropertyType* value)
253 {
254         if(d)
255                 d->propertyChanged(value);
256 }
257
258 template<typename T>
259 void AmbPlugin<T>::supportedChanged(const PropertyList &supportedProperties)
260 {
261         if(d)
262                 d->supportedChanged(supportedProperties);
263 }
264
265 template<typename T>
266 void AmbPlugin<T>::init()
267 {
268         if(d)
269                 d->init();
270 }
271
272 #endif // _AMBPLUGIN_H_
273
274 /** @} */