µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
block-device-partition.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) 2018-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_BLOCK_DEVICE_PARTITION_H_
14#define CMSIS_PLUS_POSIX_IO_BLOCK_DEVICE_PARTITION_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
27
28// ----------------------------------------------------------------------------
29
30#pragma GCC diagnostic push
31#if defined(__clang__)
32#pragma clang diagnostic ignored "-Wc++98-compat"
33#endif
34
35// ----------------------------------------------------------------------------
36
37namespace os
38{
39 namespace posix
40 {
41 // ------------------------------------------------------------------------
42
43 class block_device_partition_impl;
44
45 // ========================================================================
46
53 {
54 // ----------------------------------------------------------------------
55
61 public:
62
64
69 // The rule of five.
73 operator= (const block_device_partition&) = delete;
75 operator= (block_device_partition&&) = delete;
76
81 virtual
82 ~block_device_partition () override;
83
88 // ----------------------------------------------------------------------
94 public:
95
96 void
97 configure (blknum_t offset, blknum_t nblocks);
98
99 // ----------------------------------------------------------------------
100 // Support functions.
101
103 impl (void) const;
104
108 };
109
110 // ========================================================================
111
112#pragma GCC diagnostic push
113#if defined(__clang__)
114#pragma clang diagnostic ignored "-Wpadded"
115#elif defined(__GNUC__)
116#pragma GCC diagnostic ignored "-Wpadded"
117#endif
118
120 {
121 // ----------------------------------------------------------------------
122
124
125 // ----------------------------------------------------------------------
126
132 public:
133
135
140 // The rule of five.
144 operator= (const block_device_partition_impl&) = delete;
146 operator= (block_device_partition_impl&&) = delete;
147
152 virtual
154
159 // ----------------------------------------------------------------------
165 public:
166
167 virtual int
168 do_vioctl (int request, std::va_list args) override;
169
170 virtual int
171 do_vopen (const char* path, int oflag, std::va_list args) override;
172
173 virtual ssize_t
174 do_read_block (void* buf, blknum_t blknum, std::size_t nblocks) override;
175
176 virtual ssize_t
177 do_write_block (const void* buf, blknum_t blknum, std::size_t nblocks)
178 override;
179
180 virtual void
181 do_sync (void) override;
182
183 virtual int
184 do_close (void) override;
185
186 // ----------------------------------------------------------------------
187
188 void
190
195 // ----------------------------------------------------------------------
196 protected:
197
202 block_device& parent_;
203
204 blknum_t partition_offset_blocks_ = 0;
205
209 };
210
211#pragma GCC diagnostic pop
212
213 // ========================================================================
214
215 template<typename T = block_device_partition_impl>
217 {
218 // --------------------------------------------------------------------
219
220 public:
221
222 using value_type = T;
223
224 // --------------------------------------------------------------------
225
231 public:
232
233 template<typename ... Args>
235 block_device& parent,
236 Args&&... args);
237
242 // The rule of five.
248 operator= (const block_device_partition_implementable&) = delete;
250 operator= (block_device_partition_implementable&&) = delete;
251
256 virtual
258
263 // --------------------------------------------------------------------
269 public:
270
271 // Support functions.
272
274 impl (void) const;
275
280 // --------------------------------------------------------------------
281 protected:
282
287 // Include the implementation as a member.
288 value_type impl_instance_;
289
293 };
294
295 // ========================================================================
296
297 template<typename T, typename L>
299 {
300 // --------------------------------------------------------------------
301
302 public:
303
304 using value_type = T;
305 using lockable_type = L;
306
307 // --------------------------------------------------------------------
308
314 public:
315
316 template<typename ... Args>
318 block_device& parent,
319 lockable_type& locker,
320 Args&&... args);
321
326 // The rule of five.
330 operator= (const block_device_partition_lockable&) = delete;
332 operator= (block_device_partition_lockable&&) = delete;
333
338 virtual
340
345 // --------------------------------------------------------------------
351 public:
352
353 virtual int
354 vioctl (int request, std::va_list args) override;
355
356 virtual ssize_t
357 read_block (void* buf, blknum_t blknum, std::size_t nblocks = 1)
358 override;
359
360 virtual ssize_t
361 write_block (const void* buf, blknum_t blknum, std::size_t nblocks = 1)
362 override;
363
364 // --------------------------------------------------------------------
365 // Support functions.
366
368 impl (void) const;
369
374 // --------------------------------------------------------------------
375 protected:
376
381 // Include the implementation as a member.
382 value_type impl_instance_;
383
384 lockable_type& locker_;
385
389 };
390
391 // ========================================================================
392
393#pragma GCC diagnostic push
394#if defined(__clang__)
395#pragma clang diagnostic ignored "-Wweak-template-vtables"
396// error: extern templates are incompatible with C++98 [-Werror,-Wc++98-compat-pedantic]
397#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
398#endif
399
400 extern template class block_device_partition_implementable<
402
403#pragma GCC diagnostic pop
404
405 // ==========================================================================
406 } /* namespace posix */
407} /* namespace os */
408
409// ===== Inline & template implementations ====================================
410
411namespace os
412{
413 namespace posix
414 {
415 // ========================================================================
416
417 inline block_device_partition_impl&
419 {
420 return static_cast<block_device_partition_impl&> (impl_);
421 }
422
423 // ========================================================================
424
425 template<typename T>
426 template<typename ... Args>
428 const char* name, block_device& parent, Args&&... args) :
430 { impl_instance_, name }, //
431 impl_instance_
432 { parent, std::forward<Args>(args)... }
433 {
434#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
436 "block_device_partition_implementable::%s(\"%s\")=@%p\n",
437 __func__, name_, this);
438#endif
439 }
440
441 template<typename T>
443 {
444#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
445 trace::printf ("block_device_partition_implementable::%s() @%p %s\n",
446 __func__, this, name_);
447#endif
448 }
449
450 template<typename T>
453 {
454 return static_cast<value_type&> (impl_);
455 }
456
457 // ========================================================================
458
459 template<typename T, typename L>
460 template<typename ... Args>
462 const char* name, block_device& parent, lockable_type& locker,
463 Args&&... args) :
465 { impl_instance_, name }, //
466 impl_instance_
467 { parent, std::forward<Args>(args)... }, //
468 locker_ (locker)
469 {
470#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
471 trace::printf ("block_device_partition_lockable::%s(\"%s\")=@%p\n",
472 __func__, name_, this);
473#endif
474
475 }
476
477 template<typename T, typename L>
479 {
480#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
481 trace::printf ("block_device_partition_lockable::%s() @%p %s\n",
482 __func__, this, name_);
483#endif
484 }
485
486 // ------------------------------------------------------------------------
487
488 template<typename T, typename L>
489 int
491 std::va_list args)
492 {
493#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
494 trace::printf ("block_device_partition_lockable::%s(%d) @%p\n",
495 __func__, request, this);
496#endif
497
498 std::lock_guard<L> lock
499 { locker_ };
500
501 return block_device_partition::vioctl (request, args);
502 }
503
504 template<typename T, typename L>
505 ssize_t
507 blknum_t blknum,
508 std::size_t nblocks)
509 {
510#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
511 trace::printf ("block_device_partition_lockable::%s(%p, %u, %u) @%p\n",
512 __func__, buf, blknum, nblocks, this);
513#endif
514
515 std::lock_guard<L> lock
516 { locker_ };
517
518 return block_device_partition::read_block (buf, blknum, nblocks);
519 }
520
521 template<typename T, typename L>
522 ssize_t
524 blknum_t blknum,
525 std::size_t nblocks)
526 {
527#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
528 trace::printf ("block_device_partition_lockable::%s(%p, %u, %u) @%p\n",
529 __func__, buf, blknum, nblocks, this);
530#endif
531
532 std::lock_guard<L> lock
533 { locker_ };
534
535 return block_device_partition::write_block (buf, blknum, nblocks);
536 }
537
538 template<typename T, typename L>
541 {
542 return static_cast<value_type&> (impl_);
543 }
544
545 // ==========================================================================
546 } /* namespace posix */
547} /* namespace os */
548
549#pragma GCC diagnostic pop
550
551// ----------------------------------------------------------------------------
552
553#endif /* __cplusplus */
554
555// ----------------------------------------------------------------------------
556
557#endif /* CMSIS_PLUS_POSIX_IO_BLOCK_DEVICE_PARTITION_H_ */
block_device::blknum_t blknum_t
virtual int do_vopen(const char *path, int oflag, std::va_list args) override
virtual int do_vioctl(int request, std::va_list args) override
virtual ssize_t do_write_block(const void *buf, blknum_t blknum, std::size_t nblocks) override
void configure(blknum_t offset, blknum_t nblocks)
virtual ssize_t do_read_block(void *buf, blknum_t blknum, std::size_t nblocks) override
block_device_partition_implementable(const char *name, block_device &parent, Args &&... args)
virtual ssize_t read_block(void *buf, blknum_t blknum, std::size_t nblocks=1) override
block_device_partition_lockable(const char *name, block_device &parent, lockable_type &locker, Args &&... args)
virtual ssize_t write_block(const void *buf, blknum_t blknum, std::size_t nblocks=1) override
virtual int vioctl(int request, std::va_list args) override
Block device partition class.
block_device_partition_impl & impl(void) const
void configure(blknum_t offset, blknum_t nblocks)
Block device class.
virtual int vioctl(int request, std::va_list args) override
virtual ssize_t read_block(void *buf, blknum_t blknum, std::size_t nblocks=1)
virtual ssize_t write_block(const void *buf, blknum_t blknum, std::size_t nblocks=1)
const char * name(void) const
Definition device.h:307
off_t offset(void)
Definition io.h:476
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
System namespace.