• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #ifndef SRC_NODE_UNION_BYTES_H_
3 #define SRC_NODE_UNION_BYTES_H_
4 
5 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
6 
7 // A union of const uint8_t* or const uint16_t* data that can be
8 // turned into external v8::String when given an isolate.
9 
10 #include "v8.h"
11 
12 namespace node {
13 
14 // Similar to a v8::String, but it's independent from Isolates
15 // and can be materialized in Isolates as external Strings
16 // via ToStringChecked.
17 class UnionBytes {
18  public:
UnionBytes(const uint16_t * data,size_t length)19   UnionBytes(const uint16_t* data, size_t length)
20       : one_bytes_(nullptr), two_bytes_(data), length_(length) {}
UnionBytes(const uint8_t * data,size_t length)21   UnionBytes(const uint8_t* data, size_t length)
22       : one_bytes_(data), two_bytes_(nullptr), length_(length) {}
23   template <typename T>  // T = uint8_t or uint16_t
UnionBytes(std::shared_ptr<std::vector<T>> data)24   explicit UnionBytes(std::shared_ptr<std::vector</*const*/ T>> data)
25       : UnionBytes(data->data(), data->size()) {
26     owning_ptr_ = data;
27   }
28 
29   UnionBytes(const UnionBytes&) = default;
30   UnionBytes& operator=(const UnionBytes&) = default;
31   UnionBytes(UnionBytes&&) = default;
32   UnionBytes& operator=(UnionBytes&&) = default;
33 
is_one_byte()34   bool is_one_byte() const { return one_bytes_ != nullptr; }
two_bytes_data()35   const uint16_t* two_bytes_data() const {
36     CHECK_NOT_NULL(two_bytes_);
37     return two_bytes_;
38   }
one_bytes_data()39   const uint8_t* one_bytes_data() const {
40     CHECK_NOT_NULL(one_bytes_);
41     return one_bytes_;
42   }
43   v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const;
length()44   size_t length() const { return length_; }
45 
46  private:
47   const uint8_t* one_bytes_;
48   const uint16_t* two_bytes_;
49   size_t length_;
50   std::shared_ptr<void> owning_ptr_;
51 };
52 
53 }  // namespace node
54 
55 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
56 
57 #endif  // SRC_NODE_UNION_BYTES_H_
58