Apache Celix  2.4.0
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.
ScheduledEvent.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 "celix_bundle_context.h"
23 
24 namespace celix {
25 
35 class ScheduledEvent final {
36  public:
37  friend class ScheduledEventBuilder;
38 
44  ScheduledEvent() = default;
45 
49  ~ScheduledEvent() noexcept {
50  if (!isOneShot && ctx) {
52  }
53  }
54 
55  ScheduledEvent(const ScheduledEvent&) = delete;
56  ScheduledEvent& operator=(const ScheduledEvent&) = delete;
57 
59  : ctx{std::move(rhs.ctx)}, eventId{rhs.eventId}, isOneShot{rhs.isOneShot} {
60  rhs.eventId = -1;
61  }
62 
64  if (this != &rhs) {
65  ctx = std::move(rhs.ctx);
66  eventId = rhs.eventId;
67  isOneShot = rhs.isOneShot;
68  rhs.eventId = -1;
69  }
70  return *this;
71  }
72 
80  void cancel() {
81  if (ctx) {
82  celix_bundleContext_removeScheduledEvent(ctx.get(), eventId);
83  }
84  }
85 
90  void wakeup() {
91  if (ctx) {
92  celix_bundleContext_wakeupScheduledEvent(ctx.get(), eventId);
93  }
94  }
95 
105  template <typename Rep, typename Period>
106  bool waitFor(std::chrono::duration<Rep, Period> waitTime) {
107  double waitTimeInSeconds = std::chrono::duration_cast<std::chrono::duration<double>>(waitTime).count();
108  celix_status_t status = CELIX_SUCCESS;
109  if (ctx) {
110  status = celix_bundleContext_waitForScheduledEvent(ctx.get(), eventId, waitTimeInSeconds);
111  }
112  return status == CELIX_SUCCESS;
113  }
114 
115  private:
116  struct Callbacks {
117  std::function<void()> callback{};
118  std::function<void()> removeCallback{};
119  };
120 
121  static void (*callback)(void*);
122 
129  ScheduledEvent(std::shared_ptr<celix_bundle_context_t> _cCtx,
130  const std::string& _name,
131  std::function<void()> _callback,
132  std::function<void()> _removeCallback,
134  static auto callback = [](void* data) {
135  auto* callbacks = static_cast<Callbacks*>(data);
136  (callbacks->callback)();
137  };
138  static auto removeCallback = [](void* data) {
139  auto* callbacks = static_cast<Callbacks*>(data);
140  if (callbacks->removeCallback) {
141  (callbacks->removeCallback)();
142  }
143  delete callbacks;
144  };
145 
146  ctx = std::move(_cCtx);
147  isOneShot = options.intervalInSeconds == 0;
148  options.name = _name.c_str();
149  auto* callbacks = new Callbacks{std::move(_callback), std::move(_removeCallback)};
150  options.callbackData = callbacks;
151  options.callback = callback;
152  options.removeCallbackData = callbacks;
153  options.removeCallback = removeCallback;
154  eventId = celix_bundleContext_scheduleEvent(ctx.get(), &options);
155  }
156 
157  std::shared_ptr<celix_bundle_context_t> ctx{};
158  long eventId{-1};
159  bool isOneShot{false};
160 };
161 
162 } // end namespace celix
celix_bundleContext_wakeupScheduledEvent
CELIX_FRAMEWORK_EXPORT celix_status_t celix_bundleContext_wakeupScheduledEvent(celix_bundle_context_t *ctx, long scheduledEventId)
Wakeup a scheduled event and returns immediately, not waiting for the scheduled event callback to be ...
celix::ScheduledEvent::ScheduledEvent
ScheduledEvent()=default
Constructs a empty / not-active scheduled event.
celix_scheduled_event_options::callback
void(* callback)(void *callbackData) CELIX_OPTS_INIT
Callback function called to process a scheduled event. Will be called on the event thread.
Definition: celix_bundle_context.h:1307
celix
Definition: Bundle.h:27
celix_bundleContext_waitForScheduledEvent
CELIX_FRAMEWORK_EXPORT celix_status_t celix_bundleContext_waitForScheduledEvent(celix_bundle_context_t *ctx, long scheduledEventId, double waitTimeInSeconds)
Wait until the next scheduled event is processed.
celix::ScheduledEventBuilder
A C++ builder for a ScheduledEvent object.
Definition: ScheduledEventBuilder.h:33
celix_bundleContext_removeScheduledEvent
CELIX_FRAMEWORK_EXPORT bool celix_bundleContext_removeScheduledEvent(celix_bundle_context_t *ctx, long scheduledEventId)
Cancel and remove a scheduled event.
celix_bundleContext_tryRemoveScheduledEventAsync
CELIX_FRAMEWORK_EXPORT bool celix_bundleContext_tryRemoveScheduledEventAsync(celix_bundle_context_t *ctx, long scheduledEventId)
Try to cancel and remove a scheduled event asynchronously.
celix::ScheduledEvent::wakeup
void wakeup()
Wakeup a scheduled event and returns immediately, not waiting for the scheduled event callback to be ...
Definition: ScheduledEvent.h:90
celix_bundleContext_scheduleEvent
CELIX_FRAMEWORK_EXPORT long celix_bundleContext_scheduleEvent(celix_bundle_context_t *ctx, const celix_scheduled_event_options_t *options)
Add a scheduled event to the Celix framework.
celix_scheduled_event_options::removeCallback
void(* removeCallback)(void *removeCallbackData) CELIX_OPTS_INIT
Callback function called when a scheduled event is removed. Will be called on the event thread.
Definition: celix_bundle_context.h:1313
celix::ScheduledEvent::ScheduledEvent
ScheduledEvent(ScheduledEvent &&rhs) noexcept
Definition: ScheduledEvent.h:58
celix::ScheduledEvent::operator=
ScheduledEvent & operator=(const ScheduledEvent &)=delete
celix_scheduled_event_options
Celix scheduled event options, used for creating scheduling events with the celix framework.
Definition: celix_bundle_context.h:1292
celix::ScheduledEvent::~ScheduledEvent
~ScheduledEvent() noexcept
Destroys the scheduled event by removes it from the bundle context if it is not a one-short event.
Definition: ScheduledEvent.h:49
celix::ScheduledEvent::cancel
void cancel()
Cancels the scheduled event.
Definition: ScheduledEvent.h:80
celix::ScheduledEvent
A C++ abstraction for a scheduled event in Celix.
Definition: ScheduledEvent.h:35
celix::ScheduledEvent::waitFor
bool waitFor(std::chrono::duration< Rep, Period > waitTime)
Wait until the next scheduled event is processed.
Definition: ScheduledEvent.h:106
celix_bundle_context.h
celix::ScheduledEvent::operator=
ScheduledEvent & operator=(ScheduledEvent &&rhs) noexcept
Definition: ScheduledEvent.h:63