celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erjanalt...@apache.org
Subject celix git commit: CELIX-448: Added component_removeInterface / C++ Component::removeCInterface
Date Thu, 28 Jun 2018 18:40:24 GMT
Repository: celix
Updated Branches:
  refs/heads/develop a5192fc6a -> 788577859


CELIX-448: Added component_removeInterface / C++ Component::removeCInterface


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

Branch: refs/heads/develop
Commit: 788577859c99196b7f34f062616ab19199a9696f
Parents: a5192fc
Author: Erjan Altena <erjanaltena@gmail.com>
Authored: Thu Jun 28 20:39:27 2018 +0200
Committer: Erjan Altena <erjanaltena@gmail.com>
Committed: Thu Jun 28 20:39:49 2018 +0200

----------------------------------------------------------------------
 .../phase1/src/Phase1Activator.cc               | 33 ++++++++++++++++++--
 .../dm_example_cxx/phase1/src/Phase1Activator.h | 11 ++++++-
 libs/framework/include/celix/dm/Component.h     | 11 +++++++
 .../framework/include/celix/dm/Component_Impl.h |  8 +++++
 libs/framework/include/dm_component.h           |  7 +++++
 libs/framework/src/dm_component_impl.c          | 23 ++++++++++++++
 6 files changed, 90 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/78857785/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.cc
----------------------------------------------------------------------
diff --git a/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.cc b/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.cc
index 4bee1df..2dd5ed2 100644
--- a/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.cc
+++ b/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.cc
@@ -55,18 +55,47 @@ void Phase1Activator::init() {
         return cmp->infoCmd(line, out, err);
     };
 
+    Properties addProps;
+    addProps[OSGI_SHELL_COMMAND_NAME] = "add";
+    addProps[OSGI_SHELL_COMMAND_USAGE] = "add";
+    addProps[OSGI_SHELL_COMMAND_DESCRIPTION] = "add dummy service";
+
+
+    addCmd.handle = this;
+    addCmd.executeCommand = [](void *handle, char* /*line*/, FILE* out, FILE */*err*/) {
+        Phase1Activator* act = (Phase1Activator*)handle;
+        fprintf(out, "Adding dummy interface");
+        act->phase1cmp->addCInterface(act->dummySvc, "DUMMY_SERVICE");
+        return 0;
+    };
+
+    Properties removeProps;
+    removeProps[OSGI_SHELL_COMMAND_NAME] = "remove";
+    removeProps[OSGI_SHELL_COMMAND_USAGE] = "remove";
+    removeProps[OSGI_SHELL_COMMAND_DESCRIPTION] = "remove dummy service";
+
+
+    removeCmd.handle = this;
+    removeCmd.executeCommand = [](void *handle, char* /*line*/, FILE* out, FILE */*err*/)
{
+        Phase1Activator* act = (Phase1Activator*)handle;
+        fprintf(out, "Removing dummy interface");
+        act->phase1cmp->removeCInterface(act->dummySvc);
+        return 0;
+    };
+
     auto tst = std::unique_ptr<InvalidCServ>(new InvalidCServ{});
     tst->handle = cmp.get();
 
 
-    mng.createComponent(std::move(cmp))  //using a pointer a instance. Also supported is
lazy initialization (default constructor needed) or a rvalue reference (move)
+    phase1cmp = &mng.createComponent(std::move(cmp))  //using a pointer a instance. Also
supported is lazy initialization (default constructor needed) or a rvalue reference (move)
         .addInterface<IPhase1>(IPHASE1_VERSION)
         //.addInterface<IPhase2>() -> Compile error (static assert), because Phase1Cmp
does not implement IPhase2
         .addCInterface(&cmd, OSGI_SHELL_COMMAND_SERVICE_NAME, "", cmdProps)
+        .addCInterface(&addCmd, OSGI_SHELL_COMMAND_SERVICE_NAME, "", addProps)
+        .addCInterface(&removeCmd, OSGI_SHELL_COMMAND_SERVICE_NAME, "", removeProps)
         //.addCInterface(tst.get(), "TEST_SRV") -> Compile error (static assert), because
