Add missing include for flock()
[folly.git] / folly / wangle / rx / Subscription.h
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <folly/wangle/rx/types.h> // must come first
20 #include <folly/wangle/rx/Observable.h>
21
22 namespace folly { namespace wangle {
23
24 template <class T>
25 class Subscription {
26  public:
27   Subscription() = default;
28
29   Subscription(const Subscription&) = delete;
30
31   Subscription(Subscription&& other) noexcept {
32     *this = std::move(other);
33   }
34
35   Subscription& operator=(Subscription&& other) noexcept {
36     unsubscribe();
37     unsubscriber_ = std::move(other.unsubscriber_);
38     id_ = other.id_;
39     other.unsubscriber_ = nullptr;
40     other.id_ = 0;
41     return *this;
42   }
43
44   ~Subscription() {
45     unsubscribe();
46   }
47
48  private:
49   typedef typename Observable<T>::Unsubscriber Unsubscriber;
50
51   Subscription(std::shared_ptr<Unsubscriber> unsubscriber, uint64_t id)
52     : unsubscriber_(std::move(unsubscriber)), id_(id) {
53     CHECK(id_ > 0);
54   }
55
56   void unsubscribe() {
57     if (unsubscriber_ && id_ > 0) {
58       unsubscriber_->unsubscribe(id_);
59       id_ = 0;
60       unsubscriber_ = nullptr;
61     }
62   }
63
64   std::shared_ptr<Unsubscriber> unsubscriber_;
65   uint64_t id_{0};
66
67   friend class Observable<T>;
68 };
69
70 }}