summaryrefslogtreecommitdiff
path: root/libs/vr/libpdx/private/pdx/rpc/string_wrapper.h
blob: 2d0a4ea6ec3ad0e14cfb81a1379d08f3f9d1519e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef ANDROID_PDX_RPC_STRING_WRAPPER_H_
#define ANDROID_PDX_RPC_STRING_WRAPPER_H_

#include <cstddef>
#include <cstring>
#include <string>
#include <type_traits>

namespace android {
namespace pdx {
namespace rpc {

// Wrapper class for C string buffers, providing an interface suitable for
// SerializeObject and DeserializeObject. This class serializes to the same
// format as std::basic_string, and may be substituted for std::basic_string
// during serialization and deserialization. This substitution makes handling of
// C strings more efficient by avoiding unnecessary copies when remote method
// signatures specify std::basic_string arguments or return values.
template <typename CharT = std::string::value_type,
          typename Traits = std::char_traits<CharT>>
class StringWrapper {
 public:
  // Define types in the style of STL strings to support STL operators.
  typedef Traits traits_type;
  typedef typename Traits::char_type value_type;
  typedef std::size_t size_type;
  typedef value_type& reference;
  typedef const value_type& const_reference;
  typedef value_type* pointer;
  typedef const value_type* const_pointer;

  StringWrapper() : buffer_(nullptr), capacity_(0), end_(0) {}

  StringWrapper(pointer buffer, size_type capacity, size_type size)
      : buffer_(&buffer[0]),
        capacity_(capacity),
        end_(capacity < size ? capacity : size) {}

  StringWrapper(pointer buffer, size_type size)
      : StringWrapper(buffer, size, size) {}

  explicit StringWrapper(pointer buffer)
      : StringWrapper(buffer, std::strlen(buffer)) {}

  StringWrapper(const StringWrapper& other) { *this = other; }

  StringWrapper(StringWrapper&& other) noexcept { *this = std::move(other); }

  StringWrapper& operator=(const StringWrapper& other) {
    if (&other == this) {
      return *this;
    } else {
      buffer_ = other.buffer_;
      capacity_ = other.capacity_;
      end_ = other.end_;
    }

    return *this;
  }

  StringWrapper& operator=(StringWrapper&& other) noexcept {
    if (&other == this) {
      return *this;
    } else {
      buffer_ = other.buffer_;
      capacity_ = other.capacity_;
      end_ = other.end_;
      other.buffer_ = nullptr;
      other.capacity_ = 0;
      other.end_ = 0;
    }

    return *this;
  }

  pointer data() { return buffer_; }
  const_pointer data() const { return buffer_; }

  pointer begin() { return &buffer_[0]; }
  pointer end() { return &buffer_[end_]; }
  const_pointer begin() const { return &buffer_[0]; }
  const_pointer end() const { return &buffer_[end_]; }

  size_type size() const { return end_; }
  size_type length() const { return end_; }
  size_type max_size() const { return capacity_; }
  size_type capacity() const { return capacity_; }

  void resize(size_type size) {
    if (size <= capacity_)
      end_ = size;
    else
      end_ = capacity_;
  }

  reference operator[](size_type pos) { return buffer_[pos]; }
  const_reference operator[](size_type pos) const { return buffer_[pos]; }

 private:
  pointer buffer_;
  size_type capacity_;
  size_type end_;
};

// Utility functions that infer the underlying type of the string, simplifying
// the wrapper interface.

// TODO(eieio): Wrapping std::basic_string is here for completeness, but is it
// useful?
template <typename T, typename... Any>
StringWrapper<const T> WrapString(const std::basic_string<T, Any...>& s) {
  return StringWrapper<const T>(s.c_str(), s.length());
}

template <typename T, typename SizeType = std::size_t>
StringWrapper<T> WrapString(T* s, SizeType size) {
  return StringWrapper<T>(s, size);
}

template <typename T>
StringWrapper<T> WrapString(T* s) {
  return StringWrapper<T>(s);
}

}  // namespace rpc
}  // namespace pdx
}  // namespace android

#endif  // ANDROID_PDX_RPC_STRING_WRAPPER_H_