Apache Celix  2.4.0
Apache Celix is a framework for C, C++14 and C++17 to develop dynamic modular software applications using component and in-process service-oriented programming.
ProvidedService_Impl.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 inline celix::dm::BaseProvidedService::BaseProvidedService(celix_dm_component_t* _cmp, std::string _svcName, std::shared_ptr<void> _svc, bool _cppService) : cCmp{_cmp}, svcName{std::move(_svcName)}, svc{std::move(_svc)}, cppService{_cppService} {}
21 
22 inline const std::string &BaseProvidedService::getName() const {
23  return svcName;
24 }
25 
26 inline const std::string &BaseProvidedService::getVersion() const {
27  return svcVersion;
28 }
29 
30 inline bool BaseProvidedService::isCppService() const {
31  return cppService;
32 }
33 
34 inline void* BaseProvidedService::getService() const {
35  return svc.get();
36 }
37 
39  return properties;
40 }
41 
43  if (!provideAddedToCmp) {
44  //setup c properties
45  celix_properties_t *cProperties = celix_properties_create();
46  for (const auto &pair : properties) {
47  celix_properties_set(cProperties, pair.first.c_str(), pair.second.c_str());
48  }
49 
50  const char *cVersion = svcVersion.empty() ? nullptr : svcVersion.c_str();
51  celix_dmComponent_addInterface(cCmp, svcName.c_str(), cVersion, svc.get(), cProperties);
52  }
53  provideAddedToCmp = true;
54 }
55 
56 inline void BaseProvidedService::wait() const {
57  if (cCmp) {
59  auto* fw = celix_bundleContext_getFramework(ctx);
62  } else {
63  celix_bundleContext_log(ctx, CELIX_LOG_LEVEL_WARNING,
64  "BaseProvidedService::wait: Cannot wait for Celix event queue on the Celix event queue thread! "
65  "Use async DepMan API instead.");
66  }
67  }
68 }
69 
70 template<typename T, typename I>
72  svcVersion = std::move(v);
73  return *this;
74 }
75 
76 template<typename T, typename I>
78  properties = std::move(p);
79  return *this;
80 }
81 
82 template<typename T, typename I>
83 template<typename U>
84 ProvidedService<T, I> &ProvidedService<T, I>::addProperty(const std::string &key, const U &value) {
85  properties[key] = std::to_string(value);
86  return *this;
87 }
88 
89 template<typename T, typename I>
91  this->runBuild();
92  this->wait();
93  return *this;
94 }
95 
96 template<typename T, typename I>
98  this->runBuild();
99  return *this;
100 }
101 
102 template<typename T, typename I>
103 ProvidedService<T, I>::ProvidedService(celix_dm_component_t *_cmp, std::string svcName, std::shared_ptr<I> _svc, bool _cppService)
104  :BaseProvidedService(_cmp, svcName, std::static_pointer_cast<void>(std::move(_svc)), _cppService) {
105 
106 }
107 
108 template<typename T, typename I>
109 ProvidedService<T, I> &ProvidedService<T, I>::addProperty(const std::string &key, const char *value) {
110  properties[key] = std::string{value};
111  return *this;
112 }
113 
114 template<typename T, typename I>
115 ProvidedService<T, I> &ProvidedService<T, I>::addProperty(const std::string &key, const std::string &value) {
116  properties[key] = value;
117  return *this;
118 }
celix::dm::ProvidedService
Definition: ProvidedService.h:57
celix::dm::BaseProvidedService::cppService
bool cppService
Definition: ProvidedService.h:50
celix::dm::ProvidedService::setVersion
ProvidedService< T, I > & setVersion(std::string v)
Definition: ProvidedService_Impl.h:71
celix::dm::BaseProvidedService
Definition: ProvidedService.h:29
celix::dm::BaseProvidedService::svcName
std::string svcName
Definition: ProvidedService.h:48
celix::dm::ProvidedService::ProvidedService
ProvidedService(celix_dm_component_t *_cmp, std::string svcName, std::shared_ptr< I > svc, bool _cppService)
Definition: ProvidedService_Impl.h:103
celix::dm::BaseProvidedService::provideAddedToCmp
bool provideAddedToCmp
Definition: ProvidedService.h:53
celix::dm::BaseProvidedService::wait
void wait() const
Definition: ProvidedService_Impl.h:56
celix::dm::BaseProvidedService::svc
std::shared_ptr< void > svc
Definition: ProvidedService.h:49
celix_bundleContext_waitForEvents
CELIX_FRAMEWORK_EXPORT void celix_bundleContext_waitForEvents(celix_bundle_context_t *ctx)
Wait until all Celix event for this bundle are completed.
celix::dm::BaseProvidedService::getVersion
const std::string & getVersion() const
Definition: ProvidedService_Impl.h:26
celix::dm::BaseProvidedService::svcVersion
std::string svcVersion
Definition: ProvidedService.h:51
celix::dm::ProvidedService::buildAsync
ProvidedService< T, I > & buildAsync()
Definition: ProvidedService_Impl.h:97
celix::dm::ProvidedService::build
ProvidedService< T, I > & build()
Definition: ProvidedService_Impl.h:90
celix::dm::BaseProvidedService::properties
celix::dm::Properties properties
Definition: ProvidedService.h:52
celix_framework_isCurrentThreadTheEventLoop
CELIX_FRAMEWORK_EXPORT bool celix_framework_isCurrentThreadTheEventLoop(celix_framework_t *fw)
Returns whether the current thread is the Celix framework event loop thread.
celix_dmComponent_addInterface
CELIX_FRAMEWORK_EXPORT celix_status_t celix_dmComponent_addInterface(celix_dm_component_t *component, const char *serviceName, const char *serviceVersion, const void *service, celix_properties_t *properties)
celix_bundleContext_log
CELIX_FRAMEWORK_EXPORT void celix_bundleContext_log(const celix_bundle_context_t *ctx, celix_log_level_e level, const char *format,...) __attribute__((format(printf
Logs a message to Celix framework logger with the provided log level.
celix::dm::BaseProvidedService::getName
const std::string & getName() const
Definition: ProvidedService_Impl.h:22
celix::dm::BaseProvidedService::BaseProvidedService
BaseProvidedService(celix_dm_component_t *_cmp, std::string svcName, std::shared_ptr< void > svc, bool _cppService)
Definition: ProvidedService_Impl.h:20
celix::dm::BaseProvidedService::cCmp
celix_dm_component_t * cCmp
Definition: ProvidedService.h:47
celix::dm::BaseProvidedService::runBuild
void runBuild()
Definition: ProvidedService_Impl.h:42
celix_dmComponent_getBundleContext
CELIX_FRAMEWORK_EXPORT celix_bundle_context_t * celix_dmComponent_getBundleContext(celix_dm_component_t *component)
celix::dm::Properties
celix::Properties Properties
Definition: Properties.h:25
celix_bundleContext_getFramework
CELIX_FRAMEWORK_EXPORT celix_framework_t * celix_bundleContext_getFramework(const celix_bundle_context_t *ctx)
celix::dm::ProvidedService::addProperty
ProvidedService< T, I > & addProperty(const std::string &key, const U &value)
celix::dm::ProvidedService::setProperties
ProvidedService< T, I > & setProperties(celix::dm::Properties)
Definition: ProvidedService_Impl.h:77
celix::dm::BaseProvidedService::getProperties
const celix::dm::Properties & getProperties() const
Definition: ProvidedService_Impl.h:38
celix::dm::BaseProvidedService::isCppService
bool isCppService() const
Definition: ProvidedService_Impl.h:30
celix::dm::BaseProvidedService::getService
void * getService() const
Definition: ProvidedService_Impl.h:34