InvalidCServ is not a pod
         .addInterface<srv::info::IName>(INAME_VERSION)
         .setCallbacks(&Phase1Cmp::init, &Phase1Cmp::start, &Phase1Cmp::stop,
&Phase1Cmp::deinit);
-
 }
 
 void Phase1Activator::deinit() {

http://git-wip-us.apache.org/repos/asf/celix/blob/78857785/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.h
----------------------------------------------------------------------
diff --git a/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.h b/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.h
index 37b25e3..a3b062b 100644
--- a/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.h
+++ b/examples/celix-examples/dm_example_cxx/phase1/src/Phase1Activator.h
@@ -24,11 +24,20 @@
 #include "command.h"
 
 using namespace celix::dm;
+struct DummyService {
+    void *handle;
+};
 
 class Phase1Activator : public DmActivator {
     command_service_t cmd {nullptr, nullptr};
-public:
+    command_service_t addCmd {nullptr, nullptr};
+    command_service_t removeCmd {nullptr, nullptr};
+    Component<Phase1Cmp> *phase1cmp{nullptr};
+    DummyService *dummySvc{new DummyService};
+    public:
     Phase1Activator(DependencyManager& mng) : DmActivator(mng) {}
+    Phase1Activator(const Phase1Activator&) = delete;
+    Phase1Activator& operator=(const Phase1Activator&) = delete;
     virtual void init();
     virtual void deinit();
 };

http://git-wip-us.apache.org/repos/asf/celix/blob/78857785/libs/framework/include/celix/dm/Component.h
----------------------------------------------------------------------
diff --git a/libs/framework/include/celix/dm/Component.h b/libs/framework/include/celix/dm/Component.h
index 06aaf20..076700e 100644
--- a/libs/framework/include/celix/dm/Component.h
+++ b/libs/framework/include/celix/dm/Component.h
@@ -161,6 +161,17 @@ namespace celix { namespace dm {
 
 
         /**
+         * Adds a C interface to provide as service to the Celix framework.
+         *
+         * @param svc The service struct
+         * @param serviceName The service name to use
+         * @param version The version of the interface (e.g. "1.0.0"), can be an empty string
+         * @param properties To (meta) properties to provide with the service
+         */
+        template<class I> Component<T>& removeCInterface(const I* svc);
+
+
+        /**
          * Creates and adds a C++ service dependency to the component
          *
          * @return the Service Dependency reference for chaining (fluent API)

http://git-wip-us.apache.org/repos/asf/celix/blob/78857785/libs/framework/include/celix/dm/Component_Impl.h
----------------------------------------------------------------------
diff --git a/libs/framework/include/celix/dm/Component_Impl.h b/libs/framework/include/celix/dm/Component_Impl.h
index 722431f..f20cf09 100644
--- a/libs/framework/include/celix/dm/Component_Impl.h
+++ b/libs/framework/include/celix/dm/Component_Impl.h
@@ -92,6 +92,14 @@ Component<T>& Component<T>::addCInterface(const I* svc,
const std::string servic
 
 template<class T>
 template<class I>
+Component<T>& Component<T>::removeCInterface(const I* svc){
+    static_assert(std::is_pod<I>::value, "Service I must be a 'Plain Old Data' object");
+    component_removeInterface(this->cComponent(), svc);
+    return *this;
+};
+
+template<class T>
+template<class I>
 ServiceDependency<T,I>& Component<T>::createServiceDependency(const std::string
name) {
 #ifdef __EXCEPTIONS
     auto dep = std::shared_ptr<ServiceDependency<T,I>> {new ServiceDependency<T,I>(name)};

http://git-wip-us.apache.org/repos/asf/celix/blob/78857785/libs/framework/include/dm_component.h
----------------------------------------------------------------------
diff --git a/libs/framework/include/dm_component.h b/libs/framework/include/dm_component.h
index b3236a9..0e5bd6b 100644
--- a/libs/framework/include/dm_component.h
+++ b/libs/framework/include/dm_component.h
@@ -83,6 +83,13 @@ celix_status_t component_setCLanguageProperty(dm_component_t *component,
bool se
 celix_status_t component_addInterface(dm_component_t *component, const char* serviceName,
const char* serviceVersion, const void* service, properties_t *properties);
 
 /**
+ * Removed  a C interface from a component the Celix framework.
+ *
+ * @param serviceName the service name.
+ * @return CELIX_SUCCESS when removed, CELIX_ILLEGAL_ARGUMENT when the component does not
provide the interface
+ */
+celix_status_t component_removeInterface(dm_component_pt component, const void* service);
+/**
  * Sets the implementation of the component. e.g. the component handle/self/this pointer.
  */
 celix_status_t component_setImplementation(dm_component_t *component, void* implementation);

http://git-wip-us.apache.org/repos/asf/celix/blob/78857785/libs/framework/src/dm_component_impl.c
----------------------------------------------------------------------
diff --git a/libs/framework/src/dm_component_impl.c b/libs/framework/src/dm_component_impl.c
index e0d9f52..e853a60 100644
--- a/libs/framework/src/dm_component_impl.c
+++ b/libs/framework/src/dm_component_impl.c
@@ -373,6 +373,29 @@ celix_status_t component_addInterface(dm_component_pt component, const
char* ser
     return status;
 }
 
+celix_status_t component_removeInterface(dm_component_pt component, const void* service)
{
+    celix_status_t status = CELIX_ILLEGAL_ARGUMENT;
+
+    celixThreadMutex_lock(&component->mutex);
+    int nof_interfaces = arrayList_size(component->dm_interfaces);
+    for (unsigned int i = 0; i < nof_interfaces; ++i) {
+        dm_interface_t *interface = (dm_interface_t *) arrayList_get(component->dm_interfaces,
i);
+        if (interface->service == service) {
+            arrayList_remove(component->dm_interfaces, i);
+            if (component->state == DM_CMP_STATE_TRACKING_OPTIONAL) {
+                celixThreadMutex_unlock(&component->mutex);
+                component_unregisterServices(component);
+                component_registerServices(component);
+                celixThreadMutex_lock(&component->mutex);
+            }
+            status = CELIX_SUCCESS;
+            break;
+        }
+    }
+    celixThreadMutex_unlock(&component->mutex);
+
+    return status;
+}
 
 celix_status_t component_getInterfaces(dm_component_pt component, array_list_pt *out) {
     celix_status_t status = CELIX_SUCCESS;


Mime
View raw message