2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #import <Foundation/Foundation.h>
23 #include <type_traits>
24 #include "trigger-event.h"
28 const char *EventName = "Dali::Internal::Adaptor::Triggerevent_";
31 @interface NotificationObserver : NSObject
33 -(NotificationObserver *) initTriggerImpl:(Dali::Internal::Adaptor::TriggerEvent::Impl *) impl;
37 namespace Dali::Internal::Adaptor
40 struct TriggerEvent::Impl final
42 std::unique_ptr<CallbackBase> mCallback;
43 NotificationObserver *mReceiver;
45 TriggerEventInterface::Options mOptions;
47 Impl(CallbackBase *callback, TriggerEventInterface::Options options)
48 : mCallback(callback), mOptions(options)
50 const auto myId = mNameId.fetch_add(1, std::memory_order_relaxed);
53 ss << EventName << myId;
54 mName = [NSString stringWithUTF8String:ss.str().c_str()];
56 mReceiver = [[NotificationObserver alloc] initTriggerImpl:this];
59 Impl(Impl &&) = delete;
60 Impl &operator=(Impl &&) = delete;
64 auto *center = [NSNotificationCenter defaultCenter];
65 [center removeObserver:mReceiver];
70 CallbackBase::Execute( *mCallback );
74 // This is incremented each time the class is instatiated to guarantee
75 // an unique notification id
76 static std::atomic_uint64_t mNameId;
79 std::atomic<uint64_t> TriggerEvent::Impl::mNameId{0};
81 TriggerEvent::TriggerEvent(CallbackBase *callback, TriggerEventInterface::Options options)
83 , mImpl(std::make_unique<Impl>(MakeCallback(this, &TriggerEvent::Triggered), options))
87 void TriggerEvent::Trigger()
89 auto center = [NSDistributedNotificationCenter defaultCenter];
91 // Post a notification to the notification center
92 // The run loop will pop the queue and call the notification center
93 [center postNotificationName:mImpl->mName object:nil];
96 void TriggerEvent::Triggered()
98 CallbackBase::Execute(*mCallback);
100 if (mImpl->mOptions == TriggerEventInterface::DELETE_AFTER_TRIGGER)
108 @implementation NotificationObserver
110 Dali::Internal::Adaptor::TriggerEvent::Impl *mImpl;
113 -(void) ReceiveNotification: (NSNotification *) aNotification
118 -(NotificationObserver *) initTriggerImpl:(Dali::Internal::Adaptor::TriggerEvent::Impl *) impl;
124 auto center = [NSDistributedNotificationCenter defaultCenter];
125 [center addObserver:self
126 selector:@selector(ReceiveNotification:)
129 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];