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

Side by Side Diff: mojo/public/cpp/bindings/string_traits.h

Issue 1966933002: Mojo C++ bindings: switch the existing usage of StructTraits to use the new data view interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@26_reader
Patch Set: typeid() is not allowed :/ Created 4 years, 7 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
OLDNEW
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698