added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / ObjectStore / src / collection_notifications.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #include "collection_notifications.hpp"
20
21 #include "impl/collection_notifier.hpp"
22
23 using namespace realm;
24 using namespace realm::_impl;
25
26 NotificationToken::NotificationToken(std::shared_ptr<_impl::CollectionNotifier> notifier, uint64_t token)
27 : m_notifier(std::move(notifier)), m_token(token)
28 {
29 }
30
31 NotificationToken::~NotificationToken()
32 {
33     // m_notifier itself (and not just the pointed-to thing) needs to be accessed
34     // atomically to ensure that there are no data races when the token is
35     // destroyed after being modified on a different thread.
36     // This is needed despite the token not being thread-safe in general as
37     // users find it very surprising for obj-c objects to care about what
38     // thread they are deallocated on.
39     if (auto notifier = m_notifier.exchange({})) {
40         notifier->remove_callback(m_token);
41     }
42 }
43
44 NotificationToken::NotificationToken(NotificationToken&&) = default;
45
46 NotificationToken& NotificationToken::operator=(realm::NotificationToken&& rgt)
47 {
48     if (this != &rgt) {
49         if (auto notifier = m_notifier.exchange({})) {
50             notifier->remove_callback(m_token);
51         }
52         m_notifier = std::move(rgt.m_notifier);
53         m_token = rgt.m_token;
54     }
55     return *this;
56 }
57
58 void NotificationToken::suppress_next()
59 {
60     m_notifier.load()->suppress_next_notification(m_token);
61 }