µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
directory.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_DIRECTORY_H_
14#define CMSIS_PLUS_POSIX_IO_DIRECTORY_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
30
31#include <mutex>
32
33// ----------------------------------------------------------------------------
34
35#pragma GCC diagnostic push
36#if defined(__clang__)
37#pragma clang diagnostic ignored "-Wc++98-compat"
38#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
39#endif
40
41// ----------------------------------------------------------------------------
42
43namespace os
44{
45 namespace posix
46 {
47 // ------------------------------------------------------------------------
48
49 class directory;
50 class directory_impl;
51 class file_system;
52
53 // ------------------------------------------------------------------------
54
55 // ========================================================================
56
57#pragma GCC diagnostic push
58#if defined(__clang__)
59#pragma clang diagnostic ignored "-Wpadded"
60#elif defined(__GNUC__)
61#pragma GCC diagnostic ignored "-Wpadded"
62#endif
63
69#pragma GCC diagnostic push
70#if defined(__clang__)
71#elif defined(__GNUC__)
72#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
73#pragma GCC diagnostic ignored "-Wsuggest-final-types"
74#endif
76 {
77 // ----------------------------------------------------------------------
78
83 friend class file_system;
84
89 // ----------------------------------------------------------------------
95 public:
96
98
103 // The rule of five.
104 directory (const directory&) = delete;
105 directory (directory&&) = delete;
106 directory&
107 operator= (const directory&) = delete;
108 directory&
109 operator= (directory&&) = delete;
110
115 virtual
116 ~directory ();
117
122 // ----------------------------------------------------------------------
128 public:
129
130 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html
131 virtual /* struct */ dirent *
132 read (void);
133
134 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/rewinddir.html
135 virtual void
136 rewind (void);
137
138 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/closedir.html
139 virtual int
140 close (void);
141
142 // ----------------------------------------------------------------------
143 // Support functions.
144
145 /* struct */ dirent*
146 dir_entry (void);
147
148#pragma GCC diagnostic push
149#if defined(__clang__)
150#elif defined(__GNUC__)
151#pragma GCC diagnostic ignored "-Wredundant-tags"
152#endif
153
154 class file_system&
155 get_file_system (void) const;
156
157#pragma GCC diagnostic pop
158
160 impl (void) const;
161
166 // ----------------------------------------------------------------------
167 public:
168
173 // Intrusive node used to link this device to the deferred
174 // deallocation list. Must be public.
175 utils::double_list_links deferred_links_;
176
181 // ----------------------------------------------------------------------
182 protected:
183
188 directory_impl& impl_;
189
193 };
194#pragma GCC diagnostic pop
195
196 // ========================================================================
197
199 {
200 // ----------------------------------------------------------------------
201
206 friend class directory;
207
212 // ----------------------------------------------------------------------
218 public:
219
220 directory_impl (/* class */ file_system& fs);
221
226 // The rule of five.
227 directory_impl (const directory_impl&) = delete;
228 directory_impl (directory_impl&&) = delete;
230 operator= (const directory_impl&) = delete;
232 operator= (directory_impl&&) = delete;
233
238 virtual
240
245 // ----------------------------------------------------------------------
251 public:
252
253 // Implementations
254
258 virtual /* struct */ dirent*
259 do_read (void) = 0;
260
261 virtual void
262 do_rewind (void) = 0;
263
264 virtual int
265 do_close (void) = 0;
266
267 // ----------------------------------------------------------------------
268 // Support functions.
269
270#pragma GCC diagnostic push
271#if defined(__clang__)
272#elif defined(__GNUC__)
273#pragma GCC diagnostic ignored "-Wredundant-tags"
274#endif
275
276 class file_system&
277 get_file_system (void) const;
278
279#pragma GCC diagnostic pop
280
285 // ----------------------------------------------------------------------
286 protected:
287
292 // This also solves the readdir() re-entrancy issue.
293 /* struct */ dirent dir_entry_;
294
295 /* class */ file_system& file_system_;
296
300 };
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
310 template<typename T>
312 {
313 // --------------------------------------------------------------------
314
315 public:
316
317 using value_type = T;
318
319 // --------------------------------------------------------------------
325 public:
326
327 directory_implementable (/* class */ file_system& fs);
328
333 // The rule of five.
337 operator= (const directory_implementable&) = delete;
339 operator= (directory_implementable&&) = delete;
340
345 virtual
346 ~directory_implementable () override;
347
352 // --------------------------------------------------------------------
358 public:
359
360 // Support functions.
361
363 impl (void) const;
364
369 // --------------------------------------------------------------------
370 protected:
371
376 value_type impl_instance_;
377
381 };
382#pragma GCC diagnostic pop
383
384 // ========================================================================
385
386#pragma GCC diagnostic push
387#if defined(__clang__)
388#elif defined(__GNUC__)
389#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
390#pragma GCC diagnostic ignored "-Wsuggest-final-types"
391#endif
392 template<typename T, typename L>
394 {
395 // --------------------------------------------------------------------
396
397 public:
398
399 using value_type = T;
400 using lockable_type = L;
401
402 // --------------------------------------------------------------------
403
409 public:
410
411 directory_lockable (/* class */ file_system& fs, lockable_type& locker);
412
417 // The rule of five.
418 directory_lockable (const directory_lockable&) = delete;
421 operator= (const directory_lockable&) = delete;
423 operator= (directory_lockable&&) = delete;
424
429 virtual
430 ~directory_lockable () override;
431
436 // --------------------------------------------------------------------
442 public:
443
444 // opendir() uses the file system lock.
445
446 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html
447 virtual /* struct */ dirent *
448 read (void) override;
449
450 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/rewinddir.html
451 virtual void
452 rewind (void) override;
453
454 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/closedir.html
455 virtual int
456 close (void) override;
457
458 // --------------------------------------------------------------------
459 // Support functions.
460
462 impl (void) const;
463
468 // --------------------------------------------------------------------
469 protected:
470
475 value_type impl_instance_;
476
477 lockable_type& locker_;
478
482 };
483#pragma GCC diagnostic pop
484
485#pragma GCC diagnostic pop
486
487 // ==========================================================================
488 } /* namespace posix */
489} /* namespace os */
490
491// ===== Inline & template implementations ====================================
492
493namespace os
494{
495 namespace posix
496 {
497 // ========================================================================
498
499 inline file_system&
501 {
502 return impl ().get_file_system ();
503 }
504
505 inline /* struct */ dirent*
507 {
508 return &(impl ().dir_entry_);
509 }
510
511 inline directory_impl&
512 directory::impl (void) const
513 {
514 return /* static_cast<directory_impl&> */ (impl_);
515 }
516
517 // ========================================================================
518
519 inline file_system&
521 {
522 return file_system_;
523 }
524
525 // ========================================================================
526
527 template<typename T>
529 /* class */ file_system& fs) :
531 { impl_instance_ }, //
532 impl_instance_
533 { fs }
534 {
535#if defined(OS_TRACE_POSIX_IO_DIRECTORY)
536 trace::printf ("directory_implementable::%s()=@%p\n", __func__, this);
537#endif
538 }
539
540#pragma GCC diagnostic push
541#if defined(__clang__)
542#elif defined(__GNUC__)
543#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
544#endif
545 template<typename T>
547 {
548#if defined(OS_TRACE_POSIX_IO_DIRECTORY)
549 trace::printf ("directory_implementable::%s() @%p\n", __func__, this);
550#endif
551 }
552#pragma GCC diagnostic pop
553
554 template<typename T>
557 {
558 return static_cast<value_type&> (impl_);
559 }
560
561 // ========================================================================
562
563 template<typename T, typename L>
565 lockable_type& locker) :
567 { impl_instance_ }, //
568 impl_instance_
569 { fs }, //
570 locker_ (locker)
571 {
572#if defined(OS_TRACE_POSIX_IO_DIRECTORY)
573 trace::printf ("directory_lockable::%s()=@%p\n", __func__, this);
574#endif
575 }
576
577#pragma GCC diagnostic push
578#if defined(__clang__)
579#elif defined(__GNUC__)
580#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
581#endif
582 template<typename T, typename L>
584 {
585#if defined(OS_TRACE_POSIX_IO_DIRECTORY)
586 trace::printf ("directory_lockable::%s() @%p\n", __func__, this);
587#endif
588 }
589#pragma GCC diagnostic pop
590
591 // ------------------------------------------------------------------------
592
593 template<typename T, typename L>
594 /* struct */ dirent *
596 {
597#if defined(OS_TRACE_POSIX_IO_DIRECTORY)
598 trace::printf ("directory_lockable::%s() @%p\n", __func__, this);
599#endif
600
601 std::lock_guard<L> lock
602 { locker_ };
603
604 return directory::read ();
605 }
606
607 template<typename T, typename L>
608 void
610 {
611#if defined(OS_TRACE_POSIX_IO_DIRECTORY)
612 trace::printf ("directory_lockable::%s() @%p\n", __func__, this);
613#endif
614
615 std::lock_guard<L> lock
616 { locker_ };
617
618 return directory::rewind ();
619 }
620
621 template<typename T, typename L>
622 int
624 {
625#if defined(OS_TRACE_POSIX_IO_DIRECTORY)
626 trace::printf ("directory_lockable::%s() @%p\n", __func__, this);
627#endif
628
629 std::lock_guard<L> lock
630 { locker_ };
631
632 return directory::close ();
633 }
634
635 template<typename T, typename L>
638 {
639 return static_cast<value_type&> (impl_);
640 }
641
642 // ==========================================================================
643 } /* namespace posix */
644} /* namespace os */
645
646#pragma GCC diagnostic pop
647
648// ----------------------------------------------------------------------------
649
650#endif /* __cplusplus */
651
652// ----------------------------------------------------------------------------
653
654#endif /* CMSIS_PLUS_POSIX_IO_DIRECTORY_H_ */
class file_system & get_file_system(void) const
Definition directory.h:520
virtual int do_close(void)=0
virtual dirent * do_read(void)=0
virtual void do_rewind(void)=0
value_type & impl(void) const
Definition directory.h:556
directory_implementable(file_system &fs)
Definition directory.h:528
virtual ~directory_implementable() override
Definition directory.h:546
value_type & impl(void) const
Definition directory.h:637
virtual int close(void) override
Definition directory.h:623
directory_lockable(file_system &fs, lockable_type &locker)
Definition directory.h:564
virtual ~directory_lockable() override
Definition directory.h:583
virtual dirent * read(void) override
Definition directory.h:595
virtual void rewind(void) override
Definition directory.h:609
Directory class.
Definition directory.h:76
dirent * dir_entry(void)
Definition directory.h:506
virtual int close(void)
Definition directory.cpp:86
class file_system & get_file_system(void) const
Definition directory.h:500
virtual dirent * read(void)
Definition directory.cpp:52
virtual void rewind(void)
Definition directory.cpp:70
directory_impl & impl(void) const
Definition directory.h:512
File system class.
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
System namespace.