You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2017/03/07 19:16:37 UTC

celix git commit: CELIX-386: Fixes an issue with the C++ dep man when used out side of the celix project. Added additional lifecycle callback options for the dm component

Repository: celix
Updated Branches:
  refs/heads/develop 83c6de268 -> 8cecba96f


CELIX-386: Fixes an issue with the C++ dep man when used out side of the celix project. Added additional lifecycle callback options for the dm component


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/8cecba96
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/8cecba96
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/8cecba96

Branch: refs/heads/develop
Commit: 8cecba96f27da02607ad91866936a32f6d2ec155
Parents: 83c6de2
Author: Pepijn Noltes <pe...@gmail.com>
Authored: Tue Mar 7 20:15:26 2017 +0100
Committer: Pepijn Noltes <pe...@gmail.com>
Committed: Tue Mar 7 20:16:24 2017 +0100

----------------------------------------------------------------------
 dependency_manager_cxx/CMakeLists.txt           |  1 -
 .../include/celix/dm/Component.h                | 46 ++++++++++++--
 .../include/celix/dm/Component_Impl.h           | 65 +++++++++++++++++++-
 .../include/celix/dm/DependencyManager.h        | 24 ++++----
 .../include/celix/dm/DmActivator.h              | 43 -------------
 dependency_manager_cxx/src/Component.cc         | 43 -------------
 dependency_manager_cxx/src/DependencyManager.cc |  5 +-
 7 files changed, 117 insertions(+), 110 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/8cecba96/dependency_manager_cxx/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/dependency_manager_cxx/CMakeLists.txt b/dependency_manager_cxx/CMakeLists.txt
index 6c9869e..fd3cfe6 100644
--- a/dependency_manager_cxx/CMakeLists.txt
+++ b/dependency_manager_cxx/CMakeLists.txt
@@ -47,7 +47,6 @@ if (DEPENDENCY_MANAGER_CXX)
         ${DM_DIR}/private/src/dm_dependency_manager_impl.c
 
         src/DependencyManager.cc
-        src/Component.cc
         src/ServiceDependency.cc
         src/dm_activator_base_cxx.cc
     )

