summaryrefslogtreecommitdiff
path: root/org.genivi.commonapi.core/src/org/genivi/commonapi/core/generator/FInterfaceProxyGenerator.xtend
blob: 041e64ee3883504bd4acbc1d13aa0413585dc339 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.genivi.commonapi.core.generator

import java.util.ArrayList
import java.util.HashSet
import javax.inject.Inject
import org.eclipse.core.resources.IResource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.franca.core.franca.FAttribute
import org.franca.core.franca.FInterface
import org.franca.core.franca.FMethod
import org.genivi.commonapi.core.deployment.PropertyAccessor
import org.genivi.commonapi.core.preferences.PreferenceConstants

class FInterfaceProxyGenerator {
    @Inject private extension FTypeGenerator
    @Inject private extension FrancaGeneratorExtensions

    def generateProxy(FInterface fInterface, IFileSystemAccess fileSystemAccess, PropertyAccessor deploymentAccessor, IResource modelid) {
        fileSystemAccess.generateFile(fInterface.proxyBaseHeaderPath, PreferenceConstants.P_OUTPUT_PROXIES, fInterface.generateProxyBaseHeader(deploymentAccessor, modelid))
        fileSystemAccess.generateFile(fInterface.proxyHeaderPath, PreferenceConstants.P_OUTPUT_PROXIES, fInterface.generateProxyHeader(modelid))
    }

    def private generateProxyBaseHeader(FInterface fInterface, PropertyAccessor deploymentAccessor, IResource modelid) '''
        «generateCommonApiLicenseHeader(fInterface, modelid)»
        «FTypeGenerator::generateComments(fInterface, false)»
        #ifndef «fInterface.defineName»_PROXY_BASE_HPP_
        #define «fInterface.defineName»_PROXY_BASE_HPP_

        #include <«fInterface.headerPath»>
        «IF fInterface.base != null»
            #include <«fInterface.base.proxyBaseHeaderPath»>
        «ENDIF»

        «val generatedHeaders = new HashSet<String>»
        «val libraryHeaders = new HashSet<String>»
        «fInterface.generateRequiredTypeIncludes(generatedHeaders, libraryHeaders)»

        «FOR requiredHeaderFile : generatedHeaders.sort»
            #include <«requiredHeaderFile»>
        «ENDFOR»

        #if !defined (COMMONAPI_INTERNAL_COMPILATION)
        #define COMMONAPI_INTERNAL_COMPILATION
        #endif

        «FOR requiredHeaderFile : libraryHeaders.sort»
            #include <«requiredHeaderFile»>
        «ENDFOR»

        «IF !fInterface.managedInterfaces.empty»
            #include <CommonAPI/ProxyManager.hpp>
        «ENDIF»
        «IF fInterface.hasAttributes»
            #include <CommonAPI/Attribute.hpp>
        «ENDIF»
        «IF fInterface.hasBroadcasts»
            #include <CommonAPI/Event.hpp>
            «IF fInterface.hasSelectiveBroadcasts»
                #include <CommonAPI/SelectiveEvent.hpp>
            «ENDIF»
        «ENDIF»
        #include <CommonAPI/Proxy.hpp>
        «IF !fInterface.methods.empty»
            #include <functional>
            #include <future>
        «ENDIF»

        #undef COMMONAPI_INTERNAL_COMPILATION

        «fInterface.generateVersionNamespaceBegin»
        «fInterface.model.generateNamespaceBeginDeclaration»

        class «fInterface.proxyBaseClassName»
            : virtual public «IF fInterface.base != null»«fInterface.base.getTypeCollectionName(fInterface)»ProxyBase«ELSE»CommonAPI::Proxy«ENDIF» {
        public:
            «FOR attribute : fInterface.attributes»
                typedef CommonAPI::«attribute.commonApiBaseClassname»<«attribute.getTypeName(fInterface, true)»> «attribute.className»;
            «ENDFOR»
            «FOR broadcast : fInterface.broadcasts»
                «IF broadcast.isSelective»
                    typedef CommonAPI::SelectiveEvent<«broadcast.outArgs.map[getTypeName(fInterface, true)].join(', ')»> «broadcast.className»;
                «ELSE»
                    typedef CommonAPI::Event<
                        «broadcast.outArgs.map[getTypeName(fInterface, true)].join(', ')»
                    > «broadcast.className»;
                «ENDIF»
            «ENDFOR»

            «fInterface.generateAsyncCallbackTypedefs»

            «FOR attribute : fInterface.attributes»
                «FTypeGenerator::generateComments(attribute, false)»
                virtual «attribute.generateGetMethodDefinition» = 0;
            «ENDFOR»

            «FOR broadcast : fInterface.broadcasts»
                «FTypeGenerator::generateComments(broadcast, false)»
                virtual «broadcast.generateGetMethodDefinition» = 0;
            «ENDFOR»

            «FOR method : fInterface.methods»
                «FTypeGenerator::generateComments(method, false)»
                «IF method.isFireAndForget»
                    /**
                     * @invariant Fire And Forget
                     */
                «ENDIF»
                virtual «method.generateDefinition(true)» = 0;
                «IF !method.isFireAndForget»
                    virtual «method.generateAsyncDefinition(true)» = 0;
                «ENDIF»
            «ENDFOR»
            «FOR managed : fInterface.managedInterfaces»
                virtual CommonAPI::ProxyManager& «managed.proxyManagerGetterName»() = 0;
            «ENDFOR»
        };

        «fInterface.model.generateNamespaceEndDeclaration»
        «fInterface.generateVersionNamespaceEnd»
        
        #endif // «fInterface.defineName»_PROXY_BASE_HPP_
    '''

