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

Unified Diff: mojo/dart/packages/mojo/lib/src/types.dart

Issue 1439993003: Dart: Avoid MojoResult and MojoHandleSignals constructors. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Small fixes 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 side-by-side diff with in-line comments
Download patch
Index: mojo/dart/packages/mojo/lib/src/types.dart
diff --git a/mojo/dart/packages/mojo/lib/src/types.dart b/mojo/dart/packages/mojo/lib/src/types.dart
index bd6b642140b81bcb9cf587f3cdbd2ad2b2025ead..7488dd768c2a58bd20889edafa407c8af7a204dd 100644
--- a/mojo/dart/packages/mojo/lib/src/types.dart
+++ b/mojo/dart/packages/mojo/lib/src/types.dart
@@ -4,7 +4,7 @@
part of core;
-class MojoResult {
+abstract class MojoResult {
Cutch 2015/11/17 00:01:51 Why is this marked as abstract?
zra 2015/11/17 14:57:29 Instantiating a MojoResult no longer makes sense.
Cutch 2015/11/17 15:14:33 abstract class implies an interface with abstract
zra 2015/11/17 16:37:37 Done.
static const int kOk = 0;
static const int kCancelled = 1;
static const int kUnknown = 2;
@@ -24,72 +24,7 @@ class MojoResult {
static const int kBusy = 16;
static const int kShouldWait = 17;
- static const OK = const MojoResult._(kOk);
- static const CANCELLED = const MojoResult._(kCancelled);
- static const UNKNOWN = const MojoResult._(kUnknown);
- static const INVALID_ARGUMENT = const MojoResult._(kInvalidArgument);
- static const DEADLINE_EXCEEDED = const MojoResult._(kDeadlineExceeded);
- static const NOT_FOUND = const MojoResult._(kNotFound);
- static const ALREADY_EXISTS = const MojoResult._(kAlreadyExists);
- static const PERMISSION_DENIED = const MojoResult._(kPermissionDenied);
- static const RESOURCE_EXHAUSTED = const MojoResult._(kResourceExhausted);
- static const FAILED_PRECONDITION = const MojoResult._(kFailedPrecondition);
- static const ABORTED = const MojoResult._(kAborted);
- static const OUT_OF_RANGE = const MojoResult._(kOutOfRange);
- static const UNIMPLEMENTED = const MojoResult._(kUnimplemented);
- static const INTERNAL = const MojoResult._(kInternal);
- static const UNAVAILABLE = const MojoResult._(kUnavailable);
- static const DATA_LOSS = const MojoResult._(kDataLoss);
- static const BUSY = const MojoResult._(kBusy);
- static const SHOULD_WAIT = const MojoResult._(kShouldWait);
-
- static const _values = const <MojoResult>[
- OK,
- CANCELLED,
- UNKNOWN,
- INVALID_ARGUMENT,
- DEADLINE_EXCEEDED,
- NOT_FOUND,
- ALREADY_EXISTS,
- PERMISSION_DENIED,
- RESOURCE_EXHAUSTED,
- FAILED_PRECONDITION,
- ABORTED,
- OUT_OF_RANGE,
- UNIMPLEMENTED,
- INTERNAL,
- UNAVAILABLE,
- DATA_LOSS,
- BUSY,
- SHOULD_WAIT,
- ];
-
- final int value;
-
- const MojoResult._(this.value);
-
- factory MojoResult(int value) => _values[value];
-
- bool get isOk => (this == OK);
- bool get isCancelled => (this == CANCELLED);
- bool get isUnknown => (this == UNKNOWN);
- bool get isInvalidArgument => (this == INVALID_ARGUMENT);
- bool get isDeadlineExceeded => (this == DEADLINE_EXCEEDED);
- bool get isNotFound => (this == NOT_FOUND);
- bool get isAlreadExists => (this == ALREADY_EXISTS);
- bool get isPermissionDenied => (this == PERMISSION_DENIED);
- bool get isResourceExhausted => (this == RESOURCE_EXHAUSTED);
- bool get isFailedPrecondition => (this == FAILED_PRECONDITION);
- bool get isAborted => (this == ABORTED);
- bool get isOutOfRange => (this == OUT_OF_RANGE);
- bool get isUnimplemented => (this == UNIMPLEMENTED);
- bool get isInternal => (this == INTERNAL);
- bool get isUnavailable => (this == UNAVAILABLE);
- bool get isDataLoss => (this == DATA_LOSS);
- bool get isBusy => (this == BUSY);
- bool get isShouldWait => (this == SHOULD_WAIT);
-
- String toString() {
+ static String string(int value) {
switch (value) {
case kOk:
return "OK";
@@ -133,7 +68,7 @@ class MojoResult {
}
}
-class MojoHandleSignals {
+abstract class MojoHandleSignals {
Cutch 2015/11/17 00:01:51 ditto
zra 2015/11/17 14:57:29 Same reason.
static const int kNone = 0x0;
static const int kReadable = 0x1;
static const int kWritable = 0x2;
@@ -144,61 +79,29 @@ class MojoHandleSignals {
static const int kAll = 0x7;
static const int kBitfieldSize = 3;
- static const NONE = const MojoHandleSignals._(kNone);
- static const READABLE = const MojoHandleSignals._(kReadable);
- static const WRITABLE = const MojoHandleSignals._(kWritable);
- static const PEER_CLOSED = const MojoHandleSignals._(kPeerClosed);
- static const PEER_CLOSED_READABLE =
- const MojoHandleSignals._(kPeerClosedReadable);
- static const READWRITE = const MojoHandleSignals._(kReadWrite);
- static const PEER_CLOSED_WRITABLE =
- const MojoHandleSignals._(kPeerClosedWritable);
- static const ALL = const MojoHandleSignals._(kAll);
-
- static const _values = const <MojoHandleSignals>[
- NONE, // 0
- READABLE, // 1
- WRITABLE, // 2
- READWRITE, // 3
- PEER_CLOSED, // 4
- PEER_CLOSED_READABLE, // 5
- PEER_CLOSED_WRITABLE, // 6
- ALL, // 7
- ];
-
- final int value;
-
- const MojoHandleSignals._(this.value);
-
- factory MojoHandleSignals(int value) => _values[value];
-
- bool get isNone => (this == NONE);
- bool get isReadable => (value & kReadable) == kReadable;
- bool get isWritable => (value & kWritable) == kWritable;
- bool get isPeerClosed => (value & kPeerClosed) == kPeerClosed;
- bool get isReadWrite => (value & kReadWrite) == kReadWrite;
- bool get isAll => (this == ALL);
- bool get isValid => (value & kAll) == value;
-
- MojoHandleSignals operator +(MojoHandleSignals other) {
- return new MojoHandleSignals(value | other.value);
- }
-
- MojoHandleSignals operator -(MojoHandleSignals other) {
- return new MojoHandleSignals(value & ~other.value);
- }
-
- String toString() {
- if (isNone) {
+ static bool isNone(int v) => v == 0;
+ static bool isReadable(int v) => (v & kReadable) == kReadable;
+ static bool isWritable(int v) => (v & kWritable) == kWritable;
+ static bool isReadWrite(int v) => (v & kReadWrite) == kReadWrite;
+ static bool isPeerClosed(int v) => (v & kPeerClosed) == kPeerClosed;
+ static bool isPeerClosedReadable(int v) =>
+ (v & kPeerClosedReadable) == kPeerClosedReadable;
+ static bool isPeerClosedWritable(int v) =>
+ (v & kPeerClosedWritable) == kPeerClosedWritable;
+ static bool isAll(int v) => (v & kAll) == kAll;
+ static bool isValid(int v) => (v & kAll) == v;
+
+ static String string(int value) {
+ if (value == kNone) {
return "(None)";
}
- if (!isValid) {
+ if (!isValid(value)) {
return "(INVALID)";
}
List<String> signals = [];
- if (isReadable) signals.add("Readable");
- if (isWritable) signals.add("Writable");
- if (isPeerClosed) signals.add("PeerClosed");
+ if (value == kReadable) signals.add("Readable");
+ if (value == kWritable) signals.add("Writable");
+ if (value == kPeerClosed) signals.add("PeerClosed");
return "(" + signals.join(", ") + ")";
}
}
@@ -207,25 +110,31 @@ class MojoHandleSignalsState {
MojoHandleSignalsState(this.satisfied_signals, this.satisfiable_signals);
final int satisfied_signals;
final int satisfiable_signals;
- String toString() => (new MojoHandleSignals(satisfied_signals)).toString();
+ String toString() => MojoHandleSignals.string(satisfied_signals);
}
class MojoWaitResult {
MojoWaitResult(this.result, this.state);
- final MojoResult result;
+ final int result;
MojoHandleSignalsState state;
- String toString() => "MojoWaitResult(result: $result, state: $state)";
+ String toString() {
+ String r = MojoResult.string(result);
+ return "MojoWaitResult(result: $r, state: $state)";
+ }
}
class MojoWaitManyResult {
MojoWaitManyResult(this.result, this.index, this.states);
- final MojoResult result;
+ final int result;
final int index;
List<MojoHandleSignalsState> states;
bool get isIndexValid => (this.index != null);
bool get areSignalStatesValid => (this.states != null);
- String toString() =>
- "MojoWaitManyResult(" "result: $result, idx: $index, state: ${states[index]})";
+ String toString() {
+ String r = MojoResult.string(result);
+ return "MojoWaitManyResult(result: $r, idx: $index, "
+ "state: ${states[index]})";
+ }
}

Powered by Google App Engine
This is Rietveld 408576698