AAO DRAMA/DRAMA2 C++ Interface
parameter.hh
Go to the documentation of this file.
1 #ifndef _DRAMA2_PARAMETER_INC
2 #define _DRAMA2_PARAMETER_INC
3 
20 /*
21  * History:
22  07-Jan-2014 - TJF - Original version
23 
24  * The above ID is for Doxygen, this one has the format ACMM is looking for.
25  * "@(#) $Id: ACMM:Drama2/drama/parameter.hh,v 1.45+ 22-Feb-2016 14:09:57+11 tjf $"
26 */
27 
28 #include "drama/task.hh"
29 #include "drama/sds.hh"
30 #include <string>
31 #include "Sdp.h"
32 #include "DitsParam.h"
33 #include "DitsMonitor.h"
34 
35 namespace drama {
36 
37 
73  template<typename SDS_T> class Parameter {
74 
75  private:
76 
77  /*
78  * Pointer to the DRAMA task this parameter is part of.
79  */
80  std::weak_ptr<Task> _theTask;
81  const std::string _name;
82  public:
85  typedef Parameter<SDS_T> class_type;
95  Parameter(std::weak_ptr<Task> task,
96  const std::string &name,
97  const SDS_T &initVal) : _theTask(task), _name(name) {
98  //SdsCodeType code = SdsTypes::CodeForType<SDS_T>();
99 
100  /*
101  * Access the parameter system and insert the value.
102  */
103  sds::Id parSys = sds::Id::CreateFromSdsIdType((long)(DitsGetParId()));
104  parSys.Put(name, initVal);
105  }
113  class_type &operator =(SDS_T val) {
114 
115  // Parameter updates trigger monitors and possible entry to
116  // task, so must take lock.
117  Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
118 
119  // Fill in code to set value.
120  StatusType status = STATUS__OK;
121  Sdp::Put(_name, val, &status);
123  if (status != STATUS__OK)
124  DramaTHROW_S(status,"Failed setting parameter %",
125  _name);
126 
127  return *this;
128  }
133  operator SDS_T() const {
134  SDS_T val;
135  StatusType status = STATUS__OK;
136  Sdp::Get(_name, &val, &status);
137 
138  if (status != STATUS__OK)
139  DramaTHROW_S(status,"Failed fetching parameter %",
140  _name);
141 
142  return val;
143  }
150  virtual ~Parameter() {
151  }
152 
153  };
154 
155 
189  template<> class Parameter<drama::sds::Id> {
190 
191  private:
192  /*
193  * Pointer to the DRAMA task this parameter is part of.
194  */
195  std::weak_ptr<Task> _theTask;
196  const std::string _name; // Name of this parameter.
197  sds::Id _parSys; // Reference to task parameter system.
198  public:
201  typedef Parameter<drama::sds::Id> class_type;
213  Parameter(std::weak_ptr<Task> task,
214  const std::string &name,
215  const sds::IdPtr &initVal,
216  bool copy = true) : _theTask(task), _name(name),
217  _parSys(sds::Id::CreateFromSdsIdType((long)(DitsGetParId()))) {
218  Set(initVal, copy, true);
219  }
220 
241  void Set(const sds::IdPtr &newVal,
242  bool copy = true,
243  bool create = false) {
244 
245 
246  /*
247  * Parameter updates trigger monitors and possible entry to
248  * task, so must take lock.
249  */
250  Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
251 
252  /*
253  * The Code here is an adaption of the algorithm in SdpPutStruct().
254  */
255  SdsCodeType code = newVal->GetCode();
256 
257  std::vector<unsigned long> dims;
258  newVal->GetDims(&dims);
259 
260  if ((code != SDS_STRUCT)&&(dims.size() == 0))
261  {
262  /* C code (SdsPutStruct()) would complain here, should I ? */
263  }
264 
265  /*
266  * Access the parameter system and look for a parameter of this name.
267  */
268  if (_parSys.Exists(_name))
269  {
270  /*
271  * Delete the existing value, since the layout of the
272  * structure might be changing, better to recreate the
273  * parameter.
274  */
275  sds::Id par = _parSys.Find(_name);
276  par.Delete();
277  }
278  else if (!create)
279  {
280  /*
281  * User did not want to create it, but it does not exist, so
282  * complain.
283  */
284  DramaTHROW_S(SDS__NOITEM,
285  "Error updating parameter %, does not exist",
286  _name);
287  }
288  /*
289  * At this point, we need to create the parameter (if it existed, then
290  * we deleted it.)
291  */
292  StatusType status = STATUS__OK;
293  if (copy)
294  {
295  /*
296  * We have to make an SDS Copy of the new value and
297  * insert that.
298  */
299  sds::Id tid = newVal->Copy();
300  tid.Rename(_name);
301  _parSys.Insert(tid);
302 
303  StatusType status = STATUS__OK;
304  DitsMonitor(_name.c_str(),DITS_MON_TRUST,(SdsIdType)(tid),&status);
305  }
306  else
307  {
308  /*
309  * We are free to use the SDS item we have been passed.
310  */
311 
312  newVal->Rename(_name);
313  _parSys.Insert(*newVal);
314  DitsMonitor(_name.c_str(),DITS_MON_TRUST,(SdsIdType)(*newVal),&status);
315  }
316  if (status != STATUS__OK)
317  {
318  DramaTHROW_S(status,
319  "Error updating parameter %, DitsMonitor() failed",
320  _name);
321  }
322 
323  }
324 
338  sds::Id Get() const {
339  return _parSys.Find(_name);
340  }
341 
349  void UpdateMonitors() const {
350 
351  /*
352  *Parameter updates trigger monitors and possible entry to
353  * task, so must take lock.
354  */
355  Task::guardType DramaLock(std::shared_ptr<Task>(_theTask)->Lock());
356 
357 
358  StatusType status;
359  DitsMonitor(_name.c_str(),DITS_MON_TRUST,(SdsIdType)(_parSys.Find(_name)),&status);
360  if (status != STATUS__OK)
361  {
362  DramaTHROW_S(status,
363  "Error updating parameter %, DitsMonitor() failed",
364  _name);
365  }
366 
367  }
368 
375  virtual ~Parameter() {
376  }
377 
378  };
379 
380 
381 
382 } // namespace drama
383 
384 #endif
DRAMA 2 include file - Task class definition.
#define DramaTHROW_S(status_, format_,...)
Throw a Drama exception with safe string formatting.
Definition: exception.hh:112
class_type & operator=(SDS_T val)
Set the value of the parameter to the specified value.
Definition: parameter.hh:140
DRAMA 2 include file - Sds class definition.
virtual void Delete()
Delete the SDS item.
Definition: sds.hh:1174
std::lock_guard< mutexType > guardType
Defines the type of a lock guard using our mutex type.
Definition: task.hh:322
A C++ Interface to the handling SDS structures.
Definition: sds.hh:412
static Id CreateFromSdsIdType(const SdsIdType item, const bool free=false, const bool del=false, const bool readfree=false)
Factory constructor method that constructs an sds::Id item from an existing C language SDS id...
std::shared_ptr< Id > IdPtr
A shared pointer for sds::Id items.
Definition: sds.hh:2962
Parameter< SDS_T > class_type
The type of this template instantiation.
Definition: parameter.hh:112
virtual Id Find(const std::string &name, bool throwOnNotFound=true) const
Factory constructor method Constructor which returns a reference to a named item. ...
virtual ~Parameter()
Parameter Destructor.
Definition: parameter.hh:177
Parameter(std::weak_ptr< Task > task, const std::string &name, const SDS_T &initVal)
Construct a parameter of a given name.
Definition: parameter.hh:122
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition: drama.hh:89
void Put(const unsigned long length, const T *const data, const unsigned long offset=0)
Put data into an SDS item.
Definition: sds.hh:1737

Click here for the DRAMA home page and here for the AAO home page.

For more information, contact tjf@aao.gov.au 

Generated on Mon Feb 22 2016 15:57:52 for AAO DRAMA/DRAMA2 C++ Interface by doxygen 1.8.10