SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
SoDaBase.hxx
Go to the documentation of this file.
1/*
2Copyright (c) 2012,2013,2014,2025, 2026 Matthew H. Reilly (kb1vc)
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9 Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
14 distribution.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#pragma once
30
31#include "Command.hxx"
32#include "Debug.hxx"
33#include <complex>
34#include <string>
35#include <sys/types.h>
36#include <unistd.h>
37#include <sys/syscall.h>
38
39#include <SoDa/Format.hxx>
40#include <SoDa/MailBox.hxx>
41
42extern "C" {
43#include <signal.h>
44#include <stdio.h>
45#include <unistd.h>
46}
47
57
58
59namespace SoDa {
60 class FBuf;
61 typedef std::shared_ptr<FBuf> FBufPtr;
62
63 class CBuf;
64 typedef std::shared_ptr<CBuf> CBufPtr;
65
69 enum RXTX { RX, // the command/query/response relates to a receiver
70 TX, // the command/query/response relates to a transmitter
71 TXRX // the command/query/response relates to a transmitter and receiver
72 };
73
74
78 typedef SoDa::MailBox<CommandPtr> CmdMBox;
79 typedef std::shared_ptr<CmdMBox> CmdMBoxPtr;
80
84 typedef SoDa::MailBox<FBufPtr> FDatMBox;
85 typedef std::shared_ptr<FDatMBox> FDatMBoxPtr;
86
90 typedef SoDa::MailBox<CBufPtr> CDatMBox;
91 typedef std::shared_ptr<CDatMBox> CDatMBoxPtr;
92
102 class Base;
103 typedef std::shared_ptr<Base> BasePtr;
104
105 class Base {
106 public:
114 Base(const std::string & oname);
115
117
122 std::string & getObjName() { return objname; }
123
130 BasePtr findSoDaObject(const std::string & oname);
131
138 double getTime();
139
144 BasePtr getSelfPtr() { return self.lock(); }
145
146 private:
147 std::string objname;
148 std::weak_ptr<Base> self;
149
150 static bool first_time;
151 static double base_first_time;
152
153 static std::map<std::string, BasePtr > object_directory;
154 };
155
156 namespace SDR {
166 class Exception {
167 public:
174 Exception(const std::string & _reason, BasePtr obj = NULL)
175 {
176 thrower = obj;
177 reason = _reason;
178 }
179
185 Exception(const char * _reason, BasePtr obj) {
186 thrower = obj;
187 reason = std::string(_reason);
188 }
189
196 Exception(const SoDa::Format & _reason, BasePtr obj) {
197 thrower = obj;
198 reason = _reason.str();
199 }
200
205 const std::string & toString() {
206 if(thrower != NULL) {
207 message = SoDa::Format("SoDa Object [%0] threw exception [%1]\n")
208 .addS(thrower->getObjName())
209 .addS(reason)
210 .str();
211 }
212 else {
213 message = SoDa::Format("Unknown SoDa Object threw exception [%0]\n")
214 .addS(reason)
215 .str();
216 }
217
218 return message;
219 }
220
225 const char * what() {
226 toString();
227 return message.c_str();
228 }
229 private:
231 std::string reason;
232
233 std::string message;
234 };
235
236
237 }
238
248 template<typename BaseT> class Buf {
249 protected:
255 Buf(unsigned int size) {
256 buf.resize(size);
257 };
258
259 public:
260
261 unsigned int size() { return buf.size(); }
262
263 bool copy(const std::shared_ptr<Buf<BaseT>> & src) {
264 buf = src->buf;
265 return true;
266 }
267
272 bool setSize(size_t size) {
273 buf.resize(size);
274 return true;
275 }
276
277 std::vector<BaseT> & getBuf() {
278 return buf;
279 }
280
281 BaseT & operator[](size_t index) {
282 if(index < buf.size()) {
283 return buf[index];
284 }
285 else {
286 throw SDR::Exception(SoDa::Format("Buffer index %0 is out of range (>= %1)")
287 .addI(index).addI(buf.size()).str());
288 }
289 }
290
291 protected:
292 std::vector<BaseT> buf;
293 };
294
299 class FBuf : public Buf<float> {
300 public:
301 FBuf(unsigned int size);
302
303 static FBufPtr make(unsigned int _size);
304 };
305
306
311 class CBuf : public Buf<std::complex<float>> {
312 public:
313 CBuf(unsigned int size);
314
315 static CBufPtr make(unsigned int _size);
316 };
317
318}
The SoDa Base class.
Definition SoDaBase.hxx:105
BasePtr getSelfPtr()
get a pointer to myself.
Definition SoDaBase.hxx:144
Base(const std::string &oname)
The constructor – pass a name for the object.
std::weak_ptr< Base > self
Definition SoDaBase.hxx:148
static double base_first_time
time of first call to getTime from anyone.
Definition SoDaBase.hxx:151
std::string & getObjName()
get the name of this object
Definition SoDaBase.hxx:122
static bool first_time
have we seen the first call to getTime?
Definition SoDaBase.hxx:150
void registerSelf(BasePtr ptr)
static std::map< std::string, BasePtr > object_directory
a class member – directory of all registered objects.
Definition SoDaBase.hxx:153
BasePtr findSoDaObject(const std::string &oname)
find a SoDa Object by name.
double getTime()
Get a time stamp in nS resolution that monotonically increases and that is very inexpensive (typicall...
std::string objname
the name of the object
Definition SoDaBase.hxx:147
BaseT & operator[](size_t index)
Definition SoDaBase.hxx:281
Buf(unsigned int size)
constructor: Allocate a complex/real buffer of complex data values
Definition SoDaBase.hxx:255
bool setSize(size_t size)
set the length of the buffer (in number of complex floats.)
Definition SoDaBase.hxx:272
bool copy(const std::shared_ptr< Buf< BaseT > > &src)
Definition SoDaBase.hxx:263
unsigned int size()
Definition SoDaBase.hxx:261
std::vector< BaseT > buf
Definition SoDaBase.hxx:292
std::vector< BaseT > & getBuf()
Definition SoDaBase.hxx:277
Buf specialized buffer for floats only.
Definition SoDaBase.hxx:311
static CBufPtr make(unsigned int _size)
CBuf(unsigned int size)
Buf specialized buffer for floats only.
Definition SoDaBase.hxx:299
FBuf(unsigned int size)
static FBufPtr make(unsigned int _size)
The SoDa::SDR Exception class (to distinguish it from the SoDaLibrary SoDa::Exception class.
Definition SoDaBase.hxx:166
Exception(const SoDa::Format &_reason, BasePtr obj)
The constructor.
Definition SoDaBase.hxx:196
Exception(const std::string &_reason, BasePtr obj=NULL)
The constructor.
Definition SoDaBase.hxx:174
const std::string & toString()
Create a string that explains this exception.
Definition SoDaBase.hxx:205
const char * what()
Create a string that explains this exception.
Definition SoDaBase.hxx:225
BasePtr thrower
who caused the exception, if anyone?
Definition SoDaBase.hxx:230
std::string reason
what was the cause of the exception?
Definition SoDaBase.hxx:231
Exception(const char *_reason, BasePtr obj)
The constructor.
Definition SoDaBase.hxx:185
std::string message
the reason together with the owner.
Definition SoDaBase.hxx:233
std::shared_ptr< FBuf > FBufPtr
Definition SoDaBase.hxx:61
RXTX
We often need to distinguis between the RX and TX device.
Definition SoDaBase.hxx:69
@ TXRX
Definition SoDaBase.hxx:71
std::shared_ptr< CmdMBox > CmdMBoxPtr
Definition SoDaBase.hxx:79
std::shared_ptr< CDatMBox > CDatMBoxPtr
Definition SoDaBase.hxx:91
std::shared_ptr< CBuf > CBufPtr
Definition SoDaBase.hxx:64
std::shared_ptr< FDatMBox > FDatMBoxPtr
Definition SoDaBase.hxx:85
SoDa::MailBox< CommandPtr > CmdMBox
Mailboxes that carry commands only are of type CmdMBox.
Definition SoDaBase.hxx:78
std::shared_ptr< Base > BasePtr
Definition SoDaBase.hxx:103
SoDa::MailBox< CBufPtr > CDatMBox
Mailboxes that carry float or complex data are of type DatMBox.
Definition SoDaBase.hxx:90
SoDa::MailBox< FBufPtr > FDatMBox
Mailboxes that carry float or complex data are of type DatMBox.
Definition SoDaBase.hxx:84