-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_base.hpp
105 lines (87 loc) · 2.16 KB
/
file_base.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#if !defined HDR_FILE_BASE_HPP
#define HDR_FILE_BASE_HPP
#include "handles.hpp"
#include "blob.hpp"
#include <tchar.h>
namespace w32
{
template < class handle >
struct file_base : handle
{
typedef DWORD size_type;
typedef handle handle_type;
typedef typename handle::traits_type traits_type;
file_base() : handle_type()
{
this->check();
}
file_base( typename handle_type::value_type h ) : handle_type( h )
{
this->check();
}
file_base( const handle_type& h ) : handle_type( h )
{
this->check();
}
template < class traits_other >
file_base( const handle_holder< typename handle::value_type, traits_other >& other )
: handle_type( other )
{
this->check();
}
const file_base& operator = ( const file_base& rhs )
{
this->handle_type::operator =( rhs );
return *this;
}
const file_base& operator = ( typename traits_type::value_type rhs )
{
this->handle_type::operator =( (rhs == INVALID_HANDLE_VALUE ? 0 : rhs) );
return *this;
}
size_type write( const void * data, size_type size, LPOVERLAPPED lpover = NULL ) const
{
size_type written = 0;
if ( this->valid() && size != 0 && data != 0 )
{
::WriteFile( this->value(), data, size, &written, lpover );
}
return written;
}
size_type read( void * buffer, size_type size, LPOVERLAPPED lpover = NULL ) const
{
size_type readed = 0;
BOOL ret_code = FALSE;
if ( this->valid() && buffer != NULL && size != 0 )
ret_code = ::ReadFile( this->value(), buffer, size, &readed, lpover );
return readed;
}
size_type write( const blob& b ) const
{
return this->write( b.data, b.size );
}
size_type read( const blob& b ) const
{
return this->read( b.data, b.size );
}
bool flush() const
{
return ( FALSE != ::FlushFileBuffers( this->value() ) );
}
void check()
{
if ( this->value() == INVALID_HANDLE_VALUE )
this->handle_ = traits_type::invalid_value;
}
//*
template < typename ptype >
bool create( const TCHAR * name, const ptype& params = ptype() )
{
this->attach( detail_create( name, params ) );
this->check();
return this->valid();
}
//*/
};
}
#endif /* HDR_FILE_BASE_HPP */