    def private generateProxyHeader(FInterface fInterface, IResource modelid) '''
        «generateCommonApiLicenseHeader(fInterface, modelid)»
        «FTypeGenerator::generateComments(fInterface, false)»
        #ifndef «fInterface.defineName»_PROXY_HPP_
        #define «fInterface.defineName»_PROXY_HPP_

        #include <«fInterface.proxyBaseHeaderPath»>

        «IF fInterface.base != null»
            #include "«fInterface.base.proxyHeaderPath»"
        «ENDIF»

        #if !defined (COMMONAPI_INTERNAL_COMPILATION)
        #define COMMONAPI_INTERNAL_COMPILATION
        #endif

        «IF fInterface.hasAttributes»
            #include <CommonAPI/AttributeExtension.hpp>
            #include <CommonAPI/Factory.hpp>
        «ENDIF»

        #undef COMMONAPI_INTERNAL_COMPILATION

        «fInterface.generateVersionNamespaceBegin»
        «fInterface.model.generateNamespaceBeginDeclaration»

        template <typename ... _AttributeExtensions>
        class «fInterface.proxyClassName»
            : virtual public «fInterface.elementName», 
              virtual public «fInterface.proxyBaseClassName»,«IF fInterface.base != null»
              virtual public «fInterface.base.getTypeCollectionName(fInterface)»Proxy<_AttributeExtensions...>,«ENDIF»
              public _AttributeExtensions... {
        public:
            «fInterface.proxyClassName»(std::shared_ptr<CommonAPI::Proxy> delegate);
            ~«fInterface.proxyClassName»();

            typedef «fInterface.getRelativeNameReference(fInterface)» InterfaceType;

            «IF fInterface.base != null»
                inline static const char* getInterface() {
                    return(«fInterface.elementName»::getInterface());
                }
            «ENDIF»

            «FOR attribute : fInterface.attributes»
                «FTypeGenerator::generateComments(attribute, false)»
                /**
                 * Returns the wrapper class that provides access to the attribute «attribute.elementName».
                 */
                virtual «attribute.generateGetMethodDefinition» {
                    return delegate_->get«attribute.className»();
                }
            «ENDFOR»

            «FOR broadcast : fInterface.broadcasts»
                «FTypeGenerator::generateComments(broadcast, false)»
                /**
                 * Returns the wrapper class that provides access to the broadcast «broadcast.elementName».
                 */
                virtual «broadcast.generateGetMethodDefinition» {
                    return delegate_->get«broadcast.className»();
                }
            «ENDFOR»

            «FOR method : fInterface.methods»
                /**
                «FTypeGenerator::generateComments(method, true)»
                 * Calls «method.elementName» with «IF method.isFireAndForget»Fire&Forget«ELSE»synchronous«ENDIF» semantics.
                 * 
                «IF !method.inArgs.empty»* All const parameters are input parameters to this method.«ENDIF»
                «IF !method.outArgs.empty»* All non-const parameters will be filled with the returned values.«ENDIF»
                 * The CallStatus will be filled when the method returns and indicate either
                 * "SUCCESS" or which type of error has occurred. In case of an error, ONLY the CallStatus
                 * will be set.
                 */
                virtual «method.generateDefinition(true)»;
                «IF !method.isFireAndForget»
                    /**
                     * Calls «method.elementName» with asynchronous semantics.
                     * 
                     * The provided callback will be called when the reply to this call arrives or
                     * an error occurs during the call. The CallStatus will indicate either "SUCCESS"
                     * or which type of error has occurred. In case of any error, ONLY the CallStatus
                     * will have a defined value.
                     * The std::future returned by this method will be fulfilled at arrival of the reply.
                     * It will provide the same value for CallStatus as will be handed to the callback.
                     */
                    virtual «method.generateAsyncDefinition(true)»;
                «ENDIF»
            «ENDFOR»

            «FOR managed : fInterface.managedInterfaces»
                virtual CommonAPI::ProxyManager& «managed.proxyManagerGetterName»();
            «ENDFOR»

            /**
             * Returns the CommonAPI address of the remote partner this proxy communicates with.
             */
            virtual const CommonAPI::Address &getAddress() const;

            /**
             * Returns true if the remote partner for this proxy is currently known to be available.
             */
            virtual bool isAvailable() const;

            /**
             * Returns true if the remote partner for this proxy is available.
             */
            virtual bool isAvailableBlocking() const;

            /**
             * Returns the wrapper class that is used to (de-)register for notifications about
             * the availability of the remote partner of this proxy.
             */
            virtual CommonAPI::ProxyStatusEvent& getProxyStatusEvent();

            /**
             * Returns the wrapper class that is used to access version information of the remote
             * partner of this proxy.
             */
            virtual CommonAPI::InterfaceVersionAttribute& getInterfaceVersionAttribute();

         private:
            std::shared_ptr<«fInterface.proxyBaseClassName»> delegate_;
        };

        typedef «fInterface.proxyClassName»<> «fInterface.proxyDefaultClassName»;

        «IF fInterface.hasAttributes»
            namespace «fInterface.extensionsSubnamespace» {
                «FOR attribute : fInterface.attributes»
                    «attribute.generateExtension(fInterface)»

                «ENDFOR»
            } // namespace «fInterface.extensionsSubnamespace»
        «ENDIF»

        //
        // «fInterface.proxyClassName» Implementation
        //
        template <typename ... _AttributeExtensions>
        «fInterface.proxyClassName»<_AttributeExtensions...>::«fInterface.proxyClassName»(std::shared_ptr<CommonAPI::Proxy> delegate):
                «IF fInterface.base != null»
                «fInterface.base.getFullName()»Proxy<_AttributeExtensions...>(delegate),
                «ENDIF»
                _AttributeExtensions(*(std::dynamic_pointer_cast<«fInterface.proxyBaseClassName»>(delegate)))...,
                delegate_(std::dynamic_pointer_cast<«fInterface.proxyBaseClassName»>(delegate)) {
        }

        template <typename ... _AttributeExtensions>
        «fInterface.proxyClassName»<_AttributeExtensions...>::~«fInterface.proxyClassName»() {
        }

        «FOR method : fInterface.methods»
            «FTypeGenerator::generateComments(method, false)»
            template <typename ... _AttributeExtensions>
            «method.generateDefinitionWithin(fInterface.proxyClassName + '<_AttributeExtensions...>', false)» {
                delegate_->«method.elementName»(«method.generateSyncVariableList»);
            }
            «IF !method.isFireAndForget»

                template <typename ... _AttributeExtensions>
                «method.generateAsyncDefinitionWithin(fInterface.proxyClassName + '<_AttributeExtensions...>', false)» {
                    return delegate_->«method.elementName»Async(«method.generateASyncVariableList»);
                }
            «ENDIF»
        «ENDFOR»

        template <typename ... _AttributeExtensions>
        const CommonAPI::Address &«fInterface.proxyClassName»<_AttributeExtensions...>::getAddress() const {
            return delegate_->getAddress();
        }

        template <typename ... _AttributeExtensions>
        bool «fInterface.proxyClassName»<_AttributeExtensions...>::isAvailable() const {
            return delegate_->isAvailable();
        }

        template <typename ... _AttributeExtensions>
        bool «fInterface.proxyClassName»<_AttributeExtensions...>::isAvailableBlocking() const {
            return delegate_->isAvailableBlocking();
        }

        template <typename ... _AttributeExtensions>
        CommonAPI::ProxyStatusEvent& «fInterface.proxyClassName»<_AttributeExtensions...>::getProxyStatusEvent() {
            return delegate_->getProxyStatusEvent();
        }

        template <typename ... _AttributeExtensions>
        CommonAPI::InterfaceVersionAttribute& «fInterface.proxyClassName»<_AttributeExtensions...>::getInterfaceVersionAttribute() {
            return delegate_->getInterfaceVersionAttribute();
        }

        «FOR managed : fInterface.managedInterfaces»
            template <typename ... _AttributeExtensions>
            CommonAPI::ProxyManager& «fInterface.proxyClassName»<_AttributeExtensions...>::«managed.proxyManagerGetterName»() {
                return delegate_->«managed.proxyManagerGetterName»();
            }
        «ENDFOR»

        «fInterface.model.generateNamespaceEndDeclaration»
        «fInterface.generateVersionNamespaceEnd»

        «IF fInterface.hasAttributes»
        namespace CommonAPI {
        template<template<typename > class _AttributeExtension>
        struct DefaultAttributeProxyHelper<«fInterface.versionPrefix»«fInterface.model.generateCppNamespace»«fInterface.proxyClassName»,
            _AttributeExtension> {
            typedef typename «fInterface.versionPrefix»«fInterface.model.generateCppNamespace»«fInterface.proxyClassName»<
                    «fInterface.attributes.map[fInterface.versionPrefix + fInterface.model.generateCppNamespace + fInterface.extensionsSubnamespace + '::' + extensionClassName + "<_AttributeExtension>"].join(", \n")»
            > class_t;
        };
        }
        «ENDIF»

        #endif // «fInterface.defineName»_PROXY_HPP_
    '''

