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

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

Issue 2165233003: Mojo C++ bindings: provide data view for all object types. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 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
« no previous file with comments | « mojo/public/cpp/bindings/string_data_view.h ('k') | mojo/public/cpp/bindings/struct_traits.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "mojo/public/cpp/bindings/string_data_view.h"
9 #include "mojo/public/cpp/bindings/lib/array_internal.h"
10 9
11 namespace mojo { 10 namespace mojo {
12 11
13 // Access to the contents of a serialized string.
14 class StringDataView {
15 public:
16 explicit StringDataView(internal::String_Data* data) : data_(data) {
17 DCHECK(data_);
18 }
19
20 const char* storage() const { return data_->storage(); }
21
22 size_t size() const { return data_->size(); }
23
24 private:
25 internal::String_Data* data_;
26 };
27
28 // This must be specialized for any type |T| to be serialized/deserialized as 12 // This must be specialized for any type |T| to be serialized/deserialized as
29 // a mojom string. 13 // a mojom string.
30 // 14 //
31 // Imagine you want to specialize it for CustomString, usually you need to 15 // Imagine you want to specialize it for CustomString, usually you need to
32 // implement: 16 // implement:
33 // 17 //
34 // template <T> 18 // template <T>
35 // struct StringTraits<CustomString> { 19 // struct StringTraits<CustomString> {
36 // // These two methods are optional. Please see comments in struct_traits.h 20 // // These two methods are optional. Please see comments in struct_traits.h
37 // static bool IsNull(const CustomString& input); 21 // static bool IsNull(const CustomString& input);
38 // static void SetToNull(CustomString* output); 22 // static void SetToNull(CustomString* output);
39 // 23 //
40 // static size_t GetSize(const CustomString& input); 24 // static size_t GetSize(const CustomString& input);
41 // static const char* GetData(const CustomString& input); 25 // static const char* GetData(const CustomString& input);
42 // 26 //
27 // // The caller guarantees that |!input.is_null()|.
43 // static bool Read(StringDataView input, CustomString* output); 28 // static bool Read(StringDataView input, CustomString* output);
44 // }; 29 // };
45 // 30 //
46 // In some cases, you may need to do conversion before you can return the size 31 // In some cases, you may need to do conversion before you can return the size
47 // and data as 8-bit characters for serialization. (For example, CustomString is 32 // and data as 8-bit characters for serialization. (For example, CustomString is
48 // UTF-16 string). In that case, you can add two optional methods: 33 // UTF-16 string). In that case, you can add two optional methods:
49 // 34 //
50 // static void* SetUpContext(const CustomString& input); 35 // static void* SetUpContext(const CustomString& input);
51 // static void TearDownContext(const CustomString& input, void* context); 36 // static void TearDownContext(const CustomString& input, void* context);
52 // 37 //
53 // And then you append a second parameter, void* context, to GetSize() and 38 // And then you append a second parameter, void* context, to GetSize() and
54 // GetData(): 39 // GetData():
55 // 40 //
56 // static size_t GetSize(const CustomString& input, void* context); 41 // static size_t GetSize(const CustomString& input, void* context);
57 // static const char* GetData(const CustomString& input, void* context); 42 // static const char* GetData(const CustomString& input, void* context);
58 // 43 //
59 // If a CustomString instance is not null, the serialization code will call 44 // If a CustomString instance is not null, the serialization code will call
60 // SetUpContext() at the beginning, and pass the resulting context pointer to 45 // SetUpContext() at the beginning, and pass the resulting context pointer to
61 // GetSize()/GetData(). After serialization is done, it calls TearDownContext() 46 // GetSize()/GetData(). After serialization is done, it calls TearDownContext()
62 // so that you can do any necessary cleanup. 47 // so that you can do any necessary cleanup.
63 // 48 //
64 template <typename T> 49 template <typename T>
65 struct StringTraits; 50 struct StringTraits;
66 51
67 } // namespace mojo 52 } // namespace mojo
68 53
69 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ 54 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/string_data_view.h ('k') | mojo/public/cpp/bindings/struct_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698