Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/node_union_bytes.h
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 #include "v8.h" 8 9 namespace node { 10 11 // An external resource intended to be used with static lifetime. 12 template <typename Char, typename IChar, typename Base> 13 class StaticExternalByteResource : public Base { 14 static_assert(sizeof(IChar) == sizeof(Char), 15 "incompatible interface and internal pointers"); 16 17 public: 18 explicit StaticExternalByteResource(const Char* data, 19 size_t length, 20 std::shared_ptr<void> owning_ptr) 21 : data_(data), length_(length), owning_ptr_(owning_ptr) {} 22 23 const IChar* data() const override { 24 return reinterpret_cast<const IChar*>(data_); 25 } 26 size_t length() const override { return length_; } 27 28 void Dispose() override { 29 // We ignore Dispose calls from V8, even if we "own" a resource via 30 // owning_ptr_. All instantiations of this class are static or owned by a 31 // static map, and will be destructed when static variables are destructed. 32 } 33 34 StaticExternalByteResource(const StaticExternalByteResource&) = delete; 35 StaticExternalByteResource& operator=(const StaticExternalByteResource&) = 36 delete; 37 38 private: 39 const Char* data_; 40 const size_t length_; 41 std::shared_ptr<void> owning_ptr_; 42 }; 43 44 using StaticExternalOneByteResource = 45 StaticExternalByteResource<uint8_t, 46 char, 47 v8::String::ExternalOneByteStringResource>; 48 using StaticExternalTwoByteResource = 49 StaticExternalByteResource<uint16_t, 50 uint16_t, 51 v8::String::ExternalStringResource>; 52 53 // Similar to a v8::String, but it's independent from Isolates 54 // and can be materialized in Isolates as external Strings 55 // via ToStringChecked. 56 class UnionBytes { 57 public: 58 explicit UnionBytes(StaticExternalOneByteResource* one_byte_resource) 59 : one_byte_resource_(one_byte_resource), two_byte_resource_(nullptr) {} 60 explicit UnionBytes(StaticExternalTwoByteResource* two_byte_resource) 61 : one_byte_resource_(nullptr), two_byte_resource_(two_byte_resource) {} 62 63 UnionBytes(const UnionBytes&) = default; 64 UnionBytes& operator=(const UnionBytes&) = default; 65 UnionBytes(UnionBytes&&) = default; 66 UnionBytes& operator=(UnionBytes&&) = default; 67 68 bool is_one_byte() const { return one_byte_resource_ != nullptr; } 69 70 v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const; 71 72 private: 73 StaticExternalOneByteResource* one_byte_resource_; 74 StaticExternalTwoByteResource* two_byte_resource_; 75 }; 76 77 } // namespace node 78 79 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 80 81 #endif // SRC_NODE_UNION_BYTES_H_