Skip to content

Commit d0706e1

Browse files
committed
Started adding a super-basic RAII wrapper for C-stdlib-style functions, closes hsutter#13
1 parent 4a86570 commit d0706e1

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

include/cpp2util.h

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
#include <optional>
195195
#include <cstddef>
196196
#include <utility>
197+
#include <cstdio>
197198

198199
#if defined(CPP2_USE_SOURCE_LOCATION)
199200
#include <source_location>
@@ -679,7 +680,7 @@ constexpr auto as( X const& x ) -> auto&&
679680
// A variation of GSL's final_action_success and finally to run only on success
680681
// (based on a PR I contributed to Microsoft GSL)
681682
//
682-
// final_action_success_success ensures something is run at the end of a scope
683+
// final_action_success ensures something is run at the end of a scope
683684
// if no exception is thrown
684685
//
685686
// finally_success is a convenience function to make a final_action_success_success
@@ -705,9 +706,9 @@ class final_action_success
705706
: f(std::move(other.f)), invoke(std::exchange(other.invoke, false))
706707
{ }
707708

708-
final_action_success(const final_action_success&) = delete;
709-
void operator=(const final_action_success&) = delete;
710-
void operator=(final_action_success&&) = delete;
709+
final_action_success(final_action_success const&) = delete;
710+
void operator= (final_action_success const&) = delete;
711+
void operator= (final_action_success&&) = delete;
711712

712713
private:
713714
F f;
@@ -753,6 +754,42 @@ auto to_string(...) -> std::string {
753754
}
754755

755756

757+
//-----------------------------------------------------------------------
758+
//
759+
// Speculative: RAII wrapping for the C standard library
760+
//
761+
// As part of embracing compatibility while also reducing what we have to
762+
// 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
765+
// another that goes hand in hand with that, hence this section...
766+
//
767+
//-----------------------------------------------------------------------
768+
//
769+
template<typename T>
770+
class c_raii {
771+
T t;
772+
void (*dtor)(void*);
773+
public:
774+
template<typename D>
775+
c_raii( T t_, D d )
776+
: t{ t_ }
777+
, dtor{ [](void* x) { (D)(x); } }
778+
{ }
779+
780+
~c_raii() { dtor(t); }
781+
782+
operator T&() { return t; }
783+
784+
c_raii(c_raii const&) = delete;
785+
auto operator=(c_raii const&) = delete;
786+
};
787+
788+
auto fopen( const char* filename, const char* mode ) {
789+
return c_raii( std::fopen(filename, mode), &fclose );
790+
}
791+
792+
756793
}
757794

758795

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
// "A better C than C" ... ?
3+
//
4+
main: () -> int = {
5+
s: std::string = "Freddy";
6+
myfile := cpp2::fopen("xyzzy", "w");
7+
myfile.fprintf( "Hello %s with UFCS!", s.c_str() );
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ----- Cpp2 support -----
2+
#define CPP2_USE_MODULES Yes
3+
#include "cpp2util.h"
4+
5+
6+
#line 4 "pure2-stdio-with-raii.cpp2"
7+
[[nodiscard]] auto main() -> int;
8+
9+
//=== Cpp2 definitions ==========================================================
10+
11+
#line 1 "pure2-stdio-with-raii.cpp2"
12+
13+
// "A better C than C" ... ?
14+
//
15+
[[nodiscard]] auto main() -> int{
16+
std::string s { "Freddy" };
17+
auto myfile { cpp2::fopen("xyzzy", "w") };
18+
CPP2_UFCS(fprintf, myfile, "Hello %s with UFCS!", CPP2_UFCS_0(c_str, s));
19+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-stdio-with-raii.cpp2... ok (all Cpp2, passes safety checks)
2+

0 commit comments

Comments
 (0)