Skip to content

Commit 19fd204

Browse files
committed
Make c_raii move-only and add some caveat comments
That's all I plan to do on `c_raii` for now... onward to other things...
1 parent 76a3cca commit 19fd204

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

include/cpp2util.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,8 @@ class final_action_success
702702
}
703703
}
704704

705-
final_action_success(final_action_success&& other) noexcept
706-
: f(std::move(other.f)), invoke(std::exchange(other.invoke, false))
705+
final_action_success(final_action_success&& that) noexcept
706+
: f(std::move(that.f)), invoke(std::exchange(that.invoke, false))
707707
{ }
708708

709709
final_action_success(final_action_success const&) = delete;
@@ -760,9 +760,10 @@ auto to_string(...) -> std::string {
760760
//
761761
// As part of embracing compatibility while also reducing what we have to
762762
// teach and learn about C++ (which includes the C standard library), I
763-
// want to see if we can improve use of the C standard library from Cpp2
764-
// code... UFCS is a big part of that, and then RAII destructors is
763+
// was curious to see if we can improve use of the C standard library
764+
// from Cpp2 code... UFCS is a part of that, and then RAII destructors is
765765
// another that goes hand in hand with that, hence this section...
766+
// but see caveat note at the end.
766767
//
767768
//-----------------------------------------------------------------------
768769
//
@@ -777,18 +778,34 @@ class c_raii {
777778
, dtor{ [](void* x) { (D)(x); } }
778779
{ }
779780

780-
~c_raii() { dtor(t); }
781+
~c_raii() { if (dtor) dtor(t); }
781782

782783
operator T&() { return t; }
783784

784-
c_raii(c_raii const&) = delete;
785+
c_raii(c_raii const&) = delete;
785786
auto operator=(c_raii const&) = delete;
787+
c_raii(c_raii&& that) : t {std::move(that.t)}, dtor {that.dtor} { that.dtor = nullptr; }
788+
auto operator=(c_raii&& that) { t = std::move(that.t); dtor = that.dtor; that.dtor = nullptr; }
786789
};
787790

788791
auto fopen( const char* filename, const char* mode ) {
789-
return c_raii( std::fopen(filename, mode), &fclose );
792+
auto x = std::fopen(filename, mode);
793+
if (!x) {
794+
throw std::make_error_condition(std::errc::no_such_file_or_directory);
795+
}
796+
return c_raii( x, &fclose );
790797
}
791798

799+
// Caveat: There's little else in the C stdlib that allocates a resource...
800+
//
801+
// malloc is already wrapped like this via std::unique_ptr, which
802+
// typically uses malloc or gets memory from the same pool
803+
// thrd_create std::jthread is better
804+
//
805+
// ... is that it? I don't think it's useful to provide a c_raii just for fopen,
806+
// but perhaps c_raii may be useful for bringing forward third-party C code too,
807+
// with cpp2::fopen as a starting example.
808+
792809

793810
}
794811

source/sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ struct last_use {
175175
token const* t;
176176
bool is_forward;
177177
last_use( token const* t_, bool is_forward_ = false ) : t{t_}, is_forward{is_forward_} { }
178-
bool operator==(last_use const& other) { return t == other.t; }
178+
bool operator==(last_use const& that) { return t == that.t; }
179179
};
180180
std::vector<last_use> definite_last_uses;
181181

0 commit comments

Comments
 (0)