µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2015-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#ifndef CMSIS_PLUS_POSIX_IO_IO_H_
14#define CMSIS_PLUS_POSIX_IO_IO_H_
15
16// ----------------------------------------------------------------------------
17
18#if defined(__cplusplus)
19
20// ----------------------------------------------------------------------------
21
22#if defined(OS_USE_OS_APP_CONFIG_H)
23#include <cmsis-plus/os-app-config.h>
24#endif
25
28
29#include <cstddef>
30#include <cstdarg>
31
32// Needed for ssize_t
33#include <sys/types.h>
34
35// ----------------------------------------------------------------------------
36
37#pragma GCC diagnostic push
38#if defined(__clang__)
39#pragma clang diagnostic ignored "-Wc++98-compat"
40#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
41#endif
42
43// ----------------------------------------------------------------------------
44
45struct iovec;
46
47namespace os
48{
49 namespace posix
50 {
51 // ------------------------------------------------------------------------
52
53 class io;
54 class io_impl;
55
56 class file_system;
57 class socket;
58
64 // ------------------------------------------------------------------------
65 io*
66 open (const char* path, int oflag, ...);
67
68 io*
69 vopen (const char* path, int oflag, std::va_list args);
70
75 // ========================================================================
81#pragma GCC diagnostic push
82#if defined(__clang__)
83#elif defined(__GNUC__)
84#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
85#endif
86 class io
87 {
88 // ----------------------------------------------------------------------
89
94 friend class file_system;
95 friend class file_descriptors_manager;
96
97 friend io*
98 vopen (const char* path, int oflag, std::va_list args);
99
100#pragma GCC diagnostic push
101#if defined(__clang__)
102#elif defined(__GNUC__)
103#pragma GCC diagnostic ignored "-Wshadow"
104#endif
105 friend /* class */ socket*
106 socket (int domain, int type, int protocol);
107#pragma GCC diagnostic pop
108
113 // ----------------------------------------------------------------------
114 public:
115
121#pragma GCC diagnostic push
122#if defined(__clang__)
123#elif defined(__GNUC__)
124#pragma GCC diagnostic ignored "-Wshadow"
125#endif
126
127 using type_t = unsigned int;
128 enum class type
129 : type_t
130 { unknown = 0,
131 not_set = 1 << 0,
132 char_device = 1 << 1,
133 block_device = 1 << 2,
134 tty = 1 << 3,
135 file = 1 << 4,
136 socket = 1 << 5
137 };
138
139#pragma GCC diagnostic pop
140
145 // ----------------------------------------------------------------------
151 protected:
152
153 io (io_impl& impl, type t);
154
159 // The rule of five.
160 io (const io&) = delete;
161 io (io&&) = delete;
162 io&
163 operator= (const io&) = delete;
164 io&
165 operator= (io&&) = delete;
166
171 // ----------------------------------------------------------------------
172 public:
173
174 virtual
175 ~io ();
176
181 // ----------------------------------------------------------------------
187 public:
188
189 virtual int
190 close (void);
191
192 virtual ssize_t
193 read (void* buf, std::size_t nbyte);
194
195 virtual ssize_t
196 write (const void* buf, std::size_t nbyte);
197
198 virtual ssize_t
199 writev (const /* struct */ iovec* iov, int iovcnt);
200
201 int
202 fcntl (int cmd, ...);
203
204 virtual int
205 vfcntl (int cmd, std::va_list args);
206
207 int
208 isatty (void);
209
210#pragma GCC diagnostic push
211#if defined(__clang__)
212#elif defined(__GNUC__)
213#pragma GCC diagnostic ignored "-Wredundant-tags"
214#endif
215 virtual int
216 fstat (struct stat* buf);
217#pragma GCC diagnostic pop
218
219 virtual off_t
220 lseek (off_t offset, int whence);
221
222 // ----------------------------------------------------------------------
223 // Support functions.
224
225 type_t
226 get_type (void) const;
227
229 file_descriptor (void) const;
230
231 bool
232 is_opened (void);
233
234 io_impl&
235 impl (void) const;
236
241 // ----------------------------------------------------------------------
247 protected:
248
249 // ----------------------------------------------------------------------
250 // Support functions.
251
252#if 0
253 // Is called at the end of close, to release objects
254 // acquired from a pool.
255 virtual void
256 do_release (void);
257#endif
258
259 void
261
262 void
264
265 io*
267
272 // ----------------------------------------------------------------------
273 protected:
274
279 io_impl& impl_;
280
285 // ----------------------------------------------------------------------
286 protected:
287
292 type_t type_ = static_cast<type_t>(type::not_set);
293
294 file_descriptor_t file_descriptor_ = no_file_descriptor;
295
299 };
300#pragma GCC diagnostic pop
301
302 // ========================================================================
303
304#pragma GCC diagnostic push
305#if defined(__clang__)
306#elif defined(__GNUC__)
307#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
308#pragma GCC diagnostic ignored "-Wsuggest-final-types"
309#endif
311 {
312 // ----------------------------------------------------------------------
313
314 friend class io;
315
321 public:
322
323 io_impl (void);
324
329 // The rule of five.
330 io_impl (const io_impl&) = delete;
331 io_impl (io_impl&&) = delete;
332 io_impl&
333 operator= (const io_impl&) = delete;
334 io_impl&
335 operator= (io_impl&&) = delete;
336
341 virtual
342 ~io_impl ();
343
348 // ----------------------------------------------------------------------
354 public:
355
356 // Implementations
357
358 virtual void
359 do_deallocate (void);
360
361 virtual bool
362 do_is_opened (void) = 0;
363
364 virtual bool
365 do_is_connected (void);
366
367 virtual ssize_t
368 do_read (void* buf, std::size_t nbyte) = 0;
369
370 virtual ssize_t
371 do_write (const void* buf, std::size_t nbyte) = 0;
372
373 virtual ssize_t
374 do_writev (const /* struct */ iovec* iov, int iovcnt);
375
376 virtual int
377 do_vfcntl (int cmd, std::va_list args);
378
379 virtual int
380 do_isatty (void);
381
382#pragma GCC diagnostic push
383#if defined(__clang__)
384#elif defined(__GNUC__)
385#pragma GCC diagnostic ignored "-Wredundant-tags"
386#endif
387 virtual int
388 do_fstat (struct stat* buf);
389#pragma GCC diagnostic pop
390
391 virtual off_t
392 do_lseek (off_t offset, int whence) = 0;
393
394 virtual int
395 do_close (void) = 0;
396
397 // ----------------------------------------------------------------------
398 // Support functions.
399
400 off_t
401 offset (void);
402
403 void
404 offset (off_t offset);
405
410 // ----------------------------------------------------------------------
411 protected:
412
417 off_t offset_ = 0;
418
422 };
423#pragma GCC diagnostic pop
424
425 // ==========================================================================
426 } /* namespace posix */
427} /* namespace os */
428
429// ===== Inline & template implementations ====================================
430
431namespace os
432{
433 namespace posix
434 {
435 // ========================================================================
436
437 inline io::type_t
438 io::get_type (void) const
439 {
440 return type_;
441 }
442
443 inline void
445 {
446 file_descriptor_ = fildes;
447 }
448
449 inline void
451 {
452 file_descriptor_ = no_file_descriptor;
453 }
454
455 inline file_descriptor_t
457 {
458 return file_descriptor_;
459 }
460
461 inline bool
463 {
464 return impl ().do_is_opened ();
465 }
466
467 inline io_impl&
468 io::impl (void) const
469 {
470 return impl_;
471 }
472
473 // ========================================================================
474
475 inline off_t
477 {
478 return offset_;
479 }
480
481 inline void
482 io_impl::offset (off_t offset)
483 {
484 offset_ = offset;
485 }
486
487 // ==========================================================================
488 } /* namespace posix */
489} /* namespace os */
490
491#pragma GCC diagnostic pop
492
493// ----------------------------------------------------------------------------
494
495#endif /* __cplusplus */
496
497// ----------------------------------------------------------------------------
498
499#endif /* CMSIS_PLUS_POSIX_IO_IO_H_ */
Block device class.
Char device class.
Definition char-device.h:53
File descriptors manager static class.
File system class.
File class.
Definition file.h:65
virtual bool do_is_connected(void)
Definition io.cpp:497
virtual bool do_is_opened(void)=0
off_t offset(void)
Definition io.h:476
virtual int do_close(void)=0
virtual ssize_t do_writev(const iovec *iov, int iovcnt)
Definition io.cpp:503
virtual int do_vfcntl(int cmd, std::va_list args)
Definition io.cpp:521
virtual int do_isatty(void)
Definition io.cpp:528
virtual ssize_t do_write(const void *buf, std::size_t nbyte)=0
virtual ~io_impl()
Definition io.cpp:475
virtual void do_deallocate(void)
Definition io.cpp:483
virtual ssize_t do_read(void *buf, std::size_t nbyte)=0
virtual int do_fstat(struct stat *buf)
Definition io.cpp:535
virtual off_t do_lseek(off_t offset, int whence)=0
Base I/O class.
Definition io.h:87
int isatty(void)
Definition io.cpp:407
virtual ssize_t writev(const iovec *iov, int iovcnt)
Definition io.cpp:322
file_descriptor_t file_descriptor(void) const
Definition io.h:456
unsigned int type_t
Definition io.h:127
virtual off_t lseek(off_t offset, int whence)
Definition io.cpp:448
bool is_opened(void)
Definition io.h:462
io * alloc_file_descriptor(void)
Definition io.cpp:190
io_impl & impl(void) const
Definition io.h:468
virtual int fstat(struct stat *buf)
Definition io.cpp:417
virtual ssize_t write(const void *buf, std::size_t nbyte)
Definition io.cpp:269
int fcntl(int cmd,...)
Definition io.cpp:364
virtual int close(void)
Definition io.cpp:165
void clear_file_descriptor(void)
Definition io.h:450
virtual ssize_t read(void *buf, std::size_t nbyte)
Definition io.cpp:218
virtual ~io()
Definition io.cpp:153
type_t get_type(void) const
Definition io.h:438
virtual int vfcntl(int cmd, std::va_list args)
Definition io.cpp:381
Network socket.
int socket(int domain, int type, int protocol)
io * open(const char *path, int oflag,...)
Definition io.cpp:50
io * vopen(const char *path, int oflag, std::va_list args)
Definition io.cpp:67
int stat(const char *path, struct stat *buf)
int file_descriptor_t
Definition types.h:43
constexpr file_descriptor_t no_file_descriptor
Definition types.h:45
System namespace.
Definition uio.h:41