| 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 "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_ |
| OLD | NEW |