Apache Celix  latest
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.
ServiceRegistrationBuilder.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 #pragma once
21 
22 #include <string>
23 #include <vector>
24 #include <memory>
25 
26 #include "celix/Utils.h"
28 
29 namespace celix {
37  template<typename I>
39  private:
40  friend class BundleContext;
41 
42  //NOTE private to prevent move so that a build() call cannot be forgotten
44  public:
46  std::shared_ptr<celix_bundle_context_t> _cCtx,
47  std::shared_ptr<I> _svc,
48  std::string _name,
49  bool _registerAsync = true,
50  bool _unregisterAsync = true) :
51  cCtx{std::move(_cCtx)},
52  svc{std::move(_svc)},
53  name{std::move(_name)},
54  version{celix::typeVersion<I>()},
55  registerAsync{_registerAsync},
56  unregisterAsync{_unregisterAsync} {}
57 
58  ServiceRegistrationBuilder& operator=(ServiceRegistrationBuilder&&) = delete;
59  ServiceRegistrationBuilder(const ServiceRegistrationBuilder&) = delete;
60  ServiceRegistrationBuilder operator=(const ServiceRegistrationBuilder&) = delete;
61 
67  ServiceRegistrationBuilder& setVersion(std::string v) { version = std::move(v); return *this; }
68 
74  template<typename T>
75  ServiceRegistrationBuilder& addProperty(const std::string& key, T&& value) { properties.set(key, std::forward<T>(value)); return *this; }
76 
82  ServiceRegistrationBuilder& setProperties(celix::Properties p) { properties = std::move(p); return *this; }
83 
91  for (const auto& pair : props) {
92  properties.set(pair.first, pair.second);
93  }
94  return *this;
95  }
96 
103  onRegisteredCallbacks.emplace_back(std::move(callback));
104  return *this;
105  }
106 
113  onUnregisteredCallbacks.emplace_back(std::move(callback));
114  return *this;
115  }
116 
133  registerAsync = async;
134  return *this;
135  }
136 
156  unregisterAsync = async;
157  return *this;
158  }
159 
167  std::shared_ptr<ServiceRegistration> build() {
169  cCtx,
170  std::move(svc),
171  std::move(name),
172  std::move(version),
173  std::move(properties),
174  registerAsync,
175  unregisterAsync,
176  std::move(onRegisteredCallbacks),
177  std::move(onUnregisteredCallbacks));
178  }
179  private:
180  const std::shared_ptr<celix_bundle_context_t> cCtx;
181  std::shared_ptr<I> svc;
182  std::string name;
183  std::string version;
184  bool registerAsync;
185  bool unregisterAsync;
186  celix::Properties properties{};
187  std::vector<std::function<void(ServiceRegistration&)>> onRegisteredCallbacks{};
188  std::vector<std::function<void(ServiceRegistration&)>> onUnregisteredCallbacks{};
189  };
190 }
celix::ServiceRegistrationBuilder::addProperty
ServiceRegistrationBuilder & addProperty(const std::string &key, T &&value)
Add a property to the service properties.
Definition: ServiceRegistrationBuilder.h:75
celix::BundleContext
The bundle context is used to interact with the Celix framework.
Definition: BundleContext.h:53
celix::ServiceRegistrationBuilder::setUnregisterAsync
ServiceRegistrationBuilder & setUnregisterAsync(bool async)
Configure if the service un-registration will be done synchronized or asynchronized.
Definition: ServiceRegistrationBuilder.h:155
celix
Definition: Bundle.h:28
celix::ServiceRegistrationBuilder::setVersion
ServiceRegistrationBuilder & setVersion(std::string v)
Set the service version.
Definition: ServiceRegistrationBuilder.h:67
celix::ServiceRegistrationBuilder::addOnRegistered
ServiceRegistrationBuilder & addOnRegistered(std::function< void(ServiceRegistration &)> callback)
Adds an on registered callback for the service registration.
Definition: ServiceRegistrationBuilder.h:102
celix::ServiceRegistration
A registered service.
Definition: ServiceRegistration.h:54
celix::ServiceRegistrationBuilder::addProperties
ServiceRegistrationBuilder & addProperties(const celix::Properties &props)
Add the properties to the service properties.
Definition: ServiceRegistrationBuilder.h:90
celix::ServiceRegistrationBuilder
Fluent builder API to build a new service registration for a service.
Definition: ServiceRegistrationBuilder.h:38
celix::ServiceRegistrationBuilder::operator=
ServiceRegistrationBuilder & operator=(ServiceRegistrationBuilder &&)=delete
celix::ServiceRegistrationBuilder::addOnUnregistered
ServiceRegistrationBuilder & addOnUnregistered(std::function< void(ServiceRegistration &)> callback)
Adds an on unregistered callback for the service registration.
Definition: ServiceRegistrationBuilder.h:112
celix::ServiceRegistrationBuilder::setProperties
ServiceRegistrationBuilder & setProperties(celix::Properties p)
Set the service properties.
Definition: ServiceRegistrationBuilder.h:82
celix::ServiceRegistrationBuilder::setRegisterAsync
ServiceRegistrationBuilder & setRegisterAsync(bool async)
Configure if the service registration will be done synchronized or asynchronized.
Definition: ServiceRegistrationBuilder.h:132
celix::ServiceRegistrationBuilder::build
std::shared_ptr< ServiceRegistration > build()
"Builds" the service registration and return a ServiceRegistration.
Definition: ServiceRegistrationBuilder.h:167
celix::dm::Properties
celix::Properties Properties
Definition: Properties.h:25
ServiceRegistration.h
celix::ServiceRegistration::create
static std::shared_ptr< ServiceRegistration > create(std::shared_ptr< celix_bundle_context_t > cCtx, std::shared_ptr< void > svc, const std::string &name, const std::string &version, celix::Properties properties, bool registerAsync, bool unregisterAsync, std::vector< std::function< void(ServiceRegistration &)>> onRegisteredCallbacks, std::vector< std::function< void(ServiceRegistration &)>> onUnregisteredCallbacks)
Definition: ServiceRegistration.h:71