Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: mojo/dart/packages/mojo/lib/src/handle.dart

Issue 1439993003: Dart: Avoid MojoResult and MojoHandleSignals constructors. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 part of core; 5 part of core;
6 6
7 class MojoHandle { 7 class MojoHandle {
8 static const int INVALID = 0; 8 static const int INVALID = 0;
9 static const int DEADLINE_INDEFINITE = -1; 9 static const int DEADLINE_INDEFINITE = -1;
10 10
11 int _h; 11 int _h;
12 int get h => _h; 12 int get h => _h;
13 13
14 MojoHandle(this._h, {String description}) { 14 MojoHandle(this._h, {String description}) {
15 MojoHandleNatives.addOpenHandle(_h, description: description); 15 MojoHandleNatives.addOpenHandle(_h, description: description);
16 } 16 }
17 17
18 MojoHandle._internal(this._h); 18 MojoHandle._internal(this._h);
19 19
20 MojoHandle.invalid() : this._internal(INVALID); 20 MojoHandle.invalid() : this._internal(INVALID);
21 21
22 MojoResult close() { 22 int close() {
23 MojoHandleNatives.removeOpenHandle(_h); 23 MojoHandleNatives.removeOpenHandle(_h);
24 int result = MojoHandleNatives.close(_h); 24 int result = MojoHandleNatives.close(_h);
25 _h = INVALID; 25 _h = INVALID;
26 return new MojoResult(result); 26 return result;
27 } 27 }
28 28
29 MojoHandle pass() { 29 MojoHandle pass() {
30 MojoHandleNatives.removeOpenHandle(_h); 30 MojoHandleNatives.removeOpenHandle(_h);
31 return this; 31 return this;
32 } 32 }
33 33
34 MojoWaitResult wait(int signals, int deadline) { 34 MojoWaitResult wait(int signals, int deadline) {
35 List result = MojoHandleNatives.wait(h, signals, deadline); 35 List result = MojoHandleNatives.wait(h, signals, deadline);
36 var state = result[1] != null 36 var state = result[1] != null
37 ? new MojoHandleSignalsState(result[1][0], result[1][1]) 37 ? new MojoHandleSignalsState(result[1][0], result[1][1])
38 : null; 38 : null;
39 return new MojoWaitResult(new MojoResult(result[0]), state); 39 return new MojoWaitResult(result[0], state);
40 } 40 }
41 41
42 bool _ready(MojoHandleSignals signal) { 42 bool _ready(int signal) {
43 MojoWaitResult mwr = wait(signal.value, 0); 43 MojoWaitResult mwr = wait(signal, 0);
44 switch (mwr.result) { 44 switch (mwr.result) {
45 case MojoResult.OK: 45 case MojoResult.kOk:
46 return true; 46 return true;
47 case MojoResult.DEADLINE_EXCEEDED: 47 case MojoResult.kDeadlineExceeded:
48 case MojoResult.CANCELLED: 48 case MojoResult.kCancelled:
49 case MojoResult.INVALID_ARGUMENT: 49 case MojoResult.kInvalidArgument:
50 case MojoResult.FAILED_PRECONDITION: 50 case MojoResult.kFailedPrecondition:
51 return false; 51 return false;
52 default: 52 default:
53 // Should be unreachable. 53 // Should be unreachable.
54 throw new MojoInternalError("Unexpected result $mwr for wait on $h"); 54 throw new MojoInternalError("Unexpected result $mwr for wait on $h");
55 } 55 }
56 } 56 }
57 57
58 bool get readyRead => _ready(MojoHandleSignals.PEER_CLOSED_READABLE); 58 bool get readyRead => _ready(MojoHandleSignals.kPeerClosedReadable);
59 bool get readyWrite => _ready(MojoHandleSignals.WRITABLE); 59 bool get readyWrite => _ready(MojoHandleSignals.kWritable);
60 bool get isValid => (_h != INVALID); 60 bool get isValid => (_h != INVALID);
61 61
62 String toString() { 62 String toString() {
63 if (!isValid) { 63 if (!isValid) {
64 return "MojoHandle(INVALID)"; 64 return "MojoHandle(INVALID)";
65 } 65 }
66 var mwr = wait(MojoHandleSignals.kAll, 0); 66 var mwr = wait(MojoHandleSignals.kAll, 0);
67 return "MojoHandle(h: $h, status: $mwr)"; 67 return "MojoHandle(h: $h, status: $mwr)";
68 } 68 }
69 69
70 bool operator ==(MojoHandle other) { 70 bool operator ==(MojoHandle other) {
71 return _h == other._h; 71 return _h == other._h;
72 } 72 }
73 73
74 static MojoWaitManyResult waitMany( 74 static MojoWaitManyResult waitMany(
75 List<int> handles, List<int> signals, int deadline) { 75 List<int> handles, List<int> signals, int deadline) {
76 List result = MojoHandleNatives.waitMany(handles, signals, deadline); 76 List result = MojoHandleNatives.waitMany(handles, signals, deadline);
77 List states = result[2] != null 77 List states = result[2] != null
78 ? result[2].map((l) => new MojoHandleSignalsState(l[0], l[1])).toList() 78 ? result[2].map((l) => new MojoHandleSignalsState(l[0], l[1])).toList()
79 : null; 79 : null;
80 return new MojoWaitManyResult(new MojoResult(result[0]), result[1], states); 80 return new MojoWaitManyResult(result[0], result[1], states);
81 } 81 }
82 82
83 static bool registerFinalizer(MojoEventSubscription eventSubscription) { 83 static bool registerFinalizer(MojoEventSubscription eventSubscription) {
84 return MojoHandleNatives.registerFinalizer( 84 return MojoHandleNatives.registerFinalizer(
85 eventSubscription, eventSubscription._handle.h) == MojoResult.kOk; 85 eventSubscription, eventSubscription._handle.h) ==
86 MojoResult.kOk;
86 } 87 }
87 88
88 static bool reportLeakedHandles() => MojoHandleNatives.reportOpenHandles(); 89 static bool reportLeakedHandles() => MojoHandleNatives.reportOpenHandles();
89 } 90 }
OLDNEW
« no previous file with comments | « mojo/dart/packages/mojo/lib/src/fill_data.dart ('k') | mojo/dart/packages/mojo/lib/src/message_pipe.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698