Repository: celix Updated Branches: refs/heads/feature/CELIX-426-cxx-api 0aee10d80 -> ed32d9f05
CELIX-426: Adds bundle activator + example for the C++ api Project: http://git-wip-us.apache.org/repos/asf/celix/repo Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/ed32d9f0 Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/ed32d9f0 Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/ed32d9f0 Branch: refs/heads/feature/CELIX-426-cxx-api Commit: ed32d9f058ca664b42f91178fdd65851dae3c042 Parents: 0aee10d Author: Pepijn Noltes <[email protected]> Authored: Mon May 7 12:03:22 2018 +0200 Committer: Pepijn Noltes <[email protected]> Committed: Mon May 7 12:03:22 2018 +0200 ---------------------------------------------------------------------- examples/celix-examples/CMakeLists.txt | 2 + .../bundle_example_cxx/CMakeLists.txt | 28 ++++++++++ .../bundle_example_cxx/src/BundleActivator.cc | 41 +++++++++++++++ framework/include/celix/BundleActivator.h | 49 +++++++++++++++++ framework/include/celix/BundleContext.h | 7 +-- .../include/celix/impl/BundleActivatorImpl.h | 55 ++++++++++++++++++++ .../include/celix/impl/BundleContextImpl.h | 12 +++++ 7 files changed, 191 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/celix/blob/ed32d9f0/examples/celix-examples/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/examples/celix-examples/CMakeLists.txt b/examples/celix-examples/CMakeLists.txt index 0937cc9..fd2ec1c 100644 --- a/examples/celix-examples/CMakeLists.txt +++ b/examples/celix-examples/CMakeLists.txt @@ -21,6 +21,8 @@ else () set(EXAMPLES true) #celix_subproject is only available in the celix project -> using examples dir in other project is also supported endif () if (EXAMPLES) + add_subdirectory(bundle_example_cxx) + add_subdirectory(hello_world) add_subdirectory(hello_world_test) http://git-wip-us.apache.org/repos/asf/celix/blob/ed32d9f0/examples/celix-examples/bundle_example_cxx/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/examples/celix-examples/bundle_example_cxx/CMakeLists.txt b/examples/celix-examples/bundle_example_cxx/CMakeLists.txt new file mode 100644 index 0000000..a49ca4f --- /dev/null +++ b/examples/celix-examples/bundle_example_cxx/CMakeLists.txt @@ -0,0 +1,28 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +add_celix_bundle(bundle_example_cxx + VERSION 1.0.0 + SOURCES src/BundleActivator.cc +) + +add_celix_container(bundle_example_cxx_container + BUNDLES + Celix::shell + Celix::shell_tui + bundle_example_cxx +) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/celix/blob/ed32d9f0/examples/celix-examples/bundle_example_cxx/src/BundleActivator.cc ---------------------------------------------------------------------- diff --git a/examples/celix-examples/bundle_example_cxx/src/BundleActivator.cc b/examples/celix-examples/bundle_example_cxx/src/BundleActivator.cc new file mode 100644 index 0000000..5687ff0 --- /dev/null +++ b/examples/celix-examples/bundle_example_cxx/src/BundleActivator.cc @@ -0,0 +1,41 @@ +/** + *Licensed to the Apache Software Foundation (ASF) under one + *or more contributor license agreements. See the NOTICE file + *distributed with this work for additional information + *regarding copyright ownership. The ASF licenses this file + *to you under the Apache License, Version 2.0 (the + *"License"); you may not use this file except in compliance + *with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + *Unless required by applicable law or agreed to in writing, + *software distributed under the License is distributed on an + *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + *specific language governing permissions and limitations + *under the License. + */ + +#include <iostream> + +#include "celix/BundleActivator.h" + +namespace { + class BundleActivator : public celix::IBundleActivator { + public: + BundleActivator(celix::BundleContext &_ctx) : ctx{_ctx} { + std::cout << "Hello world from C++ bundle with id " << ctx.getBundle().getBundleId() << std::endl; + } + virtual ~BundleActivator() { + std::cout << "Goodbye world from C++ bundle with id " << ctx.getBundle().getBundleId() << std::endl; + } + protected: + celix::BundleContext &ctx; + }; +} + +celix::IBundleActivator* celix::createBundleActivator(celix::BundleContext &ctx) { + return new BundleActivator{ctx}; +} + http://git-wip-us.apache.org/repos/asf/celix/blob/ed32d9f0/framework/include/celix/BundleActivator.h ---------------------------------------------------------------------- diff --git a/framework/include/celix/BundleActivator.h b/framework/include/celix/BundleActivator.h new file mode 100644 index 0000000..6809008 --- /dev/null +++ b/framework/include/celix/BundleActivator.h @@ -0,0 +1,49 @@ +/** + *Licensed to the Apache Software Foundation (ASF) under one + *or more contributor license agreements. See the NOTICE file + *distributed with this work for additional information + *regarding copyright ownership. The ASF licenses this file + *to you under the Apache License, Version 2.0 (the + *"License"); you may not use this file except in compliance + *with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + *Unless required by applicable law or agreed to in writing, + *software distributed under the License is distributed on an + *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + *specific language governing permissions and limitations + *under the License. + */ + +#ifndef CXX_CELIX_BUNDLEACTIVATOR_H +#define CXX_CELIX_BUNDLEACTIVATOR_H + +#include "celix/BundleContext.h" +#include "bundle_activator.h" + + +/** + * Note & Warning this is a header implementation of the C bundle activator. + * As result this header can only be included ones or the activator symbols will + * be duplicate (linking error). + */ + +namespace celix { + + class IBundleActivator { + public: + IBundleActivator(){}; + virtual ~IBundleActivator(){}; + }; + + /** + * The celix::createBundleActivator which needs to be implemented by a bundle. + */ + static celix::IBundleActivator* createBundleActivator(celix::BundleContext &ctx); +} + +#include "celix/impl/BundleActivatorImpl.h" + +#endif //CXX_CELIX_BUNDLEACTIVATOR_H http://git-wip-us.apache.org/repos/asf/celix/blob/ed32d9f0/framework/include/celix/BundleContext.h ---------------------------------------------------------------------- diff --git a/framework/include/celix/BundleContext.h b/framework/include/celix/BundleContext.h index 4169b0a..8fb540d 100644 --- a/framework/include/celix/BundleContext.h +++ b/framework/include/celix/BundleContext.h @@ -30,7 +30,9 @@ namespace celix { - class BundleContext; //forward declaration + //forward declarations + class Bundle; + class BundleContext; struct BundleRegistrationOptions { std::string id{}; @@ -191,8 +193,7 @@ namespace celix { */ virtual void stopTracker(long trackerId) noexcept = 0; - //TODO - //virtual Bundle& getBundle() const noexcept = 0; + virtual Bundle& getBundle() noexcept = 0; //TODO http://git-wip-us.apache.org/repos/asf/celix/blob/ed32d9f0/framework/include/celix/impl/BundleActivatorImpl.h ---------------------------------------------------------------------- diff --git a/framework/include/celix/impl/BundleActivatorImpl.h b/framework/include/celix/impl/BundleActivatorImpl.h new file mode 100644 index 0000000..2a0db53 --- /dev/null +++ b/framework/include/celix/impl/BundleActivatorImpl.h @@ -0,0 +1,55 @@ +/** + *Licensed to the Apache Software Foundation (ASF) under one + *or more contributor license agreements. See the NOTICE file + *distributed with this work for additional information + *regarding copyright ownership. The ASF licenses this file + *to you under the Apache License, Version 2.0 (the + *"License"); you may not use this file except in compliance + *with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + *Unless required by applicable law or agreed to in writing, + *software distributed under the License is distributed on an + *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + *specific language governing permissions and limitations + *under the License. + */ + + +namespace celix { + namespace impl { + struct ActivatorData { + celix::BundleContext *ctx; + celix::IBundleActivator *act; + }; + } +} + + +extern "C" celix_status_t bundleActivator_create(bundle_context_t *c_ctx, void **userData) { + auto *data = new celix::impl::ActivatorData; + data->ctx = new celix::impl::BundleContextImpl(c_ctx); + *userData = data; + return CELIX_SUCCESS; +} + +extern "C" celix_status_t bundleActivator_start(void *userData, bundle_context_t *) { + auto *data = static_cast<celix::impl::ActivatorData*>(userData); + data->act = celix::createBundleActivator(*data->ctx); + return CELIX_SUCCESS; +} + +extern "C" celix_status_t bundleActivator_stop(void *userData, bundle_context_t *) { + auto *data = static_cast<celix::impl::ActivatorData*>(userData); + delete data->act; + return CELIX_SUCCESS; +} + +extern "C" celix_status_t bundleActivator_destroy(void *userData, bundle_context_t*) { + auto *data = static_cast<celix::impl::ActivatorData*>(userData); + delete data->ctx; + delete data; + return CELIX_SUCCESS; +} http://git-wip-us.apache.org/repos/asf/celix/blob/ed32d9f0/framework/include/celix/impl/BundleContextImpl.h ---------------------------------------------------------------------- diff --git a/framework/include/celix/impl/BundleContextImpl.h b/framework/include/celix/impl/BundleContextImpl.h index 4bff16c..8bdd5c1 100644 --- a/framework/include/celix/impl/BundleContextImpl.h +++ b/framework/include/celix/impl/BundleContextImpl.h @@ -22,6 +22,7 @@ #include <mutex> #include <cstring> +#include <memory> #include "bundle_context.h" #include "service_tracker.h" @@ -150,6 +151,16 @@ namespace celix { return celix_bundleContext_useBundle(this->c_ctx, bundleId, (void*)(&use), c_use); } + Bundle& getBundle() noexcept override { + std::lock_guard<std::mutex> lock{this->mutex}; + if (this->cachedBundle.size() == 0) { + celix_bundle_t *c_bnd = nullptr; + bundleContext_getBundle(this->c_ctx, &c_bnd); + this->cachedBundle.emplace_back(c_bnd); + } + return this->cachedBundle[0]; + }; + protected: long registerServiceInternal(const std::string &serviceName, void *svc, const std::string &version, const std::string &lang, Properties props = {}) noexcept override { @@ -294,6 +305,7 @@ namespace celix { std::mutex mutex{}; std::map<long,std::unique_ptr<TrackEntry>> trackEntries{}; + std::vector<celix::impl::BundleImpl> cachedBundle{}; }; } }
