µ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 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef CMSIS_PLUS_POSIX_IO_IO_H_
29#define CMSIS_PLUS_POSIX_IO_IO_H_
30
31// ----------------------------------------------------------------------------
32
33#if defined(__cplusplus)
34
35// ----------------------------------------------------------------------------
36
37#if defined(OS_USE_OS_APP_CONFIG_H)
38#include <cmsis-plus/os-app-config.h>
39#endif
40
43
44#include <cstddef>
45#include <cstdarg>
46
47// Needed for ssize_t
48#include <sys/types.h>
49
50// ----------------------------------------------------------------------------
51
52#pragma GCC diagnostic push
53
54#if defined(__clang__)
55#pragma clang diagnostic ignored "-Wc++98-compat"
56#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
57#endif
58
59// ----------------------------------------------------------------------------
60
61struct iovec;
62
63namespace os
64{
65 namespace posix
66 {
67 // ------------------------------------------------------------------------
68
69 class io;
70 class io_impl;
71
72 class file_system;
73 class socket;
74
80 // ------------------------------------------------------------------------
81 io*
82 open (const char* path, int oflag, ...);
83
84 io*
85 vopen (const char* path, int oflag, std::va_list args);
86
91 // ========================================================================
97 class io
98 {
99 // ----------------------------------------------------------------------
100
105 friend class file_system;
106 friend class file_descriptors_manager;
107
108 friend io*
109 vopen (const char* path, int oflag, std::va_list args);
110
111#pragma GCC diagnostic push
112#pragma GCC diagnostic ignored "-Wshadow"
113 friend class socket*
114 socket (int domain, int type, int protocol);
115#pragma GCC diagnostic pop
116
121 // ----------------------------------------------------------------------
122 public:
123
129#pragma GCC diagnostic push
130#pragma GCC diagnostic ignored "-Wshadow"
131
132 using type_t = unsigned int;
133 enum type
134 : type_t
135 { unknown = 0,
136 not_set = 1 << 0,
137 char_device = 1 << 1,
138 block_device = 1 << 2,
139 tty = 1 << 3,
140 file = 1 << 4,
141 socket = 1 << 5
142 };
143
144#pragma GCC diagnostic pop
145
150 // ----------------------------------------------------------------------
156 protected:
157
158 io (io_impl& impl, type t);
159
164 // The rule of five.
165 io (const io&) = delete;
166 io (io&&) = delete;
167 io&
168 operator= (const io&) = delete;
169 io&
170 operator= (io&&) = delete;
171
176 // ----------------------------------------------------------------------
177 public:
178
179 virtual
180 ~io ();
181
186 // ----------------------------------------------------------------------
192 public:
193
194 virtual int
195 close (void);
196
197 virtual ssize_t
198 read (void* buf, std::size_t nbyte);
199
200 virtual ssize_t
201 write (const void* buf, std::size_t nbyte);
202
203 virtual ssize_t
204 writev (const struct iovec* iov, int iovcnt);
205
206 int
207 fcntl (int cmd, ...);
208
209 virtual int
210 vfcntl (int cmd, std::va_list args);
211
212 int
213 isatty (void);
214
215 virtual int
216 fstat (struct stat* buf);
217
218 virtual off_t
219 lseek (off_t offset, int whence);
220
221 // ----------------------------------------------------------------------
222 // Support functions.
223
224 type_t
225 get_type (void) const;
226
228 file_descriptor (void) const;
229
230 bool
231 is_opened (void);
232
233 io_impl&
234 impl (void) const;
235
240 // ----------------------------------------------------------------------
246 protected:
247
248 // ----------------------------------------------------------------------
249 // Support functions.
250
251#if 0
252 // Is called at the end of close, to release objects
253 // acquired from a pool.
254 virtual void
255 do_release (void);
256#endif
257
258 void
260
261 void
263
264 io*
266
271 // ----------------------------------------------------------------------
272 protected:
273
278 io_impl& impl_;
279
284 // ----------------------------------------------------------------------
285 protected:
286
291 type_t type_ = type::not_set;
292
293 file_descriptor_t file_descriptor_ = no_file_descriptor;
294
298 };
299
300 // ========================================================================
301
303 {
304 // ----------------------------------------------------------------------
305
306 friend class io;
307
313 public:
314
315 io_impl (void);
316
321 // The rule of five.
322 io_impl (const io_impl&) = delete;
323 io_impl (io_impl&&) = delete;
324 io_impl&
325 operator= (const io_impl&) = delete;
326 io_impl&
327 operator= (io_impl&&) = delete;
328
333 virtual
334 ~io_impl ();
335
340 // ----------------------------------------------------------------------
346 public:
347
348 // Implementations
349
350 virtual void
351 do_deallocate (void);
352
353 virtual bool
354 do_is_opened (void) = 0;
355
356 virtual bool
357 do_is_connected (void);
358
359 virtual ssize_t
360 do_read (void* buf, std::size_t nbyte) = 0;
361
362 virtual ssize_t
363 do_write (const void* buf, std::size_t nbyte) = 0;
364
365 virtual ssize_t
366 do_writev (const struct iovec* iov, int iovcnt);
367
368 virtual int
369 do_vfcntl (int cmd, std::va_list args);
370
371 virtual int
372 do_isatty (void);
373
374 virtual int
375 do_fstat (struct stat* buf);
376
377 virtual off_t
378 do_lseek (off_t offset, int whence) = 0;
379
380 virtual int
381 do_close (void) = 0;
382
383 // ----------------------------------------------------------------------
384 // Support functions.
385
386 off_t
387 offset (void);
388
389 void
390 offset (off_t offset);
391
396 // ----------------------------------------------------------------------
397 protected:
398
403 off_t offset_ = 0;
404
408 };
409
410 // ==========================================================================
411 } /* namespace posix */
412} /* namespace os */
413
414// ===== Inline & template implementations ====================================
415
416namespace os
417{
418 namespace posix
419 {
420 // ========================================================================
421
422 inline io::type_t
423 io::get_type (void) const
424 {
425 return type_;
426 }
427
428 inline void
430 {
431 file_descriptor_ = fildes;
432 }
433
434 inline void
436 {
437 file_descriptor_ = no_file_descriptor;
438 }
439
440 inline file_descriptor_t
442 {
443 return file_descriptor_;
444 }
445
446 inline bool
448 {
449 return impl ().do_is_opened ();
450 }
451
452 inline io_impl&
453 io::impl (void) const
454 {
455 return impl_;
456 }
457
458 // ========================================================================
459
460 inline off_t
462 {
463 return offset_;
464 }
465
466 inline void
467 io_impl::offset (off_t offset)
468 {
469 offset_ = offset;
470 }
471
472 // ==========================================================================
473 } /* namespace posix */
474} /* namespace os */
475
476#pragma GCC diagnostic pop
477
478// ----------------------------------------------------------------------------
479
480#endif /* __cplusplus */
481
482// ----------------------------------------------------------------------------
483
484#endif /* CMSIS_PLUS_POSIX_IO_IO_H_ */
Block device class.
Char device class.
Definition char-device.h:69
File descriptors manager static class.
File system class.
File class.
Definition file.h:75
virtual bool do_is_connected(void)
Definition io.cpp:494
virtual bool do_is_opened(void)=0
virtual ssize_t do_writev(const struct iovec *iov, int iovcnt)
Definition io.cpp:500
off_t offset(void)
Definition io.h:461
virtual int do_close(void)=0
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:480
virtual void do_deallocate(void)
Definition io.cpp:488
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:98
int isatty(void)
Definition io.cpp:412
file_descriptor_t file_descriptor(void) const
Definition io.h:441
unsigned int type_t
Definition io.h:132
virtual off_t lseek(off_t offset, int whence)
Definition io.cpp:453
bool is_opened(void)
Definition io.h:447
io * alloc_file_descriptor(void)
Definition io.cpp:201
io_impl & impl(void) const
Definition io.h:453
virtual int fstat(struct stat *buf)
Definition io.cpp:422
virtual ssize_t write(const void *buf, std::size_t nbyte)
Definition io.cpp:280
int fcntl(int cmd,...)
Definition io.cpp:375
virtual int close(void)
Definition io.cpp:176
void clear_file_descriptor(void)
Definition io.h:435
virtual ssize_t read(void *buf, std::size_t nbyte)
Definition io.cpp:229
virtual ~io()
Definition io.cpp:164
type_t get_type(void) const
Definition io.h:423
virtual ssize_t writev(const struct iovec *iov, int iovcnt)
Definition io.cpp:333
@ unknown
Definition io.h:135
@ not_set
Definition io.h:136
virtual int vfcntl(int cmd, std::va_list args)
Definition io.cpp:387
Network socket.
int socket(int domain, int type, int protocol)
io * open(const char *path, int oflag,...)
Definition io.cpp:61
io * vopen(const char *path, int oflag, std::va_list args)
Definition io.cpp:78
int stat(const char *path, struct stat *buf)
int file_descriptor_t
Definition types.h:59
constexpr file_descriptor_t no_file_descriptor
Definition types.h:61
System namespace.
Definition uio.h:56