Skip to content

Commit 6666803

Browse files
author
MacroFake
committed
streams: Add AutoFile without ser-type and ser-version
The moved parts can be reviewed with "--color-moved=dimmed-zebra". The one-char changes can be reviewed with "--word-diff-regex=.".
1 parent e4e201d commit 6666803

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

src/streams.h

+42-27
Original file line numberDiff line numberDiff line change
@@ -465,35 +465,28 @@ class BitStreamWriter
465465
};
466466

467467

468-
469468
/** Non-refcounted RAII wrapper for FILE*
470469
*
471470
* Will automatically close the file when it goes out of scope if not null.
472471
* If you're returning the file pointer, return file.release().
473472
* If you need to close the file early, use file.fclose() instead of fclose(file).
474473
*/
475-
class CAutoFile
474+
class AutoFile
476475
{
477-
private:
478-
const int nType;
479-
const int nVersion;
480-
476+
protected:
481477
FILE* file;
482478

483479
public:
484-
CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
485-
{
486-
file = filenew;
487-
}
480+
explicit AutoFile(FILE* filenew) : file{filenew} {}
488481

489-
~CAutoFile()
482+
~AutoFile()
490483
{
491484
fclose();
492485
}
493486

494487
// Disallow copies
495-
CAutoFile(const CAutoFile&) = delete;
496-
CAutoFile& operator=(const CAutoFile&) = delete;
488+
AutoFile(const AutoFile&) = delete;
489+
AutoFile& operator=(const AutoFile&) = delete;
497490

498491
void fclose()
499492
{
@@ -504,14 +497,14 @@ class CAutoFile
504497
}
505498

506499
/** Get wrapped FILE* with transfer of ownership.
507-
* @note This will invalidate the CAutoFile object, and makes it the responsibility of the caller
500+
* @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
508501
* of this function to clean up the returned FILE*.
509502
*/
510503
FILE* release() { FILE* ret = file; file = nullptr; return ret; }
511504

512505
/** Get wrapped FILE* without transfer of ownership.
513506
* @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
514-
* CAutoFile outlives use of the passed pointer.
507+
* AutoFile outlives use of the passed pointer.
515508
*/
516509
FILE* Get() const { return file; }
517510

@@ -522,40 +515,62 @@ class CAutoFile
522515
//
523516
// Stream subset
524517
//
525-
int GetType() const { return nType; }
526-
int GetVersion() const { return nVersion; }
527-
528518
void read(Span<std::byte> dst)
529519
{
530-
if (!file)
531-
throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
520+
if (!file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
532521
if (fread(dst.data(), 1, dst.size(), file) != dst.size()) {
533-
throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
522+
throw std::ios_base::failure(feof(file) ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
534523
}
535524
}
536525

537526
void ignore(size_t nSize)
538527
{
539-
if (!file)
540-
throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
528+
if (!file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
541529
unsigned char data[4096];
542530
while (nSize > 0) {
543531
size_t nNow = std::min<size_t>(nSize, sizeof(data));
544532
if (fread(data, 1, nNow, file) != nNow)
545-
throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
533+
throw std::ios_base::failure(feof(file) ? "AutoFile::ignore: end of file" : "AutoFile::read: fread failed");
546534
nSize -= nNow;
547535
}
548536
}
549537

550538
void write(Span<const std::byte> src)
551539
{
552-
if (!file)
553-
throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
540+
if (!file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
554541
if (fwrite(src.data(), 1, src.size(), file) != src.size()) {
555-
throw std::ios_base::failure("CAutoFile::write: write failed");
542+
throw std::ios_base::failure("AutoFile::write: write failed");
556543
}
557544
}
558545

546+
template <typename T>
547+
AutoFile& operator<<(const T& obj)
548+
{
549+
if (!file) throw std::ios_base::failure("AutoFile::operator<<: file handle is nullptr");
550+
::Serialize(*this, obj);
551+
return *this;
552+
}
553+
554+
template <typename T>
555+
AutoFile& operator>>(T&& obj)
556+
{
557+
if (!file) throw std::ios_base::failure("AutoFile::operator>>: file handle is nullptr");
558+
::Unserialize(*this, obj);
559+
return *this;
560+
}
561+
};
562+
563+
class CAutoFile : public AutoFile
564+
{
565+
private:
566+
const int nType;
567+
const int nVersion;
568+
569+
public:
570+
CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : AutoFile{filenew}, nType(nTypeIn), nVersion(nVersionIn) {}
571+
int GetType() const { return nType; }
572+
int GetVersion() const { return nVersion; }
573+
559574
template<typename T>
560575
CAutoFile& operator<<(const T& obj)
561576
{

test/functional/feature_addrman.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def run_test(self):
9595
with open(peers_dat, "wb") as f:
9696
f.write(serialize_addrman()[:-1])
9797
self.nodes[0].assert_start_raises_init_error(
98-
expected_msg=init_error("CAutoFile::read: end of file.*"),
98+
expected_msg=init_error("AutoFile::read: end of file.*"),
9999
match=ErrorMatch.FULL_REGEX,
100100
)
101101

0 commit comments

Comments
 (0)