µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
device-registry.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_DEVICE_REGISTRY_H_
29#define CMSIS_PLUS_POSIX_IO_DEVICE_REGISTRY_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 <cassert>
46#include <cstring>
47
48// ----------------------------------------------------------------------------
49
50#pragma GCC diagnostic push
51
52#if defined(__clang__)
53#pragma clang diagnostic ignored "-Wc++98-compat"
54#endif
55
56// ----------------------------------------------------------------------------
57
58namespace os
59{
60 namespace posix
61 {
62 // ========================================================================
63
69 template<typename T>
71 {
72 public:
73
74 using value_type = T;
75 using pointer = T*;
76 using reference = T&;
77
78 // --------------------------------------------------------------------
79
85 public:
86
87 // Do not allow to create instances of this class.
88 device_registry () = delete;
89
94 // The rule of five.
95 device_registry (const value_type&) = delete;
96 device_registry (value_type&&) = delete;
98 operator= (const value_type&) = delete;
100 operator= (value_type&&) = delete;
101
106 ~device_registry () = delete;
107
112 // --------------------------------------------------------------------
118 public:
119
120 static void
122
123 static value_type*
124 identify_device (const char* path);
125
130 // --------------------------------------------------------------------
131 private:
132
137 // Since devices may be constructed statically, so may ask
138 // to be linked here at any time, this list must be initialised
139 // before any static constructor.
140 // With the order of static constructors unknown, this means it
141 // must be allocated in the BSS and will be initialised to 0 by
142 // the startup code.
143 using device_list = utils::intrusive_list<device,
144 utils::double_list_links, &device::registry_links_, T>;
145 static device_list registry_list__;
146
150 };
151
152 // ==========================================================================
153 } /* namespace posix */
154} /* namespace os */
155
156// ===== Inline & template implementations ====================================
157
158namespace os
159{
160 namespace posix
161 {
162 // ========================================================================
163
164 template<typename T>
165 void
167 {
168#if defined(DEBUG)
169 for (auto&& d : registry_list__)
170 {
171 // Validate the device name by checking duplicates.
172 if (std::strcmp (device->name (), d.name ()) == 0)
173 {
174 trace::puts ("Duplicate device name. Abort.");
175 std::abort ();
176 }
177 }
178#endif // DEBUG
179
180 registry_list__.link (*device);
181
182 trace::printf ("Device '%s%s' linked.\n", value_type::device_prefix (),
183 device->name ());
184 }
185
189 template<typename T>
190 T*
192 {
193 assert(path != nullptr);
194
195 auto prefix = value_type::device_prefix ();
196 if (std::strncmp (prefix, path, std::strlen (prefix)) != 0)
197 {
198 // The device prefix does not match, not a device.
199 return nullptr;
200 }
201
202 // The prefix was identified; try to match the rest of the path.
203 auto name = path + std::strlen (prefix);
204
205 for (auto&& p : registry_list__)
206 {
207 if (p.match_name (name))
208 {
209 return static_cast<value_type*> (&p);
210 }
211 }
212
213 // Not a known device.
214 return nullptr;
215 }
216
221#pragma GCC diagnostic push
222#if defined(__clang__)
223#pragma clang diagnostic ignored "-Wexit-time-destructors"
224#pragma clang diagnostic ignored "-Wglobal-constructors"
225#endif
226
227 // Initialised to 0 by BSS.
228 template<typename T>
230
231#pragma GCC diagnostic pop
232
237 // ==========================================================================
238 } /* namespace posix */
239} /* namespace os */
240
241#pragma GCC diagnostic pop
242
243// ----------------------------------------------------------------------------
244
245#endif /* __cplusplus */
246
247// ----------------------------------------------------------------------------
248
249#endif /* CMSIS_PLUS_POSIX_IO_DEVICE_REGISTRY_H_ */
Devices registry static class.
static value_type * identify_device(const char *path)
static void link(value_type *device)
Base device class.
Definition device.h:78
const char * name(void) const
Definition device.h:310
List of intrusive nodes.
Definition lists.h:721
int puts(const char *s)
Write the string and a line terminator to the trace device.
Definition trace.cpp:108
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
System namespace.