1 // Copyright 2017 The Fuchsia 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 // This header provides functions which make it easier to work generically
6 // with string-like objects such as fbl::StringPiece, fbl::String, std::string,
7 // and std::string_view.
8 
9 #pragma once
10 
11 #include <stddef.h>
12 #include <type_traits>
13 
14 #include <fbl/macros.h>
15 
16 namespace fbl {
17 namespace internal {
18 DECLARE_HAS_MEMBER_FN_WITH_SIGNATURE(has_data, data, const char* (C::*)() const);
19 DECLARE_HAS_MEMBER_FN_WITH_SIGNATURE(has_length, length, size_t (C::*)() const);
20 } // namespace internal
21 
22 // Gets the character data from a string-like object.
23 template <typename T>
GetStringData(const T & value)24 constexpr const char* GetStringData(const T& value) {
25     return value.data();
26 }
27 
28 // Gets the length (in characters) of a string-like object.
29 template <typename T>
GetStringLength(const T & value)30 constexpr size_t GetStringLength(const T& value) {
31     return value.length();
32 }
33 
34 // is_string_like<T>
35 //
36 // Evaluates to true_type if GetStringData() and GetStringLength() are supported
37 // instances of type T.
38 template <typename T>
39 using is_string_like = std::integral_constant<bool,
40                                               internal::has_data<T>::value &&
41                                               internal::has_length<T>::value>;
42 
43 } // namespace fbl
44