OLD | NEW |
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 // Functions to help with verifying various |Mojo...Options| structs from the | 5 // Functions to help with verifying various |Mojo...Options| structs from the |
6 // (public, C) API. These are "extensible" structs, which all have |struct_size| | 6 // (public, C) API. These are "extensible" structs, which all have |struct_size| |
7 // as their first member. All fields (other than |struct_size|) are optional, | 7 // as their first member. All fields (other than |struct_size|) are optional, |
8 // but any |flags| specified must be known to the system (otherwise, an error of | 8 // but any |flags| specified must be known to the system (otherwise, an error of |
9 // |MOJO_RESULT_UNIMPLEMENTED| should be returned). | 9 // |MOJO_RESULT_UNIMPLEMENTED| should be returned). |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 class UserOptionsReader { | 30 class UserOptionsReader { |
31 public: | 31 public: |
32 // Constructor from a |UserPointer<const Options>| (which it checks -- this | 32 // Constructor from a |UserPointer<const Options>| (which it checks -- this |
33 // constructor has side effects!). | 33 // constructor has side effects!). |
34 // Note: We initialize |options_reader_| without checking, since we do a check | 34 // Note: We initialize |options_reader_| without checking, since we do a check |
35 // in |GetSizeForReader()|. | 35 // in |GetSizeForReader()|. |
36 explicit UserOptionsReader(UserPointer<const Options> options) | 36 explicit UserOptionsReader(UserPointer<const Options> options) |
37 : options_reader_(UserPointer<const char>::Reader::NoCheck(), | 37 : options_reader_(UserPointer<const char>::Reader::NoCheck(), |
38 options.template ReinterpretCast<const char>(), | 38 options.template ReinterpretCast<const char>(), |
39 GetSizeForReader(options)) { | 39 GetSizeForReader(options)) { |
40 COMPILE_ASSERT(offsetof(Options, struct_size) == 0, | 40 static_assert(offsetof(Options, struct_size) == 0, |
41 Options_struct_size_not_first_member); | 41 "struct_size not first member of Options"); |
42 // TODO(vtl): With C++11, compile-assert that |sizeof(Options::struct_size) | 42 // TODO(vtl): Enable when MSVC supports this (C++11 extended sizeof): |
43 // == sizeof(uint32_t)| somewhere. | 43 // static_assert(sizeof(Options::struct_size) == sizeof(uint32_t), |
| 44 // "Options::struct_size not a uint32_t"); |
| 45 // (Or maybe assert that its type is uint32_t?) |
44 } | 46 } |
45 | 47 |
46 bool is_valid() const { return !!options_reader_.GetPointer(); } | 48 bool is_valid() const { return !!options_reader_.GetPointer(); } |
47 | 49 |
48 const Options& options() const { | 50 const Options& options() const { |
49 DCHECK(is_valid()); | 51 DCHECK(is_valid()); |
50 return *reinterpret_cast<const Options*>(options_reader_.GetPointer()); | 52 return *reinterpret_cast<const Options*>(options_reader_.GetPointer()); |
51 } | 53 } |
52 | 54 |
53 // Checks that the given (variable-size) |options| passed to the constructor | 55 // Checks that the given (variable-size) |options| passed to the constructor |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 // TODO(vtl): With C++11, use |sizeof(Options::member)| instead of (the | 94 // TODO(vtl): With C++11, use |sizeof(Options::member)| instead of (the |
93 // contortion below). We might also be able to pull out the type |Options| from | 95 // contortion below). We might also be able to pull out the type |Options| from |
94 // |reader| (using |decltype|) instead of requiring a parameter. | 96 // |reader| (using |decltype|) instead of requiring a parameter. |
95 #define OPTIONS_STRUCT_HAS_MEMBER(Options, member, reader) \ | 97 #define OPTIONS_STRUCT_HAS_MEMBER(Options, member, reader) \ |
96 reader.HasMember(offsetof(Options, member), sizeof(reader.options().member)) | 98 reader.HasMember(offsetof(Options, member), sizeof(reader.options().member)) |
97 | 99 |
98 } // namespace system | 100 } // namespace system |
99 } // namespace mojo | 101 } // namespace mojo |
100 | 102 |
101 #endif // MOJO_SYSTEM_OPTIONS_VALIDATION_H_ | 103 #endif // MOJO_SYSTEM_OPTIONS_VALIDATION_H_ |
OLD | NEW |