OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_BINDINGS_STRING_TRAITS_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ |
7 | 7 |
| 8 #include "base/logging.h" |
| 9 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 10 |
8 namespace mojo { | 11 namespace mojo { |
9 | 12 |
| 13 // Access to a string inside a mojo message. |
| 14 class StringDataView { |
| 15 public: |
| 16 explicit StringDataView(internal::String_Data* data) : data_(data) {} |
| 17 |
| 18 // Whether the data represents a null string. |
| 19 // Note: Must not access the following methods if this method returns true. |
| 20 bool is_null() const { return !data_; } |
| 21 |
| 22 const char* storage() const { |
| 23 DCHECK(!is_null()); |
| 24 return data_->storage(); |
| 25 } |
| 26 |
| 27 size_t size() const { |
| 28 DCHECK(!is_null()); |
| 29 return data_->size(); |
| 30 } |
| 31 |
| 32 private: |
| 33 internal::String_Data* data_; |
| 34 }; |
| 35 |
10 // This must be specialized for any UserType to be serialized/deserialized as | 36 // This must be specialized for any UserType to be serialized/deserialized as |
11 // a mojom string. | 37 // a mojom string. |
12 // | 38 // |
13 // TODO(yzshen): This is work in progress. Add better documentation once the | 39 // TODO(yzshen): This is work in progress. Add better documentation once the |
14 // interface becomes more stable. | 40 // interface becomes more stable. |
15 template <typename UserType> | 41 template <typename UserType> |
16 struct StringTraits; | 42 struct StringTraits; |
17 | 43 |
18 } // namespace mojo | 44 } // namespace mojo |
19 | 45 |
20 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ | 46 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ |
OLD | NEW |