Celix  2.2.0
An implementation of the OSGi specification adapted to C and C++
celix_bundle_activator.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 #include <stdlib.h>
21 
22 #include "celix_bundle_context.h"
23 
24 #ifndef CELIX_BUNDLE_ACTIVATOR_H_
25 #define CELIX_BUNDLE_ACTIVATOR_H_
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
45 celix_status_t celix_bundleActivator_create(celix_bundle_context_t *ctx, void **userData);
46 
63 celix_status_t celix_bundleActivator_start(void *userData, celix_bundle_context_t *ctx);
64 
82 celix_status_t celix_bundleActivator_stop(void *userData, celix_bundle_context_t *ctx);
83 
99 celix_status_t celix_bundleActivator_destroy(void *userData, celix_bundle_context_t* ctx);
100 
101 
115 #define CELIX_GEN_BUNDLE_ACTIVATOR(actType, actStart, actStop) \
116 celix_status_t celix_bundleActivator_create(celix_bundle_context_t *ctx __attribute__((unused)), void **userData) { \
117  celix_status_t status = CELIX_SUCCESS; \
118  actType *data = (actType*)calloc(1, sizeof(*data)); \
119  if (data != NULL) { \
120  *userData = data; \
121  } else { \
122  status = CELIX_ENOMEM; \
123  } \
124  return status; \
125 } \
126  \
127 celix_status_t celix_bundleActivator_start(void *userData, celix_bundle_context_t *ctx) { \
128  return actStart((actType*)userData, ctx); \
129 } \
130  \
131 celix_status_t celix_bundleActivator_stop(void *userData, celix_bundle_context_t *ctx) { \
132  return actStop((actType*)userData, ctx); \
133 } \
134  \
135 celix_status_t celix_bundleActivator_destroy(void *userData, celix_bundle_context_t *ctx __attribute__((unused))) { \
136  free(userData); \
137  return CELIX_SUCCESS; \
138 }
139 
140 #ifdef __cplusplus
141 }
142 
143 
155 #define CELIX_GEN_CXX_BUNDLE_ACTIVATOR(actType) \
156  \
157 namespace /*anon*/ { \
158 struct BundleActivatorData { \
159  std::shared_ptr<DependencyManager> mng{}; \
160  std::unique_ptr<actType> activator{}; \
161 }; \
162 } \
163  \
164 extern "C" celix_status_t bundleActivator_create(celix_bundle_context_t *context, void** userData) { \
165  int status = CELIX_SUCCESS; \
166  \
167  BundleActivatorData* data = nullptr; \
168  data = new BundleActivatorData{}; \
169  if (data != nullptr) { \
170  data->mng = std::shared_ptr<celix::dm::DependencyManager>{new celix::dm::DependencyManager{context}}; \
171  } \
172  \
173  if (data == nullptr || data->mng == nullptr) { \
174  status = CELIX_ENOMEM; \
175  if (data != nullptr) { \
176  delete data; \
177  } \
178  *userData = nullptr; \
179  } else { \
180  *userData = data; \
181  } \
182  return status; \
183 } \
184  \
185 extern "C" celix_status_t bundleActivator_start(void *userData, celix_bundle_context_t *) { \
186  auto* data = static_cast<BundleActivatorData*>(userData); \
187  if (data != nullptr) { \
188  data->activator = std::unique_ptr<actType>{new actType{data->mng}}; \
189  data->mng->start(); \
190  } \
191  return CELIX_SUCCESS; \
192 } \
193  \
194 extern "C" celix_status_t bundleActivator_stop(void *userData, celix_bundle_context_t*) { \
195  auto* data = static_cast<BundleActivatorData*>(userData); \
196  if (data != nullptr) { \
197  data->mng->stop(); \
198  data->activator = nullptr; \
199  data->mng = nullptr; \
200  } \
201  return CELIX_SUCCESS; \
202 } \
203  \
204 extern "C" celix_status_t bundleActivator_destroy(void *userData, celix_bundle_context_t*) { \
205  auto* data = static_cast<BundleActivatorData*>(userData); \
206  if (data != nullptr) { \
207  delete data; \
208  } \
209  return CELIX_SUCCESS; \
210 } \
211 
212 
213 #endif
214 
215 #endif /* CELIX_BUNDLE_ACTIVATOR_H_ */
celix_status_t celix_bundleActivator_destroy(void *userData, celix_bundle_context_t *ctx)
celix_status_t celix_bundleActivator_create(celix_bundle_context_t *ctx, void **userData)
celix_status_t celix_bundleActivator_stop(void *userData, celix_bundle_context_t *ctx)
celix_status_t celix_bundleActivator_start(void *userData, celix_bundle_context_t *ctx)