    def private generateExtension(FAttribute fAttribute, FInterface fInterface) '''
        «FTypeGenerator::generateComments(fAttribute, false)»
        template <template <typename > class _ExtensionType>
        class «fAttribute.extensionClassName» {
         public:
            typedef _ExtensionType<«fInterface.proxyBaseClassName»::«fAttribute.className»> extension_type;

            static_assert(std::is_base_of<typename CommonAPI::AttributeExtension<«fInterface.proxyBaseClassName»::«fAttribute.className»>, extension_type>::value,
                          "Not CommonAPI Attribute Extension!");
        #ifdef WIN32
            «fAttribute.extensionClassName»() {}
        #endif

            «fAttribute.extensionClassName»(«fInterface.proxyBaseClassName»& proxy): attributeExtension_(proxy.get«fAttribute.className»()) {
            }

            inline extension_type& get«fAttribute.extensionClassName»() {
                return attributeExtension_;
            }

         private:
            extension_type attributeExtension_;
        };
    '''

    def private generateAsyncCallbackTypedefs(FInterface fInterface) '''
        «var callbackDefinitions = new HashSet<String>()»
        «FOR fMethod : fInterface.methods»
            «IF !fMethod.isFireAndForget»
                «val definitionSignature = fMethod.generateASyncTypedefSignature + fMethod.asyncCallbackClassName»
                «IF !callbackDefinitions.contains(definitionSignature)»
                    «IF fMethod.needsMangling»
                        /*
                         * This method has overloaded output parameters!
                         *
                         * A hash value was generated out of the names and types of these output
                         * parameters to distinguish the callback typedefs. Be careful in changing the
                         * type definitions in Franca if you actively use these typedefs in your code.
                         */
                    «ENDIF»
                    typedef std::function<void(«fMethod.generateASyncTypedefSignature»)> «fMethod.asyncCallbackClassName»;
                    «{callbackDefinitions.add(definitionSignature);""}»
                «ENDIF»
            «ENDIF»
        «ENDFOR»
    '''

