skvlad | 98bb664 | 2016-04-07 15:36:45 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_ONETIMEEVENT_H_ |
| 12 | #define WEBRTC_BASE_ONETIMEEVENT_H_ |
| 13 | |
| 14 | #include "webrtc/base/scoped_ptr.h" |
| 15 | #include "webrtc/base/criticalsection.h" |
| 16 | #include "webrtc/typedefs.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | // Provides a simple way to perform an operation (such as logging) one |
| 20 | // time in a certain scope. |
| 21 | // Example: |
| 22 | // OneTimeEvent firstFrame; |
| 23 | // ... |
| 24 | // if (firstFrame()) { |
| 25 | // LOG(LS_INFO) << "This is the first frame". |
| 26 | // } |
| 27 | class OneTimeEvent { |
| 28 | public: |
| 29 | OneTimeEvent() {} |
| 30 | bool operator()() { |
| 31 | rtc::CritScope cs(&critsect_); |
| 32 | if (happened_) { |
| 33 | return false; |
| 34 | } |
| 35 | happened_ = true; |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | bool happened_ = false; |
| 41 | rtc::CriticalSection critsect_; |
| 42 | }; |
| 43 | |
| 44 | // A non-thread-safe, ligher-weight version of the OneTimeEvent class. |
| 45 | class ThreadUnsafeOneTimeEvent { |
| 46 | public: |
| 47 | ThreadUnsafeOneTimeEvent() {} |
| 48 | bool operator()() { |
| 49 | if (happened_) { |
| 50 | return false; |
| 51 | } |
| 52 | happened_ = true; |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | bool happened_ = false; |
| 58 | }; |
| 59 | |
| 60 | } // namespace webrtc |
| 61 | |
| 62 | #endif // WEBRTC_BASE_ONETIMEEVENT_H_ |