summaryrefslogtreecommitdiff
path: root/src/CommonAPI/MainLoopContext.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/CommonAPI/MainLoopContext.cpp')
-rw-r--r--src/CommonAPI/MainLoopContext.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/CommonAPI/MainLoopContext.cpp b/src/CommonAPI/MainLoopContext.cpp
new file mode 100644
index 0000000..b13cd89
--- /dev/null
+++ b/src/CommonAPI/MainLoopContext.cpp
@@ -0,0 +1,99 @@
+/* Copyright (C) 2013 BMW Group
+ * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
+ * Author: Juergen Gehring (juergen.gehring@bmw.de)
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "MainLoopContext.h"
+
+
+namespace CommonAPI {
+
+int64_t getCurrentTimeInMs() {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
+}
+
+DispatchSourceListenerSubscription MainLoopContext::subscribeForDispatchSources(DispatchSourceAddedCallback dispatchAddedCallback, DispatchSourceRemovedCallback dispatchRemovedCallback) {
+ dispatchSourceListeners_.emplace_front(dispatchAddedCallback, dispatchRemovedCallback);
+ return dispatchSourceListeners_.begin();
+}
+
+WatchListenerSubscription MainLoopContext::subscribeForWatches(WatchAddedCallback watchAddedCallback, WatchRemovedCallback watchRemovedCallback) {
+ watchListeners_.emplace_front(watchAddedCallback, watchRemovedCallback);
+ return watchListeners_.begin();
+}
+
+TimeoutSourceListenerSubscription MainLoopContext::subscribeForTimeouts(TimeoutSourceAddedCallback timeoutAddedCallback, TimeoutSourceRemovedCallback timeoutRemovedCallback) {
+ timeoutSourceListeners_.emplace_front(timeoutAddedCallback, timeoutRemovedCallback);
+ return timeoutSourceListeners_.begin();
+}
+
+WakeupListenerSubscription MainLoopContext::subscribeForWakeupEvents(WakeupCallback wakeupCallback) {
+ wakeupListeners_.emplace_front(wakeupCallback);
+ return wakeupListeners_.begin();
+}
+
+void MainLoopContext::unsubscribeForDispatchSources(DispatchSourceListenerSubscription subscription) {
+ dispatchSourceListeners_.erase(subscription);
+}
+
+void MainLoopContext::unsubscribeForWatches(WatchListenerSubscription subscription) {
+ watchListeners_.erase(subscription);
+}
+
+void MainLoopContext::unsubscribeForTimeouts(TimeoutSourceListenerSubscription subscription) {
+ timeoutSourceListeners_.erase(subscription);
+}
+
+void MainLoopContext::unsubscribeForWakeupEvents(WakeupListenerSubscription subscription) {
+ wakeupListeners_.erase(subscription);
+}
+
+void MainLoopContext::registerDispatchSource(DispatchSource* dispatchSource, const DispatchPriority dispatchPriority) {
+ for(auto listener = dispatchSourceListeners_.begin(); listener != dispatchSourceListeners_.end(); ++listener) {
+ listener->first(dispatchSource, dispatchPriority);
+ }
+}
+
+void MainLoopContext::deregisterDispatchSource(DispatchSource* dispatchSource) {
+ for(auto listener = dispatchSourceListeners_.begin(); listener != dispatchSourceListeners_.end(); ++listener) {
+ listener->second(dispatchSource);
+ }
+}
+
+void MainLoopContext::registerWatch(Watch* watch, const DispatchPriority dispatchPriority) {
+ for(auto listener = watchListeners_.begin(); listener != watchListeners_.end(); ++listener) {
+ listener->first(watch, dispatchPriority);
+ }
+}
+
+void MainLoopContext::deregisterWatch(Watch* watch) {
+ for(auto listener = watchListeners_.begin(); listener != watchListeners_.end(); ++listener) {
+ listener->second(watch);
+ }
+}
+
+void MainLoopContext::registerTimeoutSource(Timeout* timeoutEvent, const DispatchPriority dispatchPriority) {
+ for(auto listener = timeoutSourceListeners_.begin(); listener != timeoutSourceListeners_.end(); ++listener) {
+ listener->first(timeoutEvent, dispatchPriority);
+ }
+}
+
+void MainLoopContext::deregisterTimeoutSource(Timeout* timeoutEvent) {
+ for(auto listener = timeoutSourceListeners_.begin(); listener != timeoutSourceListeners_.end(); ++listener) {
+ listener->second(timeoutEvent);
+ }
+}
+
+void MainLoopContext::wakeup() {
+ for(auto listener = wakeupListeners_.begin(); listener != wakeupListeners_.end(); ++listener) {
+ (*listener)();
+ }
+}
+
+bool MainLoopContext::isInitialized() {
+ return dispatchSourceListeners_.size() > 0 || watchListeners_.size() > 0;
+}
+
+} //Namespace CommonAPI