Migrate to custom deleter type for unique_ptr 57/308757/1 accepted/tizen_unified accepted/tizen_unified_x tizen accepted/tizen/unified/20240402.151323 accepted/tizen/unified/20240402.163627 accepted/tizen/unified/x/20240403.102952
authorSlava Barinov <v.barinov@samsung.com>
Fri, 29 Mar 2024 13:52:32 +0000 (16:52 +0300)
committerSlava Barinov <v.barinov@samsung.com>
Fri, 29 Mar 2024 13:54:14 +0000 (16:54 +0300)
This prevents the error

 In file included from /home/abuild/rpmbuild/BUILD/trust-anchor-2.1.2/src/file-system.cpp:22:
 /home/abuild/rpmbuild/BUILD/trust-anchor-2.1.2/src/file-system.hxx:29:58: error: ignoring attributes on template argument 'int (*)(FILE*)' [-Werror=ignored-attributes]
    29 | using FilePtr = std::unique_ptr<FILE, decltype(&::fclose)>;
       |                                                          ^                    ^

When building with new toolchain

Change-Id: I4ef59f8a3c7d90e664f4fdd363cd385df980cd1d

src/certificate.cpp
src/file-system.cpp
src/file-system.hxx
tests/test-util.cpp

index 13a8f70..628f6ca 100644 (file)
@@ -51,7 +51,7 @@ Certificate::Certificate(const std::string &path) : m_path(path)
 
 std::string Certificate::getSubjectNameHash() const
 {
-       FilePtr fp = FilePtr(::fopen(this->m_path.c_str(), "rb"), ::fclose);
+       FilePtr fp = FilePtr(::fopen(this->m_path.c_str(), "rb"));
        if (fp == nullptr)
                throw std::invalid_argument("Failed to open [" + this->m_path + "].");
 
index 55783e0..82456b4 100644 (file)
@@ -40,7 +40,7 @@ void File::linkTo(const std::string &src, const std::string &dst)
 
 std::string File::read(const std::string &path)
 {
-       FilePtr fp = FilePtr(::fopen(path.c_str(), "rb"), ::fclose);
+       FilePtr fp = FilePtr(::fopen(path.c_str(), "rb"));
        if (fp == nullptr)
                throw std::invalid_argument("Failed to open [" + path + "].");
 
index ce1df38..1ca29c2 100644 (file)
 
 namespace tanchor {
 
-using FilePtr = std::unique_ptr<FILE, decltype(&::fclose)>;
+struct file_closer {
+  void operator() (FILE* f) const {
+    fclose(f);
+  }
+};
+
+using FilePtr = std::unique_ptr<FILE, file_closer>;
 
 class File {
 public:
index 11ddd33..5c28742 100644 (file)
 namespace test {
 namespace util {
 
+struct pipe_closer {
+  void operator() (FILE* f) const {
+    pclose(f);
+  }
+};
+
 std::string ls(const char *path)
 {
-       using FilePtr = std::unique_ptr<FILE, decltype(&::pclose)>;
+       using FilePtr = std::unique_ptr<FILE, pipe_closer>;
        std::string cmd("/bin/ls ");
        cmd.append(path);
 
-       FilePtr ls(::popen(cmd.c_str(), "r"), ::pclose);
+       FilePtr ls(::popen(cmd.c_str(), "r"));
        if (ls == nullptr)
                return std::string();
 
@@ -52,11 +58,11 @@ std::string ls(const char *path)
 
 std::string cat(const char *path)
 {
-       using FilePtr = std::unique_ptr<FILE, decltype(&::pclose)>;
+       using FilePtr = std::unique_ptr<FILE, pipe_closer>;
        std::string cmd("/bin/cat ");
        cmd.append(path);
 
-       FilePtr ls(::popen(cmd.c_str(), "r"), ::pclose);
+       FilePtr ls(::popen(cmd.c_str(), "r"));
        if (ls == nullptr)
                return std::string();