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