µ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++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2018-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#ifndef CMSIS_PLUS_POSIX_IO_BLOCK_DEVICE_PARTITION_H_
13#define CMSIS_PLUS_POSIX_IO_BLOCK_DEVICE_PARTITION_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(__cplusplus)
18
19// ----------------------------------------------------------------------------
20
21#if defined(OS_USE_OS_APP_CONFIG_H)
22#include <cmsis-plus/os-app-config.h>
23#endif
24
26
27// ----------------------------------------------------------------------------
28
29#pragma GCC diagnostic push
30#if defined(__clang__)
31#pragma clang diagnostic ignored "-Wc++98-compat"
32#endif
33
34// ----------------------------------------------------------------------------
35
36namespace os
37{
38 namespace posix
39 {
40 // ------------------------------------------------------------------------
41
42 class block_device_partition_impl;
43
44 // ========================================================================
45
53 {
54 // ----------------------------------------------------------------------
55
61 public:
63
68 // The rule of five.
72 operator= (const block_device_partition&)
73 = delete;
75 operator= (block_device_partition&&)
76 = delete;
77
82 virtual ~block_device_partition () override;
83
88 // ----------------------------------------------------------------------
94 public:
95 void
96 configure (blknum_t offset, blknum_t nblocks);
97
98 // ----------------------------------------------------------------------
99 // Support functions.
100
102 impl (void) const;
103
107 };
108
109 // ========================================================================
110
111#pragma GCC diagnostic push
112#if defined(__clang__)
113#pragma clang diagnostic ignored "-Wpadded"
114#elif defined(__GNUC__)
115#pragma GCC diagnostic ignored "-Wpadded"
116#endif
117
119 {
120 // ----------------------------------------------------------------------
121
123
124 // ----------------------------------------------------------------------
125
131 public:
133
138 // The rule of five.
140 = delete;
143 operator= (const block_device_partition_impl&)
144 = delete;
146 operator= (block_device_partition_impl&&)
147 = delete;
148
153 virtual ~block_device_partition_impl () override;
154
159 // ----------------------------------------------------------------------
165 public:
166 virtual int
167 do_vioctl (int request, std::va_list args) override;
168
169 virtual int
170 do_vopen (const char* path, int oflag, std::va_list args) override;
171
172 virtual ssize_t
173 do_read_block (void* buf, blknum_t blknum, std::size_t nblocks) override;
174
175 virtual ssize_t
176 do_write_block (const void* buf, blknum_t blknum,
177 std::size_t nblocks) override;
178
179 virtual void
180 do_sync (void) override;
181
182 virtual int
183 do_close (void) override;
184
185 // ----------------------------------------------------------------------
186
187 void
189
194 // ----------------------------------------------------------------------
195 protected:
200 block_device& parent_;
201
202 blknum_t partition_offset_blocks_ = 0;
203
207 };
208
209#pragma GCC diagnostic pop
210
211 // ========================================================================
212
213 template <typename T = block_device_partition_impl>
215 {
216 // ----------------------------------------------------------------------
217
218 public:
219 using value_type = T;
220
221 // ----------------------------------------------------------------------
222
228 public:
229 template <typename... Args>
231 block_device& parent,
232 Args&&... args);
233
238 // The rule of five.
241 = delete;
244 = delete;
246 operator= (const block_device_partition_implementable&)
247 = delete;
250 = delete;
251
256 virtual ~block_device_partition_implementable () override;
257
262 // ----------------------------------------------------------------------
268 public:
269 // Support functions.
270
272 impl (void) const;
273
278 // ----------------------------------------------------------------------
279 protected:
284 // Include the implementation as a member.
285 value_type impl_instance_;
286
290 };
291
292 // ========================================================================
293
294 template <typename T, typename L>
296 {
297 // ----------------------------------------------------------------------
298
299 public:
300 using value_type = T;
301 using lockable_type = L;
302
303 // ----------------------------------------------------------------------
304
310 public:
311 template <typename... Args>
313 lockable_type& locker, Args&&... args);
314
319 // The rule of five.
321 = delete;
323 = delete;
325 operator= (const block_device_partition_lockable&)
326 = delete;
329 = delete;
330
335 virtual ~block_device_partition_lockable () override;
336
341 // ----------------------------------------------------------------------
347 public:
348 virtual int
349 vioctl (int request, std::va_list args) override;
350
351 virtual ssize_t
352 read_block (void* buf, blknum_t blknum,
353 std::size_t nblocks = 1) override;
354
355 virtual ssize_t
356 write_block (const void* buf, blknum_t blknum,
357 std::size_t nblocks = 1) override;
358
359 // ----------------------------------------------------------------------
360 // Support functions.
361
363 impl (void) const;
364
369 // ----------------------------------------------------------------------
370 protected:
375 // Include the implementation as a member.
376 value_type impl_instance_;
377
378 lockable_type& locker_;
379
383 };
384
385 // ========================================================================
386
387#pragma GCC diagnostic push
388#if defined(__clang__)
389#pragma clang diagnostic ignored "-Wweak-template-vtables"
390// error: extern templates are incompatible with C++98
391// [-Werror,-Wc++98-compat-pedantic]
392#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
393#endif
394
395 extern template class block_device_partition_implementable<
397
398#pragma GCC diagnostic pop
399
400 // ========================================================================
401 } /* namespace posix */
402} /* namespace os */
403
404// ===== Inline & template implementations ====================================
405
406namespace os
407{
408 namespace posix
409 {
410 // ========================================================================
411
412 inline block_device_partition_impl&
414 {
415 return static_cast<block_device_partition_impl&> (impl_);
416 }
417
418 // ========================================================================
419
420 template <typename T>
421 template <typename... Args>
423 T>::block_device_partition_implementable (const char* name,
424 block_device& parent,
425 Args&&... args)
426 : block_device_partition{ impl_instance_, name }, //
427 impl_instance_{ parent, std::forward<Args> (args)... }
428 {
429#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
430 trace::printf ("block_device_partition_implementable::%s(\"%s\")=@%p\n",
431 __func__, name_, this);
432#endif
433 }
434
435 template <typename T>
437 T>::~block_device_partition_implementable ()
438 {
439#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
440 trace::printf ("block_device_partition_implementable::%s() @%p %s\n",
441 __func__, this, name_);
442#endif
443 }
444
445 template <typename T>
448 {
449 return static_cast<value_type&> (impl_);
450 }
451
452 // ========================================================================
453
454 template <typename T, typename L>
455 template <typename... Args>
457 const char* name, block_device& parent, lockable_type& locker,
458 Args&&... args)
459 : block_device_partition{ impl_instance_, name }, //
460 impl_instance_{ parent, std::forward<Args> (args)... }, //
461 locker_ (locker)
462 {
463#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
464 trace::printf ("block_device_partition_lockable::%s(\"%s\")=@%p\n",
465 __func__, name_, this);
466#endif
467 }
468
469 template <typename T, typename L>
471 {
472#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
473 trace::printf ("block_device_partition_lockable::%s() @%p %s\n",
474 __func__, this, name_);
475#endif
476 }
477
478 // ------------------------------------------------------------------------
479
480 template <typename T, typename L>
481 int
483 std::va_list args)
484 {
485#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
486 trace::printf ("block_device_partition_lockable::%s(%d) @%p\n", __func__,
487 request, this);
488#endif
489
490 std::lock_guard<L> lock{ locker_ };
491
492 return block_device_partition::vioctl (request, args);
493 }
494
495 template <typename T, typename L>
496 ssize_t
498 blknum_t blknum,
499 std::size_t nblocks)
500 {
501#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
502 trace::printf ("block_device_partition_lockable::%s(%p, %u, %u) @%p\n",
503 __func__, buf, blknum, nblocks, this);
504#endif
505
506 std::lock_guard<L> lock{ locker_ };
507
508 return block_device_partition::read_block (buf, blknum, nblocks);
509 }
510
511 template <typename T, typename L>
512 ssize_t
514 blknum_t blknum,
515 std::size_t nblocks)
516 {
517#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
518 trace::printf ("block_device_partition_lockable::%s(%p, %u, %u) @%p\n",
519 __func__, buf, blknum, nblocks, this);
520#endif
521
522 std::lock_guard<L> lock{ locker_ };
523
524 return block_device_partition::write_block (buf, blknum, nblocks);
525 }
526
527 template <typename T, typename L>
530 {
531 return static_cast<value_type&> (impl_);
532 }
533
534 // ========================================================================
535 } /* namespace posix */
536} /* namespace os */
537
538#pragma GCC diagnostic pop
539
540// ----------------------------------------------------------------------------
541
542#endif /* __cplusplus */
543
544// ----------------------------------------------------------------------------
545
546#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
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.<cmsis-plus/posix-io/block-device-partitions.h>
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:304
off_t offset(void)
Definition io.h:472
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
System namespace.
Standard std namespace.