-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.h
1724 lines (1524 loc) · 44.2 KB
/
file.h
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <limits>
#include <memory>
#include <stdexcept>
#include <string_view>
#include <system_error>
#include <type_traits>
#include <utility>
#include <vector>
#if __has_include(<windows.h>)
#include <windows.h>
#define ZPP_FILE_WINDOWS
#else
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif
namespace zpp
{
#if !__has_include("zpp/byte_view.h")
/**
* Represents a view of bytes.
*/
template <typename ByteType>
class basic_byte_view
{
public:
// Check that the underlying type is either char, unsigned char, or
// std::byte.
static_assert(
std::is_same_v<std::remove_cv_t<ByteType>, char> ||
std::is_same_v<std::remove_cv_t<ByteType>, unsigned char> ||
std::is_same_v<std::remove_cv_t<ByteType>, std::byte>,
"Byte type must either be char, unsigned char, or std::byte.");
/**
* Type definition.
* @{
*/
using value_type = ByteType;
using const_value_type = std::add_const_t<value_type>;
using reference = std::add_lvalue_reference_t<value_type>;
using const_reference = std::add_lvalue_reference_t<const_value_type>;
using pointer = std::add_pointer_t<value_type>;
using const_pointer = std::add_pointer_t<const_value_type>;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using index_type = std::size_t;
/**
* @}
*/
/**
* Construct an empty byte view.
*/
constexpr basic_byte_view() noexcept = default;
/**
* Construction of byte view for same byte ranges.
* @{
*/
constexpr basic_byte_view(pointer begin, index_type count) noexcept :
m_data(begin),
m_size(count)
{
}
constexpr basic_byte_view(pointer begin, pointer end) noexcept :
basic_byte_view(begin, end - begin)
{
}
/**
* @}
*/
/**
* Construction of byte view from different byte ranges.
* @{
*/
template <typename Pointer,
// Make sure other pointer is not pointing to the same type
// current byte view type.
typename = std::enable_if_t<!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::remove_cv_t<value_type>>>,
// Make sure the other pointer points to a byte type.
typename = std::enable_if_t<
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
char> ||
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
unsigned char> ||
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::byte>>>
basic_byte_view(Pointer begin, index_type count) noexcept :
m_data(reinterpret_cast<pointer>(begin)),
m_size(count)
{
}
template <typename Pointer,
// Make sure other pointer is not pointing to the same type
// current byte view type.
typename = std::enable_if_t<!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::remove_cv_t<value_type>>>,
// Make sure the other pointer points to a byte type.
typename = std::enable_if_t<
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
char> ||
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
unsigned char> ||
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::byte>>>
basic_byte_view(Pointer begin, Pointer end) noexcept :
basic_byte_view(begin, end - begin)
{
}
/**
* @}
*/
/**
* Explicit construction of byte views of non-byte ranges.
* @{
*/
template <typename Pointer,
// Make sure the other pointer is not a byte type.
typename = std::enable_if_t<
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
char> &&
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
unsigned char> &&
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::byte>>>
explicit basic_byte_view(Pointer begin, index_type count) noexcept :
m_data(reinterpret_cast<pointer>(begin)),
m_size(count * sizeof(*begin))
{
}
template <typename Pointer,
// Make sure the other pointer is not a byte type.
typename = std::enable_if_t<
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
char> &&
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
unsigned char> &&
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::byte>>>
explicit basic_byte_view(Pointer begin, Pointer end) noexcept :
basic_byte_view(begin, end - begin)
{
}
/**
* @}
*/
/**
* Explicit construction of byte views from void pointer ranges.
* @{
*/
template <typename Pointer,
// Make sure the other pointer is void type.
typename = std::enable_if_t<std::is_void_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>>>,
typename = void,
typename = void>
explicit basic_byte_view(Pointer begin, index_type count) noexcept :
m_data(static_cast<pointer>(begin)),
m_size(count)
{
}
template <typename Pointer,
// Make sure the other pointer is void type.
typename = std::enable_if_t<std::is_void_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>>>,
typename = void,
typename = void,
typename = void>
explicit basic_byte_view(Pointer begin, Pointer end) noexcept :
basic_byte_view(
begin, Pointer(std::uintptr_t(end) - std::uintptr_t(begin)))
{
}
/**
* @}
*/
/**
* Construction from other byte views whose underlying type is
* convertible (CV qualification).
* @{
*/
template <typename OtherByteType,
typename = std::enable_if_t<
std::is_convertible_v<std::add_pointer_t<OtherByteType>,
pointer>>>
constexpr basic_byte_view(
const basic_byte_view<OtherByteType> & other) noexcept :
m_data(other.m_data),
m_size(other.m_size)
{
}
template <typename OtherByteType,
typename = std::enable_if_t<
std::is_convertible_v<std::add_pointer_t<OtherByteType>,
pointer>>>
constexpr basic_byte_view(
basic_byte_view<OtherByteType> && other) noexcept :
m_data(other.m_data),
m_size(other.m_size)
{
}
/**
* @}
*/
/**
* Construct from other byte views with non convertible underlying
* type.
*/
template <typename OtherByteType,
typename = std::enable_if_t<
!std::is_convertible_v<std::add_pointer_t<OtherByteType>,
pointer>>,
typename = void>
basic_byte_view(
const basic_byte_view<OtherByteType> & other) noexcept :
m_data(reinterpret_cast<pointer>(other.m_data)),
m_size(other.m_size)
{
}
/**
* Construct from containers/string/array of byte types.
* @{
*/
template <typename Type,
// The pointer type.
typename Pointer = decltype(std::data(std::declval<Type>())),
// Make sure the container is random access (that together
// with std::data() function is probably enough to
// require contiguous).
typename = std::enable_if_t<std::is_base_of_v<
std::random_access_iterator_tag,
typename std::iterator_traits<decltype(std::begin(
std::declval<Type>()))>::iterator_category>>,
// Make sure other pointer is pointing to the same type
// current byte view type.
typename = std::enable_if_t<std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::remove_cv_t<value_type>>>,
// Make sure the other pointer points to a byte type.
typename = std::enable_if_t<
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
char> ||
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
unsigned char> ||
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::byte>>>
constexpr basic_byte_view(Type && value) noexcept :
basic_byte_view(std::data(value), std::size(value))
{
}
template <typename Type,
// The pointer type.
typename Pointer = decltype(std::data(std::declval<Type>())),
// Make sure the container is random access (that together
// with std::data() function is probably enough to
// require contiguous).
typename = std::enable_if_t<std::is_base_of_v<
std::random_access_iterator_tag,
typename std::iterator_traits<decltype(std::begin(
std::declval<Type>()))>::iterator_category>>,
// Make sure other pointer is not pointing to the same type
// current byte view type.
typename = std::enable_if_t<!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::remove_cv_t<value_type>>>,
// Make sure the other pointer points to a byte type.
typename = std::enable_if_t<
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
char> ||
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
unsigned char> ||
std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::byte>>,
typename = void>
basic_byte_view(Type && value) noexcept :
basic_byte_view(std::data(value), std::size(value))
{
}
/**
* @}
*/
/**
* Explicitly construct from containers/string/array of non byte types.
* @{
*/
template <typename Type,
// The pointer type.
typename Pointer = decltype(std::data(std::declval<Type>())),
// Make sure the container is random access (that together
// with a std::data() function is probably enough to
// require contiguous).
typename = std::enable_if_t<std::is_base_of_v<
std::random_access_iterator_tag,
typename std::iterator_traits<decltype(std::begin(
std::declval<Type>()))>::iterator_category>>,
// Make sure the other pointer does not point to a byte type.
typename = std::enable_if_t<
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
char> &&
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
unsigned char> &&
!std::is_same_v<
std::remove_cv_t<std::remove_pointer_t<Pointer>>,
std::byte>>>
explicit basic_byte_view(Type && value) noexcept :
basic_byte_view(std::data(value), std::size(value))
{
}
/**
* @}
*/
/**
* Assignment and swap.
* @{
*/
constexpr basic_byte_view & operator=(basic_byte_view other) noexcept
{
other.swap(*this);
return *this;
}
constexpr void swap(basic_byte_view & other) noexcept
{
std::swap(m_data, other.m_data);
std::swap(m_size, other.m_size);
}
/**
* @}
*/
/**
* Accessing a byte at a specific index.
* @{
*/
constexpr reference operator[](index_type index)
{
return m_data[index];
}
constexpr reference operator[](index_type index) const
{
return m_data[index];
}
reference at(index_type index)
{
if (index >= m_size) {
throw std::out_of_range("byte view access out of range");
}
return m_data[index];
}
reference at(index_type index) const
{
if (index >= m_size) {
throw std::out_of_range("byte view access out of range");
}
return m_data[index];
}
/**
* @}
*/
/**
* Accessing front and back.
* @{
*/
constexpr reference front()
{
return *m_data;
}
constexpr reference front() const
{
return *m_data;
}
constexpr reference back()
{
return *(m_data + m_size - 1);
}
constexpr reference back() const
{
return *(m_data + m_size - 1);
}
/**
* @}
*/
/**
* Return the byte view data and size.
* @{
*/
constexpr pointer data()
{
return m_data;
}
constexpr pointer data() const
{
return m_data;
}
constexpr index_type size() const
{
return m_size;
}
constexpr bool empty() const
{
return !m_size;
}
/**
* @}
*/
/**
* Iteration support.
* @{
*/
constexpr iterator begin()
{
return m_data;
}
constexpr iterator begin() const
{
return m_data;
}
constexpr const_iterator cbegin() const
{
return m_data;
}
constexpr reverse_iterator rbegin()
{
return reverse_iterator(end());
}
constexpr const_reverse_iterator rbegin() const
{
return const_reverse_iterator(end());
}
constexpr const_reverse_iterator crbegin() const
{
return const_reverse_iterator(end());
}
constexpr iterator end()
{
return m_data + m_size;
}
constexpr iterator end() const
{
return m_data + m_size;
}
constexpr const_iterator cend() const
{
return m_data + m_size;
}
constexpr reverse_iterator rend()
{
return reverse_iterator(begin());
}
constexpr reverse_iterator rend() const
{
return reverse_iterator(begin());
}
constexpr const_reverse_iterator crend() const
{
return reverse_iterator(begin());
}
/**
* @}
*/
private:
/**
* Pointer to the byte view beginning.
*/
pointer m_data{};
/**
* Size of the byte view.
*/
index_type m_size{};
};
/**
* Swap between left and right byte views.
*/
template <typename Type>
constexpr void swap(basic_byte_view<Type> & left,
basic_byte_view<Type> & right) noexcept
{
left.swap(right);
}
/**
* Byte view aliases.
* @{
*/
using byte_view = basic_byte_view<std::byte>;
using cbyte_view = basic_byte_view<const std::byte>;
/**
* @}
*/
} // namespace zpp
#endif
namespace zpp::filesystem
{
/**
* File seek mode, end, set or current.
* - seek_mode::set - relative to the beginning of the file.
* - seek_mode::end - relative to the end of file.
* - seek_mode::current - relative to the current position.
*/
enum class seek_mode
{
set,
end,
current,
};
namespace detail
{
/**
* Implements read, write, seek, close.
*/
template <typename File>
class basic_file_base
{
public:
/**
* Reads all requested bytes, unless the end of file is reached where
* the reading stops. Returns the data inside a vector of bytes.
*/
std::vector<std::byte> read(std::size_t size) const;
/**
* Reads the entire file and returns its data.
*/
std::vector<std::byte> read() const;
/**
* Reads all requested bytes, unless the end of file is reached where
* the reading stops. The amount of bytes read is returned.
*/
std::size_t read(byte_view data) const;
/**
* Write all of the given data to the file. May return
* less bytes only if there is an insufficient space.
*/
std::size_t write(cbyte_view data) const;
/**
* Attempts to read exactly the amount of bytes requested.
* If not possible, an end_of_file_exception is thrown.
*/
void read_exact(byte_view data) const;
/**
* Attempts to write exactly the amount of bytes requested.
* If not possible, an insufficient_space_exception is thrown.
*/
void write_exact(cbyte_view data) const;
/**
* Reads from the file into the specified data byte array.
* Executes a single read operation which may return less bytes
* than requested. The amount of bytes read is returned.
* If zero is returned, the end of file is reached.
*/
std::size_t read_once(byte_view data) const;
/**
* Writes the given byte array to the file.
* Executes a single write operation which may write less bytes
* than requested. The amount of bytes written is returned.
* If zero is returned there is insufficient space.
*/
std::size_t write_once(cbyte_view data) const;
/**
* Returns the file size.
*/
std::uint64_t size() const;
/**
* Returns true if the file is empty, else false.
*/
bool empty() const;
/**
* Seeks into the file the given number of bytes, according
* to the seek mode. Negative number of bytes is backward direction.
* Returns the previous file pointer.
*/
std::uint64_t seek(std::int64_t offset, seek_mode mode) const;
/**
* Returns the current file pointer.
*/
std::uint64_t tell() const;
/**
* Truncates the file to the given size.
* If the file is extended, the extended contents are unspecified.
* If this function fails, the file position is unspecified.
*/
void truncate(std::uint64_t size) const;
/**
* Sync the file.
*/
void sync() const;
/**
* Closes the file.
*/
void close();
protected:
/**
* Protected destructor as this is not meant
* to be destroyed externally.
*/
~basic_file_base() = default;
private:
/**
* Returns the derived file.
*/
decltype(auto) derived()
{
return static_cast<File &>(*this);
}
/**
* Returns the derived file.
*/
decltype(auto) derived() const
{
return static_cast<const File &>(*this);
}
};
} // namespace detail
/**
* End of file is reached, the amount of bytes read is stored within.
*/
class end_of_file_exception : public std::runtime_error
{
public:
/**
* Creates an end of file exception.
*/
explicit end_of_file_exception(std::size_t bytes_read) :
std::runtime_error("End of file"),
m_bytes_read(bytes_read)
{
}
/**
* Returns the amount of bytes read until the end of file.
*/
std::size_t bytes_read() const
{
return m_bytes_read;
}
private:
std::size_t m_bytes_read;
};
/**
* Insufficient space encountered while writing, the amount of bytes
* written is stored within.
*/
class insufficient_space_exception : public std::runtime_error
{
public:
/**
* Creates an insufficient space exception.
*/
explicit insufficient_space_exception(std::size_t bytes_written) :
std::runtime_error("Insufficient space"),
m_bytes_written(bytes_written)
{
}
/**
* Returns the amount of bytes written until the space limit reached.
*/
std::size_t bytes_written() const
{
return m_bytes_written;
}
private:
std::size_t m_bytes_written;
};
/**
* Represents a file.
*/
template <typename FileHandle, typename InvalidFileHandle>
class basic_file : public detail::basic_file_base<
basic_file<FileHandle, InvalidFileHandle>>
{
public:
/**
* Become friends with all files.
*/
template <typename, typename>
friend class basic_file;
/**
* Invalid file handle value.
*/
inline static const auto invalid_file_handle =
InvalidFileHandle::value;
/**
* The file handle type.
*/
using file_handle = FileHandle;
/**
* Default constructs a file.
*/
basic_file() = default;
/**
* Create a file from a handle.
*/
basic_file(file_handle file_handle) : m_file(std::move(file_handle))
{
}
/**
* Create a file from another file if file handle is convertible.
*/
template <typename OtherFileHandle>
basic_file(
const basic_file<OtherFileHandle, InvalidFileHandle> & other) :
m_file(other.m_file)
{
}
/**
* Swaps between this file and other.
*/
void swap(basic_file & other) noexcept
{
using std::swap;
swap(m_file, other.m_file);
}
/**
* Returns the file handle.
*/
auto get() const
{
if constexpr (std::is_class_v<file_handle>) {
return m_file.get();
} else {
return m_file;
}
}
/**
* Releases ownership of the file handle.
*/
auto release()
{
if constexpr (std::is_class_v<file_handle>) {
return m_file.release();
} else {
auto file = m_file;
m_file = invalid_file_handle;
return file;
}
}
/**
* Returns true if valid, else false.
*/
explicit operator bool() const
{
return (invalid_file_handle != m_file);
}
private:
/**
* The file handle.
*/
file_handle m_file{invalid_file_handle};
};
/**
* Swaps between left file and right file.
*/
template <typename... Arguments>
void swap(basic_file<Arguments...> & left,
basic_file<Arguments...> & right) noexcept
{
left.swap(right);
}
//
// Platform specific code starts here.
//
/**
* The file handle type.
*/
#ifdef ZPP_FILE_WINDOWS
struct invalid_file_handle
{
inline static const auto value = INVALID_HANDLE_VALUE;
};
using weak_file_handle = HANDLE;
#else
struct invalid_file_handle
{
static const auto value = -1;
};
using weak_file_handle = int;
#endif
/**
* Represents a file handle owning class.
*/
class file_handle
{
public:
/**
* Default constructs a file handle.
*/
file_handle() = default;
/**
* Create a file from a handle.
*/
explicit file_handle(weak_file_handle handle) : m_file(handle)
{
}
/**
* Construct a file handle by move from other.
*/
file_handle(file_handle && other) noexcept : m_file(other.release())
{
}
/**
* Assigns to this file handle by from other file handle.
*/
file_handle & operator=(file_handle other) noexcept
{
other.swap(*this);
return *this;
}
/**
* Destroys the file handle, closing it if open.
*/
~file_handle()
{
if (invalid_file_handle::value != m_file) {
#ifdef ZPP_FILE_WINDOWS
CloseHandle(m_file);
#else
::close(m_file);
#endif
}
}
/**
* Swaps between this file handle and other.
*/
void swap(file_handle & other) noexcept
{
std::swap(m_file, other.m_file);
}
/**
* Returns the weak file handle.
*/
weak_file_handle get() const
{
return m_file;
}
/**
* Returns the weak file handle.
*/
explicit operator weak_file_handle() const
{
return m_file;
}
/**
* Releases ownership of the file handle.
*/
weak_file_handle release()
{
auto handle = m_file;
m_file = invalid_file_handle::value;
return handle;
}
private:
/**
* The weak file handle.
*/
weak_file_handle m_file{invalid_file_handle::value};
};
/**
* Swaps between left file handle and right file handle.
*/
inline void swap(file_handle & left, file_handle & right) noexcept
{
left.swap(right);
}
/**
* Alias file and weak_file
* @{
*/
using file = basic_file<file_handle, invalid_file_handle>;
using weak_file = basic_file<weak_file_handle, invalid_file_handle>;
/**
* @}
*/
} // namespace zpp::filesystem
//
// Implementation
//
namespace zpp::filesystem
{
namespace detail
{
template <typename File>
std::size_t basic_file_base<File>::read(byte_view data) const
{