µ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 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_BLOCK_DEVICE_PARTITION_H_
29#define CMSIS_PLUS_POSIX_IO_BLOCK_DEVICE_PARTITION_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
42
43// ----------------------------------------------------------------------------
44
45#pragma GCC diagnostic push
46
47#if defined(__clang__)
48#pragma clang diagnostic ignored "-Wc++98-compat"
49#endif
50
51// ----------------------------------------------------------------------------
52
53namespace os
54{
55 namespace posix
56 {
57 // ------------------------------------------------------------------------
58
59 class block_device_partition_impl;
60
61 // ========================================================================
62
69 {
70 // ----------------------------------------------------------------------
71
77 public:
78
80
85 // The rule of five.
89 operator= (const block_device_partition&) = delete;
91 operator= (block_device_partition&&) = delete;
92
97 virtual
98 ~block_device_partition () override;
99
104 // ----------------------------------------------------------------------
110 public:
111
112 void
113 configure (blknum_t offset, blknum_t nblocks);
114
115 // ----------------------------------------------------------------------
116 // Support functions.
117
119 impl (void) const;
120
124 };
125
126 // ========================================================================
127
128#pragma GCC diagnostic push
129#pragma GCC diagnostic ignored "-Wpadded"
130
132 {
133 // ----------------------------------------------------------------------
134
136
137 // ----------------------------------------------------------------------
138
144 public:
145
147
152 // The rule of five.
156 operator= (const block_device_partition_impl&) = delete;
158 operator= (block_device_partition_impl&&) = delete;
159
164 virtual
166
171 // ----------------------------------------------------------------------
177 public:
178
179 virtual int
180 do_vioctl (int request, std::va_list args) override;
181
182 virtual int
183 do_vopen (const char* path, int oflag, std::va_list args) override;
184
185 virtual ssize_t
186 do_read_block (void* buf, blknum_t blknum, std::size_t nblocks) override;
187
188 virtual ssize_t
189 do_write_block (const void* buf, blknum_t blknum, std::size_t nblocks)
190 override;
191
192 virtual void
193 do_sync (void) override;
194
195 virtual int
196 do_close (void) override;
197
198 // ----------------------------------------------------------------------
199
200 void
202
207 // ----------------------------------------------------------------------
208 protected:
209
214 block_device& parent_;
215
216 blknum_t partition_offset_blocks_ = 0;
217
221 };
222
223#pragma GCC diagnostic pop
224
225 // ========================================================================
226
227 template<typename T = block_device_partition_impl>
229 {
230 // --------------------------------------------------------------------
231
232 public:
233
234 using value_type = T;
235
236 // --------------------------------------------------------------------
237
243 public:
244
245 template<typename ... Args>
247 block_device& parent,
248 Args&&... args);
249
254 // The rule of five.
260 operator= (const block_device_partition_implementable&) = delete;
262 operator= (block_device_partition_implementable&&) = delete;
263
268 virtual
270
275 // --------------------------------------------------------------------
281 public:
282
283 // Support functions.
284
286 impl (void) const;
287
292 // --------------------------------------------------------------------
293 protected:
294
299 // Include the implementation as a member.
300 value_type impl_instance_;
301
305 };
306
307 // ========================================================================
308
309 template<typename T, typename L>
311 {
312 // --------------------------------------------------------------------
313
314 public:
315
316 using value_type = T;
317 using lockable_type = L;
318
319 // --------------------------------------------------------------------
320
326 public:
327
328 template<typename ... Args>
330 block_device& parent,
331 lockable_type& locker,
332 Args&&... args);
333
338 // The rule of five.
342 operator= (const block_device_partition_lockable&) = delete;
344 operator= (block_device_partition_lockable&&) = delete;
345
350 virtual
352
357 // --------------------------------------------------------------------
363 public:
364
365 virtual int
366 vioctl (int request, std::va_list args) override;
367
368 virtual ssize_t
369 read_block (void* buf, blknum_t blknum, std::size_t nblocks = 1)
370 override;
371
372 virtual ssize_t
373 write_block (const void* buf, blknum_t blknum, std::size_t nblocks = 1)
374 override;
375
376 // --------------------------------------------------------------------
377 // Support functions.
378
380 impl (void) const;
381
386 // --------------------------------------------------------------------
387 protected:
388
393 // Include the implementation as a member.
394 value_type impl_instance_;
395
396 lockable_type& locker_;
397
401 };
402
403 // ========================================================================
404
405#pragma GCC diagnostic push
406#if defined(__clang__)
407#pragma clang diagnostic ignored "-Wweak-template-vtables"
408// error: extern templates are incompatible with C++98 [-Werror,-Wc++98-compat-pedantic]
409#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
410#endif
411
412 extern template class block_device_partition_implementable<
414
415#pragma GCC diagnostic pop
416
417 // ==========================================================================
418 } /* namespace posix */
419} /* namespace os */
420
421// ===== Inline & template implementations ====================================
422
423namespace os
424{
425 namespace posix
426 {
427 // ========================================================================
428
429 inline block_device_partition_impl&
431 {
432 return static_cast<block_device_partition_impl&> (impl_);
433 }
434
435 // ========================================================================
436
437 template<typename T>
438 template<typename ... Args>
440 const char* name, block_device& parent, Args&&... args) :
442 { impl_instance_, name }, //
443 impl_instance_
444 { parent, std::forward<Args>(args)... }
445 {
446#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
448 "block_device_partition_implementable::%s(\"%s\")=@%p\n",
449 __func__, name_, this);
450#endif
451 }
452
453 template<typename T>
455 {
456#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
457 trace::printf ("block_device_partition_implementable::%s() @%p %s\n",
458 __func__, this, name_);
459#endif
460 }
461
462 template<typename T>
465 {
466 return static_cast<value_type&> (impl_);
467 }
468
469 // ========================================================================
470
471 template<typename T, typename L>
472 template<typename ... Args>
474 const char* name, block_device& parent, lockable_type& locker,
475 Args&&... args) :
477 { impl_instance_, name }, //
478 impl_instance_
479 { parent, std::forward<Args>(args)... }, //
480 locker_ (locker)
481 {
482#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
483 trace::printf ("block_device_partition_lockable::%s(\"%s\")=@%p\n",
484 __func__, name_, this);
485#endif
486
487 }
488
489 template<typename T, typename L>
491 {
492#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
493 trace::printf ("block_device_partition_lockable::%s() @%p %s\n",
494 __func__, this, name_);
495#endif
496 }
497
498 // ------------------------------------------------------------------------
499
500 template<typename T, typename L>
501 int
503 std::va_list args)
504 {
505#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
506 trace::printf ("block_device_partition_lockable::%s(%d) @%p\n",
507 __func__, request, this);
508#endif
509
510 std::lock_guard<L> lock
511 { locker_ };
512
513 return block_device_partition::vioctl (request, args);
514 }
515
516 template<typename T, typename L>
517 ssize_t
519 blknum_t blknum,
520 std::size_t nblocks)
521 {
522#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
523 trace::printf ("block_device_partition_lockable::%s(%p, %u, %u) @%p\n",
524 __func__, buf, blknum, nblocks, this);
525#endif
526
527 std::lock_guard<L> lock
528 { locker_ };
529
530 return block_device_partition::read_block (buf, blknum, nblocks);
531 }
532
533 template<typename T, typename L>
534 ssize_t
536 blknum_t blknum,
537 std::size_t nblocks)
538 {
539#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE_PARTITION)
540 trace::printf ("block_device_partition_lockable::%s(%p, %u, %u) @%p\n",
541 __func__, buf, blknum, nblocks, this);
542#endif
543
544 std::lock_guard<L> lock
545 { locker_ };
546
547 return block_device_partition::write_block (buf, blknum, nblocks);
548 }
549
550 template<typename T, typename L>
553 {
554 return static_cast<value_type&> (impl_);
555 }
556
557 // ==========================================================================
558 } /* namespace posix */
559} /* namespace os */
560
561#pragma GCC diagnostic pop
562
563// ----------------------------------------------------------------------------
564
565#endif /* __cplusplus */
566
567// ----------------------------------------------------------------------------
568
569#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:310
off_t offset(void)
Definition io.h:461
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
System namespace.