http://git-wip-us.apache.org/repos/asf/celix/blob/8cecba96/dependency_manager_cxx/include/celix/dm/Component.h
----------------------------------------------------------------------
diff --git a/dependency_manager_cxx/include/celix/dm/Component.h b/dependency_manager_cxx/include/celix/dm/Component.h
index fd07a42..6c12a99 100644
--- a/dependency_manager_cxx/include/celix/dm/Component.h
+++ b/dependency_manager_cxx/include/celix/dm/Component.h
@@ -32,21 +32,24 @@ namespace celix { namespace dm {
     class BaseComponent {
     private:
         bundle_context_pt context {nullptr};
-        std::string name {};
         dm_component_pt cCmp {nullptr};
     public:
-        BaseComponent(const bundle_context_pt context, std::string name);
-        virtual ~BaseComponent();
+        BaseComponent(const bundle_context_pt con, std::string name) : context{con}, cCmp{nullptr} {
+            component_create(this->context, name.c_str(), &this->cCmp);
+            component_setImplementation(this->cCmp, this);
+        }
+
+        virtual ~BaseComponent() {}
 
         /**
          * Returns the C DM Component
          */
-        const dm_component_pt cComponent() const;
+        dm_component_pt cComponent() const { return this->cCmp; }
 
         /**
          * Returns the C bundle context
          */
-        const bundle_context_pt bundleContext() const;
+        bundle_context_pt bundleContext() const { return this->context; }
     };
         
 
@@ -63,16 +66,29 @@ namespace celix { namespace dm {
         void (T::*startFp)() = {};
         void (T::*stopFp)() = {};
         void (T::*deinitFp)() = {};
+
+        int (T::*initFpNoExc)() = {};
+        int (T::*startFpNoExc)() = {};
+        int (T::*stopFpNoExc)() = {};
+        int (T::*deinitFpNoExc)() = {};
     public:
         Component(const bundle_context_pt context, std::string name);
         virtual ~Component();
 
         /**
+         * Creates a Component using the provided bundle context
+         * and component name.
+         * Will use new(nothrow) if exceptions are disabled.
+         * @return newly created DM Component or nullptr
+         */
+        static Component<T>* create(bundle_context_pt, std::string name);
+
+        /**
          * Creates a Component using the provided bundle context.
          * Will use new(nothrow) if exceptions are disabled.
          * @return newly created DM Component or nullptr
          */
-        static Component<T>* create(bundle_context_pt, std::string name = std::string{});
+        static Component<T>* create(bundle_context_pt);
 
         /**
          * Wether the component is valid. Invalid component can occurs when no new components can be created and
@@ -190,6 +206,24 @@ namespace celix { namespace dm {
             void (T::*stop)(),
             void (T::*deinit)()
         );
+
+        /**
+         * Set the callback for the component life cycle control
+         * with a int return to indicate an error.
+         *
+         * @param init The init callback.
+         * @param start The start callback.
+         * @param stop The stop callback.
+         * @param deinit The deinit callback.
+         *
+         * @return the DM Component reference for chaining (fluent API)
+         */
+        Component<T>& setCallbacks(
+            int (T::*init)(),
+            int (T::*start)(),
+            int (T::*stop)(),
+            int (T::*deinit)()
+        );
     };
 }}
 

http://git-wip-us.apache.org/repos/asf/celix/blob/8cecba96/dependency_manager_cxx/include/celix/dm/Component_Impl.h
----------------------------------------------------------------------
diff --git a/dependency_manager_cxx/include/celix/dm/Component_Impl.h b/dependency_manager_cxx/include/celix/dm/Component_Impl.h
index a7b51f8..86dbe33 100644
--- a/dependency_manager_cxx/include/celix/dm/Component_Impl.h
+++ b/dependency_manager_cxx/include/celix/dm/Component_Impl.h
@@ -142,13 +142,18 @@ Component<T>& Component<T>::remove(CServiceDependency<T,I>& dep) {
 }
 
 template<class T>
+Component<T>* Component<T>::create(bundle_context_pt context) {
+    std::string name = typeName<T>();
+    return Component<T>::create(context, name);
+}
+
+template<class T>
 Component<T>* Component<T>::create(bundle_context_pt context, std::string name) {
-    std::string n = name.empty() ? typeName<T>() : name;
 #ifdef __EXCEPTIONS
-    Component<T>* cmp = new Component<T>{context, n};
+    Component<T>* cmp = new Component<T>{context, name};
 #else
     static Component<T> invalid{nullptr, std::string{}};
-    Component<T>* cmp = new(std::nothrow) Component<T>(context, n);
+    Component<T>* cmp = new(std::nothrow) Component<T>(context, name);
     if (cmp == nullptr) {
         cmp = &invalid;
     }
@@ -258,3 +263,57 @@ Component<T>& Component<T>::setCallbacks(
 
     return *this;
 }
+
+template<class T>
+Component<T>& Component<T>::setCallbacks(
+            int (T::*init)(),
+            int (T::*start)(),
+            int (T::*stop)(),
+            int (T::*deinit)() ) {
+
+    this->initFpNoExc = init;
+    this->startFpNoExc = start;
+    this->stopFpNoExc = stop;
+    this->deinitFpNoExc = deinit;
+
+    int (*cInit)(void *) = [](void *handle) {
+        Component<T>* cmp = (Component<T>*)(handle);
+        T* inst = &cmp->getInstance();
+        int (T::*fp)() = cmp->initFpNoExc;
+        if (fp != nullptr) {
+            return (inst->*fp)();
+        }
+        return 0;
+    };
+    int (*cStart)(void *) = [](void *handle) {
+        Component<T>* cmp = (Component<T>*)(handle);
+        T* inst = &cmp->getInstance();
+        int (T::*fp)() = cmp->startFpNoExc;
+        if (fp != nullptr) {
+            return (inst->*fp)();
+        }
+        return 0;
+    };
+    int (*cStop)(void *) = [](void *handle) {
+        Component<T>* cmp = (Component<T>*)(handle);
+        T* inst = &cmp->getInstance();
+        int (T::*fp)() = cmp->stopFpNoExc;
+        if (fp != nullptr) {
+            return (inst->*fp)();
+        }
+        return 0;
+    };
+    int (*cDeinit)(void *) = [](void *handle) {
+        Component<T>* cmp = (Component<T>*)(handle);
+        T* inst = &cmp->getInstance();
+        int (T::*fp)() = cmp->deinitFpNoExc;
+        if (fp != nullptr) {
+            return (inst->*fp)();
+        }
+        return 0;
+    };
+
+    component_setCallbacks(this->cComponent(), cInit, cStart, cStop, cDeinit);
+
+    return *this;
+}

http://git-wip-us.apache.org/repos/asf/celix/blob/8cecba96/dependency_manager_cxx/include/celix/dm/DependencyManager.h
----------------------------------------------------------------------
diff --git a/dependency_manager_cxx/include/celix/dm/DependencyManager.h b/dependency_manager_cxx/include/celix/dm/DependencyManager.h
index c3479f4..c5905db 100644
--- a/dependency_manager_cxx/include/celix/dm/DependencyManager.h
+++ b/dependency_manager_cxx/include/celix/dm/DependencyManager.h
@@ -41,8 +41,8 @@ namespace celix { namespace dm {
         DependencyManager(bundle_context_pt context);
         virtual ~DependencyManager();
 
-        const bundle_context_pt bundleContext() const;
-        const dm_dependency_manager_pt cDependencyManager() const;
+        bundle_context_pt bundleContext() const;
+        dm_dependency_manager_pt cDependencyManager() const;
 
 
         /**
@@ -51,11 +51,13 @@ namespace celix { namespace dm {
          * @return Returns a reference to the DM Component
          */
         template<class T>
-        Component<T>& createComponent() {
-            Component<T>* cmp = Component<T>::create(this->context);
+        Component<T>& createComponent(std::string name = std::string{}) {
+            Component<T>* cmp = name.empty() ?
+                Component<T>::create(this->context) :
+                Component<T>::create(this->context, name);
             if (cmp->isValid()) {
                 this->components.push_back(std::unique_ptr<BaseComponent> {cmp});
-            }
+            } 
             return *cmp;
         }
 
@@ -66,8 +68,8 @@ namespace celix { namespace dm {
          * @return Returns a reference to the DM Component
          */
         template<class T>
-        Component<T>& createComponent(std::unique_ptr<T>&& rhs) {
-            return this->createComponent<T>().setInstance(std::move(rhs));
+        Component<T>& createComponent(std::unique_ptr<T>&& rhs, std::string name = std::string{}) {
+            return this->createComponent<T>(name).setInstance(std::move(rhs));
         }
 
         /**
@@ -77,8 +79,8 @@ namespace celix { namespace dm {
          * @return Returns a reference to the DM Component
          */
         template<class T>
-        Component<T>& createComponent(std::shared_ptr<T> rhs) {
-            return this->createComponent<T>().setInstance(rhs);
+        Component<T>& createComponent(std::shared_ptr<T> rhs, std::string name = std::string{}) {
+            return this->createComponent<T>(name).setInstance(rhs);
         }
 
         /**
@@ -88,8 +90,8 @@ namespace celix { namespace dm {
          * @return Returns a reference to the DM Component
          */
         template<class T>
-        Component<T>& createComponent(T rhs) {
-            return this->createComponent<T>().setInstance(std::forward(rhs));
+        Component<T>& createComponent(T rhs, std::string name = std::string{}) {
+            return this->createComponent<T>(name).setInstance(std::forward<T>(rhs));
         }
 
         /**

http://git-wip-us.apache.org/repos/asf/celix/blob/8cecba96/dependency_manager_cxx/include/celix/dm/DmActivator.h
----------------------------------------------------------------------
diff --git a/dependency_manager_cxx/include/celix/dm/DmActivator.h b/dependency_manager_cxx/include/celix/dm/DmActivator.h
index 2ec57fb..50c5aeb 100644
--- a/dependency_manager_cxx/include/celix/dm/DmActivator.h
+++ b/dependency_manager_cxx/include/celix/dm/DmActivator.h
@@ -58,49 +58,6 @@ namespace celix { namespace dm {
          * @returns A pointer to a DmActivator. The Dependency Manager is responsible for deleting the pointer when the bundle is stopped.
          */
         static DmActivator* create(DependencyManager& mng);
-
-        /**
-         * Creates and adds a new DM Component for a component of type T.
-         *
-         * @return Returns a reference to the DM Component
-         */
-        template<class T>
-        Component<T>& createComponent() {
-            return mng.createComponent<T>();
-        }
-
-        /**
-         * Creates and adds a new DM Component for a component of type T and setting
-         * the instance using a unique ptr.
-         *
-         * @return Returns a reference to the DM Component
-         */
-        template<class T>
-        Component<T>& createComponent(std::unique_ptr<T>&& rhs) {
-            return mng.createComponent<T>(std::move(rhs));
-        }
-
-        /**
-         * Creates and adds a new DM Component for a component of type T and setting
-         * the instance using a shared ptr.
-         *
-         * @return Returns a reference to the DM Component
-         */
-        template<class T>
-        Component<T>& createComponent(std::shared_ptr<T> rhs) {
-            return mng.createComponent<T>(rhs);
-        }
-
-        /**
-         * Creates and adds a new DM Component for a component of type T and setting
-         * the instance.
-         *
-         * @return Returns a reference to the DM Component
-         */
-        template<class T>
-        Component<T>& createComponent(T rhs) {
-            return mng.createComponent<T>(std::forward(rhs));
-        }
     };
 }}
 

http://git-wip-us.apache.org/repos/asf/celix/blob/8cecba96/dependency_manager_cxx/src/Component.cc
----------------------------------------------------------------------
diff --git a/dependency_manager_cxx/src/Component.cc b/dependency_manager_cxx/src/Component.cc
deleted file mode 100644
index cab3da5..0000000
--- a/dependency_manager_cxx/src/Component.cc
+++ /dev/null
@@ -1,43 +0,0 @@
-/**
- * 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 "celix/dm/Component.h"
-#include "celix/dm/DependencyManager.h"
-
-#include <typeinfo>
-
-using namespace celix::dm;
-
-BaseComponent::BaseComponent(const bundle_context_pt context, std::string name) : context(context), name(name) {
-    cCmp = nullptr;
-    component_create(context, name.c_str(), &this->cCmp);
-    component_setImplementation(this->cCmp, this);
-}
-
-BaseComponent::~BaseComponent() {
-
-}
-
-const dm_component_pt BaseComponent::cComponent() const {
-    return this->cCmp;
-}
-
-const bundle_context_pt BaseComponent::bundleContext() const {
-    return this->context;
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/celix/blob/8cecba96/dependency_manager_cxx/src/DependencyManager.cc
----------------------------------------------------------------------
diff --git a/dependency_manager_cxx/src/DependencyManager.cc b/dependency_manager_cxx/src/DependencyManager.cc
index 39f1995..b07383d 100644
--- a/dependency_manager_cxx/src/DependencyManager.cc
+++ b/dependency_manager_cxx/src/DependencyManager.cc
@@ -18,7 +18,6 @@
  */
 
 #include "celix/dm/DependencyManager.h"
-#include <typeinfo>
 
 using namespace celix::dm;
 
@@ -32,11 +31,11 @@ DependencyManager::~DependencyManager() {
     this->cDepMan = nullptr;
 }
 
-const bundle_context_pt DependencyManager::bundleContext() const {
+bundle_context_pt DependencyManager::bundleContext() const {
     return context;
 }
 
-const dm_dependency_manager_pt DependencyManager::cDependencyManager() const {
+dm_dependency_manager_pt DependencyManager::cDependencyManager() const {
     return cDepMan;
 }