194
194
#include < optional>
195
195
#include < cstddef>
196
196
#include < utility>
197
+ #include < cstdio>
197
198
198
199
#if defined(CPP2_USE_SOURCE_LOCATION)
199
200
#include < source_location>
@@ -679,7 +680,7 @@ constexpr auto as( X const& x ) -> auto&&
679
680
// A variation of GSL's final_action_success and finally to run only on success
680
681
// (based on a PR I contributed to Microsoft GSL)
681
682
//
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
683
684
// if no exception is thrown
684
685
//
685
686
// finally_success is a convenience function to make a final_action_success_success
@@ -705,9 +706,9 @@ class final_action_success
705
706
: f(std::move(other.f)), invoke(std::exchange(other.invoke, false ))
706
707
{ }
707
708
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 ;
711
712
712
713
private:
713
714
F f;
@@ -753,6 +754,42 @@ auto to_string(...) -> std::string {
753
754
}
754
755
755
756
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
+
756
793
}
757
794
758
795
0 commit comments