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.
ScheduledEventBuilder.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 <memory>
23 #include <functional>
24 
25 #include "celix/Exceptions.h"
26 #include "celix/ScheduledEvent.h"
27 
28 namespace celix {
29 
33 class ScheduledEventBuilder final {
34 public:
40  ScheduledEventBuilder(std::shared_ptr<celix_bundle_context_t> _cCtx) : ctx{std::move(_cCtx)}, options{} {}
41 
50  template<typename Rep, typename Period>
51  ScheduledEventBuilder& withInitialDelay(std::chrono::duration<Rep, Period> delay) {
52  options.initialDelayInSeconds = std::chrono::duration_cast<std::chrono::duration<double>>(delay).count();
53  return *this;
54  }
55 
64  template<typename Rep, typename Period>
65  ScheduledEventBuilder& withInterval(std::chrono::duration<Rep, Period> interval) {
66  options.intervalInSeconds = std::chrono::duration_cast<std::chrono::duration<double>>(interval).count();
67  return *this;
68  }
69 
76  ScheduledEventBuilder& withName(std::string n) {
77  name = std::move(n);
78  return *this;
79  }
80 
89  ScheduledEventBuilder& withCallback(std::function<void()> cb) {
90  callback = std::move(cb);
91  return *this;
92  }
93 
103  ScheduledEventBuilder& withRemoveCallback(std::function<void()> cb) {
104  removeCallback = std::move(cb);
105  return *this;
106  }
107 
114  if (!callback) {
115  throw celix::Exception{"Cannot build scheduled event without callback"};
116  }
117  return ScheduledEvent{ctx, name, std::move(callback), std::move(removeCallback), options};
118  }
119 
120 private:
121  std::shared_ptr<celix_bundle_context_t> ctx;
123  std::string name{};
124  std::function<void()> callback{};
125  std::function<void()> removeCallback{};
126 };
127 
128 } // end namespace celix
celix
Definition: Bundle.h:28
celix::ScheduledEventBuilder::withInitialDelay
ScheduledEventBuilder & withInitialDelay(std::chrono::duration< Rep, Period > delay)
Set the initial delay of the scheduled event.
Definition: ScheduledEventBuilder.h:51
celix::ScheduledEventBuilder
A C++ builder for a ScheduledEvent object.
Definition: ScheduledEventBuilder.h:33
ScheduledEvent.h
celix::ScheduledEventBuilder::ScheduledEventBuilder
ScheduledEventBuilder(std::shared_ptr< celix_bundle_context_t > _cCtx)
Construct a scheduled event builder with the given bundle context.
Definition: ScheduledEventBuilder.h:40
celix::ScheduledEventBuilder::withName
ScheduledEventBuilder & withName(std::string n)
Set the name of the scheduled event.
Definition: ScheduledEventBuilder.h:76
celix_scheduled_event_options
Celix scheduled event options, used for creating scheduling events with the celix framework.
Definition: celix_bundle_context.h:1463
celix::ScheduledEventBuilder::withRemoveCallback
ScheduledEventBuilder & withRemoveCallback(std::function< void()> cb)
Set the remove callback of the scheduled event.
Definition: ScheduledEventBuilder.h:103
celix::ScheduledEvent
A C++ abstraction for a scheduled event in Celix.
Definition: ScheduledEvent.h:35
celix::ScheduledEventBuilder::withCallback
ScheduledEventBuilder & withCallback(std::function< void()> cb)
Set the callback of the scheduled event.
Definition: ScheduledEventBuilder.h:89
celix::ScheduledEventBuilder::withInterval
ScheduledEventBuilder & withInterval(std::chrono::duration< Rep, Period > interval)
Set the interval of the scheduled event.
Definition: ScheduledEventBuilder.h:65
celix::ScheduledEventBuilder::build
ScheduledEvent build()
Build the scheduled event with the given options.
Definition: ScheduledEventBuilder.h:113