From: Joel Rosdahl Date: Thu, 20 Feb 2020 20:05:56 +0000 (+0100) Subject: Add default Stat constructor X-Git-Tag: v4.0~592 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=65e7c9c4d1149bee26655a4b56603e628618f39b;p=thirdparty%2Fccache.git Add default Stat constructor --- diff --git a/src/Stat.hpp b/src/Stat.hpp index 7eb025101..19550c20b 100644 --- a/src/Stat.hpp +++ b/src/Stat.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019 Joel Rosdahl and other contributors +// Copyright (C) 2019-2020 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -38,6 +38,10 @@ public: throw_error, }; + // Create an empty stat result. operator bool() will return false, + // error_number() will return -1 and other accessors will return false or 0. + Stat(); + // Run stat(2). // // Arguments: @@ -83,6 +87,10 @@ private: int m_errno; }; +inline Stat::Stat() : m_stat{}, m_errno(-1) +{ +} + inline Stat Stat::stat(const std::string& path, OnError on_error) { diff --git a/unittest/test_Stat.cpp b/unittest/test_Stat.cpp index 5345c5047..9f5c6ad16 100644 --- a/unittest/test_Stat.cpp +++ b/unittest/test_Stat.cpp @@ -25,7 +25,24 @@ using Catch::Equals; -TEST_CASE("Constructor") +TEST_CASE("Default constructor") +{ + Stat stat; + CHECK(!stat); + CHECK(stat.error_number() == -1); + CHECK(stat.device() == 0); + CHECK(stat.inode() == 0); + CHECK(stat.mode() == 0); + CHECK(stat.ctime() == 0); + CHECK(stat.mtime() == 0); + CHECK(stat.size() == 0); + CHECK(stat.size_on_disk() == 0); + CHECK(!stat.is_directory()); + CHECK(!stat.is_regular()); + CHECK(!stat.is_symlink()); +} + +TEST_CASE("Named constructors") { CHECK(!Stat::stat("does_not_exist")); CHECK(!Stat::stat("does_not_exist", Stat::OnError::ignore));