AAO DRAMA/DRAMA2 C++ Interface
DRAMA C++11 and later interface
parsys.hh
Go to the documentation of this file.
1#ifndef _DRAMA2_PARSYS_INC
2#define _DRAMA2_PARSYS_INC
23/*
24 * History:
25 14-Jul-2014 - TJF - Original version, based on Sdp.h
26 14-Jun-2018 - TJF - Get() method and similar should be const.
27
28 * The above ID is for Doxygen, this one has the format ACMM is looking for.
29 * "@(#) $Id$"
30 */
31
32#include "drama/task.hh"
33#include "drama/sds.hh"
34#include <string>
35#include "Sdp.h"
36#include "DitsParam.h"
37
38
39
40namespace drama {
41
51 class ParSys {
52 friend class ParId;
53 private:
54 /*
55 * Pointer to the DRAMA task this parameter system is part of.
56 */
57 std::weak_ptr<Task> _theTask;
58
59 public:
67 ParSys(std::weak_ptr<Task> dramaTask) : _theTask(dramaTask) {}
68
84 void CreateItem(sds::Id &item) {
85
86 // Lock access to DRAMA.
87 Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
88
90
92 if (status != STATUS__OK)
94 "Failed to create new structured parameter");
95
96 /* Ensure item is not deleted by destructor (id can go) */
97 item.ClearDelete();
98 }
99
112 template <typename T>
113 void Create (const std::string &name, T value) {
114
115 // Lock access to DRAMA.
116 Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
117
118 // Access the parameter system SDS ID.
120
121 // Use the SDS ID to create the item.
122 parSysId.Put(name, value);
123
124 }
135 void Create (const std::string &name, const std::string &value) {
136
137 // Lock access to DRAMA.
138 Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
139
140 // Access the parameter system SDS ID.
142
143 // Use the SDS ID to create the item.
144 parSysId.Put(name, value);
145
146
147
148 }
167 sds::Id CreateSds(const std::string &name, SdsCodeType code, const std::string &extra = "") {
168
169 // Lock access to DRAMA.
170 Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
171
172 // Access the parameter system SDS ID.
174
175
176 // Use sds to create the item
177 return parSysId.CreateChildItem(name, code, extra);
178 }
179
180
186 bool Exists(const std::string &name) const {
187
188 // Lock access to DRAMA.
189 Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
190
191 // Access the parameter system SDS ID.
193 return parSysId.Exists(name);
195
196 private:
197 /*
198 * The SdpPut() template function is used by the Put() method
199 * of ParSys which is implementing traditional Sdp style Put.
200 *
201 * The idea is that we must instantiate a specific version of this
202 * for each primitive SDS type and for strings. This is done
203 * outside the class definition.
204 *
205 * The Put() method won't link unless there is a specific version for
206 * the type in question.
207 *
208 * DRAMA Lock will be taken when invoked.
209 */
210 template <typename T> static void SdpPut(
211 const std::string &name,
212 T value, StatusType *status);
214
215 /*
216 * The SdpGet() template function is used by the Get() method
217 * of ParSys and is implementing traditional Sdp style Get.
218 *
219 * The idea is that we must instantiate a specific version of this
220 * for each primitive SDS type and for strings. These are done
221 * outside the class definition at the bottom of this file.
222 *
223 * The Get() method won't link unless there is a specific version for
224 * the type in question.
225 *
226 * DRAMA Lock will be taken when invoked.
227 */
228 template <typename T> static void SdpGet(
229 const std::string &name,
230 T *value, StatusType *status);
231
232
233 public:
234
253 template <typename T>
254 void Get (const std::string &name, T *value) const {
255
256 // Lock access to DRAMA.
257 Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
258
260 /*
261 There must be a specialization of SdpGet() which
262 supports type T. Note that conversion of DRAMA status
263 to DRAMA 2 exceptions is done here so it need only be
264 done once.
265 */
266 SdpGet(name, value, &status);
267 if (status != STATUS__OK)
269 "SdpGet of parameter % failed",
270 name);
271
272 }
292 template <typename T>
293 void Put (const std::string &name, T value) {
294
295 /*
296 * Confirm the data type is a POD type.
297 * From C++20, std::is_pod<>() is depreciated, we can replace
298 * by std::is_standard_layout<>::value && std::is_trivial<>::value
299 */
300 static_assert(
301 (std::is_standard_layout<T>::value && std::is_trivial<T>::value ) ,
302 "The type T must be a standard layout and trivial (POD) type");
303
304 static_assert(
305 (std::is_integral<T>::value or std::is_floating_point<T>::value),
306 "The type T must be integral or floating point");
307
308 // Lock access to DRAMA.
309 Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
310
312 /*
313 There must be a specialization of SdpPut() which
314 supports type T. Note that conversion of DRAMA status
315 to DRAMA 2 exceptions is done here so it need only be
316 done once.
317 */
318 SdpPut(name, value, &status);
319 if (status != STATUS__OK)
321 "SdpPut of parameter % failed",
322 name);
323
324 }
333 void Put(const std::string &name, const char *value);
334
335
344 void Put (const std::string &name, const std::string &value);
345
351 int GetInt(const std::string &name) const;
358 unsigned int GetUInt(const std::string &name) const;
359
365 long GetLong(const std::string &name) const;
372 unsigned long GetULong(const std::string &name) const;
378 double GetDouble(const std::string &name) const;
379
386 std::string GetString(const std::string &name) const;
387
388
411 void PutSdsCvt(const std::string &name,
412 const sds::Id & value);
435 void PutSdsReplaceByCopy(const std::string &name,
436 const sds::Id &value,
437 const bool create=true );
461 void PutSdsReplaceByInsert(const std::string &name,
463 const bool create=true );
464
483 void Update(const std::string &name);
484
485
486 }; // class ParSys.
487
488 /*
489 * The SdpPut() template function is used by the Put() method
490 * of ParSys.
491 *
492 * The idea is that we must instantiate a specific version of this
493 * for each primitive SDS item (and for strings).
494 *
495 * The Put method won't link unless there is a specific version for
496 * the type in question. These are all implemented in parsys.cpp.
497 */
498 template <> void ParSys::SdpPut<bool>(
499 const std::string &name,
500 bool value, StatusType *status);
501 template <> void ParSys::SdpPut<char>(
502 const std::string &name,
503 char value, StatusType *status);
504 template <> void ParSys::SdpPut<short>(
505 const std::string &name,
506 short value, StatusType *status);
507 template <> void ParSys::SdpPut<unsigned short>(
508 const std::string &name,
509 unsigned short value, StatusType *status);
510 template <> void ParSys::SdpPut<INT32>(
511 const std::string &name,
513 template <> void ParSys::SdpPut<UINT32>(
514 const std::string &name,
516 template <> void ParSys::SdpPut<INT64>(
517 const std::string &name,
519 template <> void ParSys::SdpPut<UINT64>(
520 const std::string &name,
522 template <> void ParSys::SdpPut<float>(
523 const std::string &name,
524 float value, StatusType *status);
525 template <> void ParSys::SdpPut<double>(
526 const std::string &name,
527 double value, StatusType *status);
528
529
530 template <> void ParSys::SdpPut<const char *>(
531 const std::string &name,
532 const char *value, StatusType *status);
533
534 template <> void ParSys::SdpPut<const std::string&>(
535 const std::string &name,
536 const std::string &value, StatusType *status);
537
538 /*
539 * The SdpGet() template function is used by the Get() method
540 * of ParSys.
541 *
542 * The idea is that we must instantiate a specific version of this
543 * for each primitive SDS item (and for strings).
544 *
545 * The Get method won't link unless there is a specific version for
546 * the type in question. These are all implemented in parsys.cpp.
547 *
548 * Note - we have a Get() of an SDS type, but not a Put, there are
549 * specific PutSds... methods in ParSys for different cases.
550 */
551
552
553 template <> void ParSys::SdpGet<bool>(
554 const std::string &name,
555 bool *value, StatusType *status);
556 template <> void ParSys::SdpGet<char>(
557 const std::string &name,
558 char *value, StatusType *status);
559 template <> void ParSys::SdpGet<short>(
560 const std::string &name,
561 short *value, StatusType *status);
562 template <> void ParSys::SdpGet<unsigned short>(
563 const std::string &name,
564 unsigned short *value, StatusType *status);
565 template <> void ParSys::SdpGet<INT32>(
566 const std::string &name,
568 template <> void ParSys::SdpGet<UINT32>(
569 const std::string &name,
571 template <> void ParSys::SdpGet<INT64>(
572 const std::string &name,
574 template <> void ParSys::SdpGet<UINT64>(
575 const std::string &name,
577 template <> void ParSys::SdpGet<float>(
578 const std::string &name,
579 float *value, StatusType *status);
580 template <> void ParSys::SdpGet<double>(
581 const std::string &name,
582 double *value, StatusType *status);
583 template <> void ParSys::SdpGet<std::string>(
584 const std::string &name,
585 std::string *value, StatusType *status);
586#if 0
587 template <> void ParSys::SdpGet<sds::Id>(
588 const std::string &name,
590#endif
591
592
603 class ParId : public sds::Id {
604 private:
605 /*
606 * Pointer to the DRAMA task this parameter system is part of.
607 */
608 std::weak_ptr<Task> _theTask;
609
610 void InvalidCall(const std::string &fromWhere);
611
612 /*
613 * Various SDS methods are not allowed, make them private and
614 * (incase they are invoked in the class itself) replace them
615 * such that they throw an exception.
616 *
617 * We can't just delete these - Under the C++11 standard, a deleted
618 * virtual function may not override a non-deleted virtual function
619 *
620 * We don't need to change the free flag - it is set correctly.
621 */
622 void SetFree() override final {
623 InvalidCall("SetFree()");
624 }
625
626 // Don't allow delete to be set - that would remove the parameter.
627 void SetDelete() override final {
628 InvalidCall("SetDelete()");
629 }
631 // Not needed, delete will never be set.
632 void ClearDelete() override final {
633 InvalidCall("ClearDelete()");
634 }
635
636 // Not needed - outlives anyway.
637 void Outlive() override final {
638 InvalidCall("Outlive()");
639 }
640 // Can't delete- that will kill the parameter, mess up monitors.
641 void Delete() override final {
642 InvalidCall("Delete()");
643 }
644 //Would remove the parameter from the parameter system,mess up monitors
645 void Extract() override final {
646 InvalidCall("Extract()");
647 }
648 // Potential to mess up parameter monitors.
649 void Rename(const std::string &) override final {
650 InvalidCall("Rename()");
651 }
652 // Lots of potential to mess things up.
653 SdsIdType COut(const bool /*outlives*/,
654 bool * const /*free*/ = 0,
655 bool * const /*del*/= 0,
656 bool * const /*readfree*/ = 0) override final {
657
658 InvalidCall("COut()");
659 return 0;
660 }
661 // This replaces the current item - messing up the parameter.
662 void ShallowCopy (Id * /*source*/,
663 const bool /*outlives*/=true) override final {
664 InvalidCall("ShallowCopy()");
665 }
666 // This replaces the current item - messing up the parameter.
667 void ShallowCopy (const Id & /*source*/) override final {
668 InvalidCall("ShallowCopy()");
669 }
670 void ShallowCopy (const SdsIdType /*source*/,
671 const bool /*free*/=false,
672 const bool /*del*/ = false,
673 const bool /*readfree*/ = false) override final {
674 InvalidCall("ShallowCopy()");
675 }
676
677 public:
686 ParId(std::weak_ptr<Task> dramaTask, const std::string& name);
687
697 ParId(ParSys *parSys, const std::string& name);
698
699
705 void Update() const;
708 virtual ~ParId();
709 }; // class ParId
710
711} // namespace drama
712
713#endif /* defined _DRAMA2_SDP_INC */
ParId(ParSys *parSys, const std::string &name)
Access a parameter of a given name.
void Update() const
Indicate to DRAMA that the parameter has been updated.
virtual ~ParId()
Destroy this object.
ParId(std::weak_ptr< Task > dramaTask, const std::string &name)
Access a parameter of a given name.
A class used to access a DRAMA Parameter via an SDS Id.
Definition parsys.hh:630
void CreateItem(sds::Id &item)
Create a new parameter by inserting an SDS item.
Definition parsys.hh:111
void Create(const std::string &name, const std::string &value)
Create a string parameter.
Definition parsys.hh:162
void PutSdsCvt(const std::string &name, const sds::Id &value)
Put an SDS item a named parameter.
unsigned long GetULong(const std::string &name) const
If the Parameter contains scalar value, convert it to an unsigned long integer.
void Update(const std::string &name)
Indicate a parameter value has been updated by other means.
unsigned int GetUInt(const std::string &name) const
If the Parameter contains scalar value, convert it to an unsigned integer.
bool Exists(const std::string &name) const
Does a parameter of a given name exist?
Definition parsys.hh:213
std::string GetString(const std::string &name) const
If the Parameter contains scalar value or a character string convert it to a string.
void Put(const std::string &name, const std::string &value)
Insert a string value into the parameter.
void Get(const std::string &name, T *value) const
Fetch primitive value from parameter.
Definition parsys.hh:281
void Create(const std::string &name, T value)
Create primitive parameter.
Definition parsys.hh:140
ParSys(std::weak_ptr< Task > dramaTask)
Create an object to access the parameter system.
Definition parsys.hh:94
void Put(const std::string &name, T value)
Update parameter from primitive value.
Definition parsys.hh:320
void PutSdsReplaceByCopy(const std::string &name, const sds::Id &value, const bool create=true)
Put a SDS item a named parameter - replacing the existing item.
sds::Id CreateSds(const std::string &name, SdsCodeType code, const std::string &extra="")
Create a parameters as an raw SDS item.
Definition parsys.hh:194
long GetLong(const std::string &name) const
If the Parameter contains scalar value, convert it to long integer.
void Put(const std::string &name, const char *value)
Insert a char * (string) value into the parameter.
int GetInt(const std::string &name) const
If the Parameter contains scalar value, convert it to integer.
void PutSdsReplaceByInsert(const std::string &name, sds::Id *value, const bool create=true)
Put a SDS item a named parameter - replacing the existing value.
double GetDouble(const std::string &name) const
If the Parameter contains scalar value, convert it to a double.
A DRAMA 2 C++ Interface to the DRAMA Simple DITS Parameter System (SDP).
Definition parsys.hh:78
std::lock_guard< mutexType > guardType
Defines the type of a lock guard using our mutex type.
Definition task.hh:460
static Id CreateFromSdsId(const SdsId &item)
Factory constructor method that constructs an sds::Id item from an existing old C++ interface SdsId i...
Id()
Default constructor.
Definition sds.hh:611
A C++ Interface to the handling SDS structures.
Definition sds.hh:428
#define DramaTHROW_S(status_, format_,...)
Throw a Drama exception with safe string formatting.
Definition exception.hh:110
#define DramaTHROW(status_, message_)
Throw a Drama exception.
Definition exception.hh:93
@ Create
Create any temporary memory section.
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1322
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:93
DRAMA 2 include file - Sds class definition.
DRAMA 2 include file - Task class definition.