tensorflow::tstring is the scalar type for DT_STRING tensors. TODO(b/138799229): In order to ease migration from tensorflow::string to tensorflow::tstring, we define a simplified tstring class which wraps std::string. The API defined below is the expected subset of methods for tstring. The underlying implementation of tstring will be replaced with the one defined in [1] once the migration in te
| 47 | // |
| 48 | // [1] https://github.com/tensorflow/community/pull/91 |
| 49 | class tstring { |
| 50 | std::string str_; |
| 51 | |
| 52 | template <typename T, typename = void> |
| 53 | struct ResizeUninitialized { |
| 54 | static void Resize(T& s, size_t new_size) { s.resize(new_size); } |
| 55 | }; |
| 56 | |
| 57 | template <typename T> |
| 58 | struct ResizeUninitialized< |
| 59 | T, decltype(std::declval<T>().__resize_default_init(0))> { |
| 60 | static void Resize(T& s, size_t new_size) { |
| 61 | s.__resize_default_init(new_size); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | public: |
| 66 | tstring() = default; |
| 67 | |
| 68 | tstring(const tstring&) = default; |
| 69 | |
| 70 | tstring(const std::string& str) : str_(str) {} |
| 71 | |
| 72 | tstring(const char* str, size_t len) : str_(str, len) {} |
| 73 | |
| 74 | tstring(const char* str) : str_(str) {} |
| 75 | |
| 76 | explicit tstring(const StringPiece str) : tstring(str.data(), str.size()) {} |
| 77 | |
| 78 | template <typename T, |
| 79 | typename std::enable_if<std::is_same<T, absl::string_view>::value, |
| 80 | T>::type* = nullptr> |
| 81 | explicit tstring(const T& str) : str_(str.data(), str.size()) {} |
| 82 | |
| 83 | tstring(tstring&&) noexcept = default; |
| 84 | |
| 85 | ~tstring() = default; |
| 86 | |
| 87 | tstring& operator=(const tstring& str) = default; |
| 88 | |
| 89 | tstring& operator=(const StringPiece str) { |
| 90 | str_.assign(str.data(), str.size()); |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | tstring& operator=(const std::string& str) { |
| 95 | str_ = str; |
| 96 | |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | template <typename T, |
| 101 | typename std::enable_if<std::is_same<T, absl::string_view>::value, |
| 102 | T>::type* = nullptr> |
| 103 | tstring& operator=(const T& str) { |
| 104 | str_.assign(str.data(), str.size()); |
| 105 | |
| 106 | return *this; |