Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/shared/EventWithSetter.h

Issue 6567422169448448: Issue 119 - Switch to injecting CSS for element hiding (Closed)
Patch Set: rebase, improve memory handling (OnQuit) and improve code organizing Created April 27, 2016, 1:10 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #pragma once
2 /*
3 * This file is part of Adblock Plus <https://adblockplus.org/>,
4 * Copyright (C) 2006-2016 Eyeo GmbH
5 *
6 * Adblock Plus is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3 as
8 * published by the Free Software Foundation.
9 *
10 * Adblock Plus is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "AutoHandle.h"
19 #include <cstdint>
20 #include <memory>
21
22 /// Wrapper around unnamed manual event windows object.
23 class Event {
24 public:
25 enum {
26 InfiniteTimeout = 0xFFFFFFFF
27 };
28 /// Creates manual event with nonsignaled initial state.
29 Event();
30 ~Event();
31 void Set();
32 void Reset();
33 bool operator!() const {
34 return m_handle == nullptr;
35 }
36 bool Wait(int32_t timeoutMsec = InfiniteTimeout);
37 private:
38 Event(const Event&);
39 void operator=(const Event&);
40 private:
41 HANDLE m_handle;
42 };
43
44 /// This class is a temporary replacement of std::promise/std::future.
45 ///
46 /// Before scheduling a task to a worker thread, the caller creates
47 /// `EventWithSetter` and obtains `EventWithSetter::Setter`. The task at the
48 /// end should call `EventWithSetter::Setter::Set` to notify the caller about
49 /// finishing.
50 /// If the the task is not finished because it has been canceled or it has
51 /// thrown an exception then the `EventWithSetter::Wait` returns `false`.
52 /// Example:
53 /// EventWithSetter event;
54 /// { // this scope is important because without it if `doWork()` throws
55 /// // `setter` won't be destroyed and the caller will wait forever.
56 /// auto setter = event.CreateSetter();
57 /// dispatchTask([setter]{
58 /// doWork();
59 /// setter->Set();
60 /// });
61 /// }
62 /// if (event.Wait()) onSuccess();
63 /// else onFail();
64 class EventWithSetter
65 {
66 public:
67 class Setter;
68 private:
69 struct Data
70 {
71 Data() : isSetCalled(false), isSetterCreated(false)
72 {}
73 Event event;
74 std::weak_ptr<Setter> eventSetter;
75 bool isSetCalled;
76 bool isSetterCreated;
77 };
78 typedef std::shared_ptr<Data> DataPtr;
79 public:
80 class Setter
81 {
82 public:
83 Setter(const DataPtr& data);
84 ~Setter();
85 void Set();
86 private:
87 Setter(const Setter&);
88 void operator=(const Setter&);
89 private:
90 DataPtr m_data;
91 };
92 EventWithSetter();
93 std::shared_ptr<Setter> CreateSetter();
94 bool Wait(int32_t timeoutMsec = Event::InfiniteTimeout);
95 private:
96 EventWithSetter(const EventWithSetter&);
97 void operator=(const EventWithSetter&);
98 private:
99 DataPtr m_data;
100 };
OLDNEW

Powered by Google App Engine
This is Rietveld