    def private getCommonApiBaseClassname(FAttribute fAttribute) {
        var baseClassname = 'Attribute'

        if (fAttribute.isReadonly)
            baseClassname = 'Readonly' + baseClassname

        if (fAttribute.isObservable)
            baseClassname = 'Observable' + baseClassname

        return baseClassname
    }

    def private getExtensionsSubnamespace(FInterface fInterface) {
        fInterface.elementName + 'Extensions'
    }

    def private getProxyClassName(FInterface fInterface) {
        fInterface.elementName + 'Proxy'
    }

    def private getProxyDefaultClassName(FInterface fInterface) {
        fInterface.proxyClassName + 'Default'
    }

    def private getExtensionClassName(FAttribute fAttribute) {
        return fAttribute.className + 'Extension'
    }

    def private generateSyncVariableList(FMethod fMethod) {
        val syncVariableList = new ArrayList(fMethod.inArgs.map['_' + elementName])

        syncVariableList.add('_internalCallStatus')

        if (fMethod.hasError)
            syncVariableList.add('_error')

        syncVariableList.addAll(fMethod.outArgs.map['_' + elementName])
        
        if (!fMethod.isFireAndForget)
            return syncVariableList.join(', ') + ", _info"
        else
            return syncVariableList.join(', ')
    }

    def private generateASyncVariableList(FMethod fMethod) {
        var asyncVariableList = new ArrayList(fMethod.inArgs.map['_' + elementName])
        asyncVariableList.add('_callback')
        return asyncVariableList.join(', ') + ", _info"
    }
}