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 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ 7 8 #include "mojo/public/cpp/bindings/lib/template_util.h" 9 10 namespace mojo { 11 12 // This must be specialized for any type |T| to be serialized/deserialized as 13 // a mojom string. 14 // 15 // Imagine you want to specialize it for CustomString, usually you need to 16 // implement: 17 // 18 // template <T> 19 // struct StringTraits<CustomString> { 20 // // These two methods are optional. Please see comments in struct_traits.h 21 // static bool IsNull(const CustomString& input); 22 // static void SetToNull(CustomString* output); 23 // 24 // static size_t GetSize(const CustomString& input); 25 // static const char* GetData(const CustomString& input); 26 // 27 // // The caller guarantees that |!input.is_null()|. 28 // static bool Read(StringDataView input, CustomString* output); 29 // }; 30 // 31 // In some cases, you may need to do conversion before you can return the size 32 // and data as 8-bit characters for serialization. (For example, CustomString is 33 // UTF-16 string). In that case, you can add two optional methods: 34 // 35 // static void* SetUpContext(const CustomString& input); 36 // static void TearDownContext(const CustomString& input, void* context); 37 // 38 // And then you append a second parameter, void* context, to GetSize() and 39 // GetData(): 40 // 41 // static size_t GetSize(const CustomString& input, void* context); 42 // static const char* GetData(const CustomString& input, void* context); 43 // 44 // If a CustomString instance is not null, the serialization code will call 45 // SetUpContext() at the beginning, and pass the resulting context pointer to 46 // GetSize()/GetData(). After serialization is done, it calls TearDownContext() 47 // so that you can do any necessary cleanup. 48 // 49 template <typename T> 50 struct StringTraits { 51 static_assert(internal::AlwaysFalse<T>::value, 52 "Cannot find the mojo::StringTraits specialization. Did you " 53 "forget to include the corresponding header file?"); 54 }; 55 56 } // namespace mojo 57 58 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRING_TRAITS_H_ 59