Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/blob_serializer_deserializer.h
1 #ifndef SRC_BLOB_SERIALIZER_DESERIALIZER_H_ 2 #define SRC_BLOB_SERIALIZER_DESERIALIZER_H_ 3 4 #include <string> 5 #include <vector> 6 7 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 8 9 // This is related to the blob that is used in snapshots and single executable 10 // applications and has nothing to do with `node_blob.h`. 11 12 namespace node { 13 14 class BlobSerializerDeserializer { 15 public: 16 explicit BlobSerializerDeserializer(bool is_debug_v) : is_debug(is_debug_v) {} 17 18 template <typename... Args> 19 void Debug(const char* format, Args&&... args) const; 20 21 template <typename T> 22 std::string ToStr(const T& arg) const; 23 24 template <typename T> 25 std::string GetName() const; 26 27 bool is_debug = false; 28 }; 29 30 enum class StringLogMode { 31 kAddressOnly, // Can be used when the string contains binary content. 32 kAddressAndContent, 33 }; 34 35 // Child classes are expected to implement T Read<T>() where 36 // !std::is_arithmetic_v<T> && !std::is_same_v<T, std::string> 37 template <typename Impl> 38 class BlobDeserializer : public BlobSerializerDeserializer { 39 public: 40 explicit BlobDeserializer(bool is_debug_v, std::string_view s) 41 : BlobSerializerDeserializer(is_debug_v), sink(s) {} 42 ~BlobDeserializer() = default; 43 44 size_t read_total = 0; 45 std::string_view sink; 46 47 Impl* impl() { return static_cast<Impl*>(this); } 48 const Impl* impl() const { return static_cast<const Impl*>(this); } 49 50 // Helper for reading numeric types. 51 template <typename T> 52 T ReadArithmetic(); 53 54 // Layout of vectors: 55 // [ 4/8 bytes ] count 56 // [ ... ] contents (count * size of individual elements) 57 template <typename T> 58 std::vector<T> ReadVector(); 59 60 // ReadString() creates a copy of the data. ReadStringView() doesn't. 61 std::string ReadString(); 62 std::string_view ReadStringView(StringLogMode mode); 63 64 // Helper for reading an array of numeric types. 65 template <typename T> 66 void ReadArithmetic(T* out, size_t count); 67 68 // Helper for reading numeric vectors. 69 template <typename Number> 70 std::vector<Number> ReadArithmeticVector(size_t count); 71 72 private: 73 // Helper for reading non-numeric vectors. 74 template <typename T> 75 std::vector<T> ReadNonArithmeticVector(size_t count); 76 77 template <typename T> 78 T ReadElement(); 79 }; 80 81 // Child classes are expected to implement size_t Write<T>(const T&) where 82 // !std::is_arithmetic_v<T> && !std::is_same_v<T, std::string> 83 template <typename Impl> 84 class BlobSerializer : public BlobSerializerDeserializer { 85 public: 86 explicit BlobSerializer(bool is_debug_v) 87 : BlobSerializerDeserializer(is_debug_v) {} 88 ~BlobSerializer() = default; 89 90 Impl* impl() { return static_cast<Impl*>(this); } 91 const Impl* impl() const { return static_cast<const Impl*>(this); } 92 93 std::vector<char> sink; 94 95 // Helper for writing numeric types. 96 template <typename T> 97 size_t WriteArithmetic(const T& data); 98 99 // Layout of vectors: 100 // [ 4/8 bytes ] count 101 // [ ... ] contents (count * size of individual elements) 102 template <typename T> 103 size_t WriteVector(const std::vector<T>& data); 104 105 // The layout of a written string: 106 // [ 4/8 bytes ] length 107 // [ |length| bytes ] contents 108 size_t WriteStringView(std::string_view data, StringLogMode mode); 109 size_t WriteString(const std::string& data); 110 111 // Helper for writing an array of numeric types. 112 template <typename T> 113 size_t WriteArithmetic(const T* data, size_t count); 114 115 // Helper for writing numeric vectors. 116 template <typename Number> 117 size_t WriteArithmeticVector(const std::vector<Number>& data); 118 119 private: 120 // Helper for writing non-numeric vectors. 121 template <typename T> 122 size_t WriteNonArithmeticVector(const std::vector<T>& data); 123 124 template <typename T> 125 size_t WriteElement(const T& data); 126 }; 127 128 } // namespace node 129 130 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 131 132 #endif // SRC_BLOB_SERIALIZER_DESERIALIZER_H_