Home | History | Annotate | Download | only in system
      1 // Copyright 2017 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_HANDLE_SIGNALS_STATE_H_
      6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_SIGNALS_STATE_H_
      7 
      8 #include "mojo/public/c/system/types.h"
      9 #include "mojo/public/cpp/system/system_export.h"
     10 
     11 namespace mojo {
     12 
     13 // A convenience wrapper around the MojoHandleSignalsState struct.
     14 struct MOJO_CPP_SYSTEM_EXPORT HandleSignalsState final
     15     : public MojoHandleSignalsState {
     16   HandleSignalsState() {
     17     satisfied_signals = MOJO_HANDLE_SIGNAL_NONE;
     18     satisfiable_signals = MOJO_HANDLE_SIGNAL_NONE;
     19   }
     20 
     21   HandleSignalsState(MojoHandleSignals satisfied,
     22                      MojoHandleSignals satisfiable) {
     23     satisfied_signals = satisfied;
     24     satisfiable_signals = satisfiable;
     25   }
     26 
     27   bool operator==(const HandleSignalsState& other) const {
     28     return satisfied_signals == other.satisfied_signals &&
     29            satisfiable_signals == other.satisfiable_signals;
     30   }
     31 
     32   // TODO(rockot): Remove this in favor of operator==.
     33   bool equals(const HandleSignalsState& other) const {
     34     return satisfied_signals == other.satisfied_signals &&
     35            satisfiable_signals == other.satisfiable_signals;
     36   }
     37 
     38   bool satisfies_any(MojoHandleSignals signals) const {
     39     return !!(satisfied_signals & signals);
     40   }
     41 
     42   bool satisfies_all(MojoHandleSignals signals) const {
     43     return (satisfied_signals & signals) == signals;
     44   }
     45 
     46   bool can_satisfy_any(MojoHandleSignals signals) const {
     47     return !!(satisfiable_signals & signals);
     48   }
     49 
     50   // The handle is currently readable. May apply to a message pipe handle or
     51   // data pipe consumer handle.
     52   bool readable() const { return satisfies_any(MOJO_HANDLE_SIGNAL_READABLE); }
     53 
     54   // The handle is currently writable. May apply to a message pipe handle or
     55   // data pipe producer handle.
     56   bool writable() const { return satisfies_any(MOJO_HANDLE_SIGNAL_WRITABLE); }
     57 
     58   // The handle's peer is closed. May apply to any message pipe or data pipe
     59   // handle.
     60   bool peer_closed() const {
     61     return satisfies_any(MOJO_HANDLE_SIGNAL_PEER_CLOSED);
     62   }
     63 
     64   // The handle's peer exists in a remote execution context (e.g. in another
     65   // process.)
     66   bool peer_remote() const {
     67     return satisfies_any(MOJO_HANDLE_SIGNAL_PEER_REMOTE);
     68   }
     69 
     70   // Indicates whether the handle has exceeded some quota limit.
     71   bool quota_exceeded() const {
     72     return satisfies_any(MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED);
     73   }
     74 
     75   // The handle will never be |readable()| again.
     76   bool never_readable() const {
     77     return !can_satisfy_any(MOJO_HANDLE_SIGNAL_READABLE);
     78   }
     79 
     80   // The handle will never be |writable()| again.
     81   bool never_writable() const {
     82     return !can_satisfy_any(MOJO_HANDLE_SIGNAL_WRITABLE);
     83   }
     84 
     85   // The handle can never indicate |peer_closed()|. Never true for message pipe
     86   // or data pipe handles (they can always signal peer closure), but always true
     87   // for other types of handles (they have no peer.)
     88   bool never_peer_closed() const {
     89     return !can_satisfy_any(MOJO_HANDLE_SIGNAL_PEER_CLOSED);
     90   }
     91 
     92   // THe handle will never indicate |peer_remote()| again. True iff the peer is
     93   // known to be closed.
     94   bool never_peer_remote() const {
     95     return !can_satisfy_any(MOJO_HANDLE_SIGNAL_PEER_REMOTE);
     96   }
     97 
     98   // (Copy and assignment allowed.)
     99 };
    100 
    101 static_assert(sizeof(HandleSignalsState) == sizeof(MojoHandleSignalsState),
    102               "HandleSignalsState should add no overhead");
    103 
    104 }  // namespace mojo
    105 
    106 #endif  // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_SIGNALS_STATE_H_
    107