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

Side by Side Diff: mojo/public/cpp/system/core.h

Issue 223213004: Mojo: Add an "overview" comment to mojo/public/cpp/system/core.h. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: oops Created 6 years, 8 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #ifndef MOJO_PUBLIC_CPP_SYSTEM_CORE_H_ 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_CORE_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_CORE_H_ 6 #define MOJO_PUBLIC_CPP_SYSTEM_CORE_H_
7 7
8 #include <assert.h> 8 #include <assert.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 10
11 #include <limits> 11 #include <limits>
12 12
13 #include "mojo/public/c/system/core.h" 13 #include "mojo/public/c/system/core.h"
14 #include "mojo/public/c/system/system_export.h" 14 #include "mojo/public/c/system/system_export.h"
15 #include "mojo/public/cpp/system/macros.h" 15 #include "mojo/public/cpp/system/macros.h"
16 16
17 namespace mojo { 17 namespace mojo {
18 18
19 // OVERVIEW
20 //
21 // |Handle| and |...Handle|:
22 //
23 // |Handle| is a simple, copyable wrapper for the C type |MojoHandle| (which is
24 // just an integer). Its purpose is to increase type-safety, not provide
25 // lifetime management. For the same purpose, we have trivial *subclasses* of
26 // |Handle|, e.g., |MessagePipeHandle| and |DataPipeProducerHandle|. |Handle|
27 // and its subclasses impose *no* extra overhead over using |MojoHandle|s
28 // directly.
29 //
30 // Note that though we provide constructors for |Handle|/|...Handle| from a
31 // |MojoHandle|, we do not provide, e.g., a constructor for |MessagePipeHandle|
32 // from a |Handle|. This is for type safety: If we did, you'd then be able to
33 // construct a |MessagePipeHandle| from, e.g., a |DataPipeProducerHandle| (since
34 // it's a |Handle|).
35 //
36 // |ScopedHandleBase| and |Scoped...Handle|:
37 //
38 // |ScopedHandleBase<HandleType>| is a templated scoped wrapper, for the handle
39 // types above (in the same sense that a C++11 |unique_ptr<T>| is a scoped
40 // wrapper for a |T*|). It provides lifetime management, closing its owned
41 // handle on destruction. It also provides (emulated) move semantics, again
42 // along the lines of C++11's |unique_ptr| (and exactly like Chromium's
43 // |scoped_ptr|).
44 //
45 // |ScopedHandle| is just (a typedef of) a |ScopedHandleBase<Handle>|.
46 // Similarly, |ScopedMessagePipeHandle| is just a
47 // |ScopedHandleBase<MessagePipeHandle>|. Etc. Note that a
48 // |ScopedMessagePipeHandle| is *not* a (subclass of) |ScopedHandle|.
49 //
50 // Wrapper functions:
51 //
52 // We provide simple wrappers for the |Mojo...()| functions (in
53 // mojo/public/c/system/core.h -- see that file for details on individual
54 // functions).
55 //
56 // The general guideline is functions that imply ownership transfer of a handle
57 // should take (or produce) an appropriate |Scoped...Handle|, while those that
58 // don't take a |...Handle|. For example, |CreateMessagePipe()| has two
59 // |ScopedMessagePipe| "out" parameters, whereas |Wait()| and |WaitMany()| take
60 // |Handle| parameters. Some, have both: e.g., |DuplicatedBuffer()| takes a
61 // suitable (unscoped) handle (e.g., |SharedBufferHandle|) "in" parameter and
62 // produces a suitable scoped handle (e.g., |ScopedSharedBufferHandle| a.k.a.
63 // |ScopedHandleBase<SharedBufferHandle>|) as an "out" parameter.
64 //
65 // An exception are some of the |...Raw()| functions. E.g., |CloseRaw()| takes a
66 // |Handle|, leaving the user to discard the handle.
67 //
68 // More significantly, |WriteMessageRaw()| exposes the full API complexity of
69 // |MojoWriteMessage()| (but doesn't require any extra overhead). It takes a raw
70 // array of |Handle|s as input, and takes ownership of them (i.e., invalidates
71 // them) on *success* (but not on failure). There are a number of reasons for
72 // this. First, C++03 |std::vector|s cannot contain the move-only
73 // |Scoped...Handle|s. Second, |std::vector|s impose extra overhead
74 // (necessitating heap-allocation of the buffer). Third, |std::vector|s wouldn't
75 // provide the desired level of flexibility/safety: a vector of handles would
76 // have to be all of the same type (probably |Handle|/|ScopedHandle|). Fourth,
77 // it's expected to not be used directly, but instead be used by generated
78 // bindings.
79 //
80 // Other |...Raw()| functions expose similar rough edges, e.g., dealing with raw
81 // pointers (and lengths) instead of taking |std::vector|s or similar.
82
19 // Standalone functions -------------------------------------------------------- 83 // Standalone functions --------------------------------------------------------
20 84
21 inline MojoTimeTicks GetTimeTicksNow() { 85 inline MojoTimeTicks GetTimeTicksNow() {
22 return MojoGetTimeTicksNow(); 86 return MojoGetTimeTicksNow();
23 } 87 }
24 88
25 // ScopedHandleBase ------------------------------------------------------------ 89 // ScopedHandleBase ------------------------------------------------------------
26 90
27 // Scoper for the actual handle types defined further below. It's move-only, 91 // Scoper for the actual handle types defined further below. It's move-only,
28 // like the C++11 |unique_ptr|. 92 // like the C++11 |unique_ptr|.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 return ScopedHandleBase<HandleType>(handle); 148 return ScopedHandleBase<HandleType>(handle);
85 } 149 }
86 150
87 // Handle ---------------------------------------------------------------------- 151 // Handle ----------------------------------------------------------------------
88 152
89 const MojoHandle kInvalidHandleValue = MOJO_HANDLE_INVALID; 153 const MojoHandle kInvalidHandleValue = MOJO_HANDLE_INVALID;
90 154
91 // Wrapper base class for |MojoHandle|. 155 // Wrapper base class for |MojoHandle|.
92 class Handle { 156 class Handle {
93 public: 157 public:
94 Handle() : value_(MOJO_HANDLE_INVALID) {} 158 Handle() : value_(kInvalidHandleValue) {}
95 explicit Handle(MojoHandle value) : value_(value) {} 159 explicit Handle(MojoHandle value) : value_(value) {}
96 ~Handle() {} 160 ~Handle() {}
97 161
98 void swap(Handle& other) { 162 void swap(Handle& other) {
99 MojoHandle temp = value_; 163 MojoHandle temp = value_;
100 value_ = other.value_; 164 value_ = other.value_;
101 other.value_ = temp; 165 other.value_ = temp;
102 } 166 }
103 167
104 bool is_valid() const { 168 bool is_valid() const {
105 return value_ != MOJO_HANDLE_INVALID; 169 return value_ != kInvalidHandleValue;
106 } 170 }
107 171
108 MojoHandle value() const { return value_; } 172 MojoHandle value() const { return value_; }
109 MojoHandle* mutable_value() { return &value_; } 173 MojoHandle* mutable_value() { return &value_; }
110 void set_value(MojoHandle value) { value_ = value; } 174 void set_value(MojoHandle value) { value_ = value; }
111 175
112 private: 176 private:
113 MojoHandle value_; 177 MojoHandle value_;
114 178
115 // Copying and assignment allowed. 179 // Copying and assignment allowed.
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 CreateSharedBuffer(&options, num_bytes, &handle); 522 CreateSharedBuffer(&options, num_bytes, &handle);
459 assert(result == MOJO_RESULT_OK); 523 assert(result == MOJO_RESULT_OK);
460 } 524 }
461 525
462 inline SharedBuffer::~SharedBuffer() { 526 inline SharedBuffer::~SharedBuffer() {
463 } 527 }
464 528
465 } // namespace mojo 529 } // namespace mojo
466 530
467 #endif // MOJO_PUBLIC_CPP_SYSTEM_CORE_H_ 531 #endif // MOJO_PUBLIC_CPP_SYSTEM_CORE_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698