]> git.tdb.fi Git - libs/core.git/blob - source/io/base.h
Add move semantics to Variant
[libs/core.git] / source / io / base.h
1 #ifndef MSP_IO_BASE_H_
2 #define MSP_IO_BASE_H_
3
4 #include <cstddef>
5 #include <sigc++/sigc++.h>
6 #include <msp/core/mspcore_api.h>
7 #include <msp/core/mutex.h>
8 #include <msp/core/noncopyable.h>
9 #include "handle.h"
10 #include "mode.h"
11 #include "poll.h"
12
13 namespace Msp {
14 namespace IO {
15
16 /**
17 Common interface for all I/O objects.
18 */
19 class MSPCORE_API Base: private NonCopyable
20 {
21 public:
22         /** RAII synchronization primitive.  Prevents concurrent access to the
23         target object during the lifetime of the Synchronize object. */
24         class Synchronize
25         {
26         private:
27                 Base &io;
28
29         public:
30                 Synchronize(Base &);
31                 ~Synchronize();
32         };
33
34         /** Emitted when there is no more data to be read. */
35         sigc::signal<void> signal_end_of_file;
36
37         /** Emitted when there is a nonlinearity in I/O (such as a file being
38         seeked) and any data buffered by upper layers needs to be flushed. */
39         sigc::signal<void> signal_flush_required;
40
41         /** Emitted when the object is deleted.  Mainly for use by
42         EventDispatcher. */
43         sigc::signal<void> signal_deleted;
44
45 protected:
46         Mode mode = M_READ;
47         bool eof_flag = false;
48         Mutex *mutex = nullptr;
49
50         Base();
51 public:
52         virtual ~Base();
53
54         /** Sets blocking mode.  When blocking is enabled, most operations won't
55         return until they can make progress.  When blocking is disabled, these
56         operations may return immediately with a return code indicating that nothing
57         was done.
58
59         Blocking is enabled by default. */
60         virtual void set_block(bool) = 0;
61
62         /** Sets inheritance mode.  When inheritance is enabled, the file descriptor
63         will persist through Process::execute.
64
65         Inheritance is disabled by default. */
66         virtual void set_inherit(bool) = 0;
67
68         /** Returns the current mode flags. */
69         Mode get_mode() const { return mode; }
70
71 protected:
72         void check_access(Mode) const;
73
74         virtual std::size_t do_write(const char *, std::size_t) = 0;
75         virtual std::size_t do_read(char *, std::size_t) = 0;
76
77 public:
78         /** Writes data from a buffer.  Subject to blocking.  Returns the number of
79         bytes written, which may be zero for a non-blockin operation. */
80         std::size_t write(const char *b, std::size_t c) { return do_write(b, c); }
81
82         /** Writes a string.  This is identical to calling
83         write(s.data(), s.size()). */
84         std::size_t write(const std::string &s) { return do_write(s.data(), s.size()); }
85
86         /** Writes a single character.  This is identical to calling
87         write(&c, 1). */
88         virtual std::size_t put(char c) { return do_write(&c, 1); }
89
90         /** Reads data into a buffer.  Subject to blocking.  Returns the number of
91         bytes read, which may be zero for a non-blocking operation. */
92         std::size_t read(char *b, std::size_t c) { return do_read(b, c); }
93
94         /** Reads characters up to the next linefeed or end-of-file.  The linefeed
95         is not included in the line.  Returns true if a line was successfully read,
96         false otherwise. */
97         virtual bool getline(std::string &);
98
99         /** Reads a single character.  Returns -1 if no character was available due
100         to end-of-file or non-blocking operation. */
101         virtual int get();
102
103 protected:
104         void set_eof();
105
106 public:
107         /** Returns the end-of-file flag.  Note that some types of objects won't
108         indicate end-of-file until you try to read at least one byte past the actual
109         end, while others indicate it when you've read the last byte. */
110         bool eof() const { return eof_flag; }
111
112         /** Returns the system-level handle for the object.  Used by Console to
113         perform redirections. */
114         virtual const Handle &get_handle(Mode) = 0;
115 };
116
117 } // namespace IO
118 } // namespace Msp
119
120 #endif