1  
//
1  
//
2  
// Copyright (c) 2026 Michael Vandeberg
2  
// Copyright (c) 2026 Michael Vandeberg
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/corosio
7  
// Official repository: https://github.com/cppalliance/corosio
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_COROSIO_FILE_BASE_HPP
10  
#ifndef BOOST_COROSIO_FILE_BASE_HPP
11  
#define BOOST_COROSIO_FILE_BASE_HPP
11  
#define BOOST_COROSIO_FILE_BASE_HPP
12  

12  

13  
#include <boost/corosio/detail/config.hpp>
13  
#include <boost/corosio/detail/config.hpp>
14  

14  

15  
namespace boost::corosio {
15  
namespace boost::corosio {
16  

16  

17  
/** Common definitions for file I/O objects.
17  
/** Common definitions for file I/O objects.
18  

18  

19  
    Provides open flags and seek origin constants shared
19  
    Provides open flags and seek origin constants shared
20  
    by @ref stream_file and @ref random_access_file.
20  
    by @ref stream_file and @ref random_access_file.
21  
*/
21  
*/
22  
struct file_base
22  
struct file_base
23  
{
23  
{
24  
    /** Bitmask flags for opening a file.
24  
    /** Bitmask flags for opening a file.
25  

25  

26  
        Flags are combined with bitwise OR to specify the
26  
        Flags are combined with bitwise OR to specify the
27  
        desired access mode and creation behavior.
27  
        desired access mode and creation behavior.
28  
    */
28  
    */
29  
    enum flags : unsigned
29  
    enum flags : unsigned
30  
    {
30  
    {
31  
        /// Open for reading only.
31  
        /// Open for reading only.
32  
        read_only = 1,
32  
        read_only = 1,
33  

33  

34  
        /// Open for writing only.
34  
        /// Open for writing only.
35  
        write_only = 2,
35  
        write_only = 2,
36  

36  

37  
        /// Open for reading and writing.
37  
        /// Open for reading and writing.
38  
        read_write = read_only | write_only,
38  
        read_write = read_only | write_only,
39  

39  

40  
        /// Append to the end of the file on each write.
40  
        /// Append to the end of the file on each write.
41  
        append = 4,
41  
        append = 4,
42  

42  

43  
        /// Create the file if it does not exist.
43  
        /// Create the file if it does not exist.
44  
        create = 8,
44  
        create = 8,
45  

45  

46  
        /// Fail if the file already exists (requires @ref create).
46  
        /// Fail if the file already exists (requires @ref create).
47  
        exclusive = 16,
47  
        exclusive = 16,
48  

48  

49  
        /// Truncate the file to zero length on open.
49  
        /// Truncate the file to zero length on open.
50  
        truncate = 32,
50  
        truncate = 32,
51  

51  

52  
        /// Synchronize data to disk on each write.
52  
        /// Synchronize data to disk on each write.
53  
        sync_all_on_write = 64
53  
        sync_all_on_write = 64
54  
    };
54  
    };
55  

55  

56  
    /** Origin for seek operations. */
56  
    /** Origin for seek operations. */
57  
    enum seek_basis
57  
    enum seek_basis
58  
    {
58  
    {
59  
        /// Seek relative to the beginning of the file.
59  
        /// Seek relative to the beginning of the file.
60  
        seek_set,
60  
        seek_set,
61  

61  

62  
        /// Seek relative to the current position.
62  
        /// Seek relative to the current position.
63  
        seek_cur,
63  
        seek_cur,
64  

64  

65  
        /// Seek relative to the end of the file.
65  
        /// Seek relative to the end of the file.
66  
        seek_end
66  
        seek_end
67  
    };
67  
    };
68  

68  

69  
    friend constexpr flags operator|(flags a, flags b) noexcept
69  
    friend constexpr flags operator|(flags a, flags b) noexcept
70  
    {
70  
    {
71  
        return static_cast<flags>(
71  
        return static_cast<flags>(
72  
            static_cast<unsigned>(a) | static_cast<unsigned>(b));
72  
            static_cast<unsigned>(a) | static_cast<unsigned>(b));
73  
    }
73  
    }
74  

74  

75  
    friend constexpr flags operator&(flags a, flags b) noexcept
75  
    friend constexpr flags operator&(flags a, flags b) noexcept
76  
    {
76  
    {
77  
        return static_cast<flags>(
77  
        return static_cast<flags>(
78  
            static_cast<unsigned>(a) & static_cast<unsigned>(b));
78  
            static_cast<unsigned>(a) & static_cast<unsigned>(b));
79  
    }
79  
    }
80  

80  

81  
    friend constexpr flags& operator|=(flags& a, flags b) noexcept
81  
    friend constexpr flags& operator|=(flags& a, flags b) noexcept
82  
    {
82  
    {
83  
        return a = a | b;
83  
        return a = a | b;
84  
    }
84  
    }
85  

85  

86  
    friend constexpr flags& operator&=(flags& a, flags b) noexcept
86  
    friend constexpr flags& operator&=(flags& a, flags b) noexcept
87  
    {
87  
    {
88  
        return a = a & b;
88  
        return a = a & b;
89  
    }
89  
    }
90  
};
90  
};
91  

91  

92  
} // namespace boost::corosio
92  
} // namespace boost::corosio
93  

93  

94  
#endif // BOOST_COROSIO_FILE_BASE_HPP
94  
#endif // BOOST_COROSIO_FILE_BASE_HPP