一些乐趣。
namespace file {
template<auto f>
using constant = std::integral_constant< std::decay_t<decltype(f)>, f >;
using file_ptr = std::unique_ptr<std::FILE, constant<std::fclose>>;
FILE* unwrap( file_ptr const& ptr ) { return ptr.get(); }
template<class T> requires (!std::is_same_v< file_ptr, std::decay_t<T> >)
T&& unwrap( T&& t ) { return std::forward<T>(t); }
file_ptr wrap( FILE* ptr ) { return file_ptr(ptr); }
template<class T> requires (!std::is_same_v< file_ptr, std::decay_t<T> >)
T&& wrap( T&& t ) { return std::forward<T>(t); }
template<auto f>
auto call = [](auto&&...args){ return wrap( f( unwrap(decltype(args)(args))... ) ); };
// most FILE* operations can be rebound as follows:
auto open = call<std::fopen>;
auto close = call<std::fclose>;
auto getc = call<std::fgetc>;
auto putc = call<std::fputc>;
// any one that works with raw buffers (like std::fread or std::fwrite) needs more work
}
其核心是unique_ptr<FILE, thing that calls fclose> 是一个质量不错的FILE* C++ 包装器。
然后我添加了一些机制,通过包装/解包将fgetc 和类似功能映射到在file::file_ptr 上运行的功能。
现在你可以这样做了:
auto pFile = file::open("hello.txt", "r");
Live example.
auto pFile = file::open("hello.txt", "r");
auto pFileOut = file::open("world.txt", "w");
if (pFile)
{
while(auto c = file::getc(pFile))
{
file::putc(c, pFileOut);
}
}
更高级的版本会从函数指针中获取参数,将它们扔到一个元组中,将它们映射到来自 munged 参数的元组。
所以
char*, int
将映射到std::span<char>。
call 帮助器而不是上面的幼稚东西将采用 munged 参数,将它们映射到 C 样式参数的元组,将元组连接在一起,然后 std::apply C 函数。然后映射返回值。
call 稍加工作甚至可以有一个固定的签名而不是auto&&...,因此 IDE 可以给出提示。