MCPcopy Create free account
hub / github.com/ElementsProject/elements / CVectorWriter

Class CVectorWriter

src/streams.h:73–130  ·  view source on GitHub ↗

Minimal stream for overwriting and/or appending to an existing byte vector * * The referenced vector will grow as necessary */

Source from the content-addressed store, hash-verified

71 * The referenced vector will grow as necessary
72 */
73class CVectorWriter
74{
75 public:
76
77/*
78 * @param[in] nTypeIn Serialization Type
79 * @param[in] nVersionIn Serialization Version (including any flags)
80 * @param[in] vchDataIn Referenced byte vector to overwrite/append
81 * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
82 * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
83*/
84 CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
85 {
86 if(nPos > vchData.size())
87 vchData.resize(nPos);
88 }
89/*
90 * (other params same as above)
91 * @param[in] args A list of items to serialize starting at nPosIn.
92*/
93 template <typename... Args>
94 CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
95 {
96 ::SerializeMany(*this, std::forward<Args>(args)...);
97 }
98 void write(Span<const std::byte> src)
99 {
100 assert(nPos <= vchData.size());
101 size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
102 if (nOverwrite) {
103 memcpy(vchData.data() + nPos, src.data(), nOverwrite);
104 }
105 if (nOverwrite < src.size()) {
106 vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.end()));
107 }
108 nPos += src.size();
109 }
110 template<typename T>
111 CVectorWriter& operator<<(const T& obj)
112 {
113 // Serialize to this stream
114 ::Serialize(*this, obj);
115 return (*this);
116 }
117 int GetVersion() const
118 {
119 return nVersion;
120 }
121 int GetType() const
122 {
123 return nType;
124 }
125private:
126 const int nType;
127 const int nVersion;
128 std::vector<unsigned char>& vchData;
129 size_t nPos;
130};

Callers 1

BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 1

SerializeFunction · 0.70

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68