MeshLib
 
Loading...
Searching...
No Matches
MRFile.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include <filesystem>
5#include <cstdio>
6
7namespace MR
8{
9
11MRMESH_API FILE * fopen( const std::filesystem::path & filename, const char * mode );
12
14class File
15{
16public:
17 File() = default;
18 File( const File & ) = delete;
19 File( File && r ) : handle_( r.handle_ ) { r.detach(); }
20 File( const std::filesystem::path & filename, const char * mode ) { open( filename, mode ); }
21 ~File() { close(); }
22
23 File& operator =( const File & ) = delete;
24 File& operator =( File && r ) { close(); handle_ = r.handle_; r.detach(); return * this; }
25
26 operator FILE *() const { return handle_; }
27
28 MRMESH_API FILE * open( const std::filesystem::path & filename, const char * mode );
30
32 void detach() { handle_ = nullptr; }
34 void attach( FILE * h ) { if ( handle_ != h ) { close(); handle_ = h; } }
35
36private:
37 FILE * handle_ = nullptr;
38};
39
40} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
the class to open C FILE handle and automatically close it in the destructor
Definition MRFile.h:15
File & operator=(const File &)=delete
void attach(FILE *h)
gives control over the handle to this object
Definition MRFile.h:34
MRMESH_API FILE * open(const std::filesystem::path &filename, const char *mode)
File(const File &)=delete
void detach()
the user takes control over the handle
Definition MRFile.h:32
~File()
Definition MRFile.h:21
MRMESH_API void close()
File(const std::filesystem::path &filename, const char *mode)
Definition MRFile.h:20
File()=default
File(File &&r)
Definition MRFile.h:19
Definition MRCameraOrientationPlugin.h:7
MRMESH_API FILE * fopen(const std::filesystem::path &filename, const char *mode)
this version of fopen unlike std::fopen supports unicode file names on Windows