LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
Object.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2017 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 #ifndef ASTSHIM_OBJECT_H
23 #define ASTSHIM_OBJECT_H
24 
25 #include <ostream>
26 #include <memory>
27 
28 #include "astshim/base.h"
29 #include "astshim/detail/utils.h"
30 #include "astshim/MapSplit.h"
31 
32 namespace ast {
33 
34 class FrameDict;
35 
51 class Object {
52  friend class MapSplit;
53  friend class FrameDict;
54 
55 private:
56  using Deleter = void (*)(AstObject *);
57 
58 public:
60 
61  virtual ~Object() {}
62 
64  Object(Object const &object) : _objPtr(object.getRawPtrCopy(), &detail::annulAstObject) {}
65  Object(Object &&) = default;
66  Object &operator=(Object const &) = delete;
67  Object &operator=(Object &&) = default;
68 
75  bool operator==(Object const &rhs) const;
76 
82  bool operator!=(Object const &rhs) const {
83  return !(*this == rhs); };
84 
89  auto *rawPtr = reinterpret_cast<AstObject *>(astFromString(str.c_str()));
90  return Object::_basicFromAstObject(rawPtr);
91  }
92 
104  template <typename Class>
105  static std::shared_ptr<Class> fromAstObject(AstObject *rawObj, bool copy);
106 
109 
118  void clear(std::string const &attrib) {
119  astClear(getRawPtr(), attrib.c_str());
120  assertOK();
121  }
122 
126  bool hasAttribute(std::string const &attrib) const {
127  bool ret = astHasAttribute(getRawPtr(), attrib.c_str());
128  assertOK();
129  return ret;
130  }
131 
139 
141  std::string getID() const { return getC("ID"); }
142 
144  std::string getIdent() const { return getC("Ident"); }
145 
152  int getNObject() const { return getI("NObject"); }
153 
155  int getObjSize() const { return getI("ObjSize"); }
156 
162  int getRefCount() const { return getI("RefCount"); }
163 
165  bool getUseDefs() const { return getB("UseDefs"); }
166 
201  void lock(bool wait) {
202  astLock(getRawPtr(), static_cast<int>(wait));
203  assertOK();
204  }
205 
211  bool same(Object const &other) const { return astSame(getRawPtr(), other.getRawPtr()); }
212 
214  void setID(std::string const &id) { setC("ID", id); }
215 
217  void setIdent(std::string const &ident) { setC("Ident", ident); }
218 
220  void setUseDefs(bool usedefs) { setB("UseDefs", usedefs); }
221 
228  void show(std::ostream &os, bool showComments = true) const;
229 
235  std::string show(bool showComments = true) const;
236 
249  bool test(std::string const &attrib) const {
250  bool res = astTest(getRawPtr(), attrib.c_str());
251  assertOK();
252  return res;
253  }
254 
279  void unlock(bool report = false) {
280  astUnlock(getRawPtr(), static_cast<int>(report));
281  assertOK();
282  }
283 
291  AstObject const *getRawPtr() const { return &*_objPtr; };
292 
293  AstObject *getRawPtr() { return &*_objPtr; };
295 
296 protected:
300  explicit Object(AstObject *object);
301 
308  template <typename ShimT, typename AstT>
309  static std::shared_ptr<ShimT> makeShim(AstObject *p) {
310  return std::shared_ptr<ShimT>(new ShimT(reinterpret_cast<AstT *>(p)));
311  }
312 
318  template <typename T, typename AstT>
320  auto *rawptr = reinterpret_cast<AstT *>(astCopy(getRawPtr()));
321  auto retptr = std::shared_ptr<T>(new T(rawptr));
322  assertOK();
323  return retptr;
324  }
325 
338  virtual std::shared_ptr<Object> copyPolymorphic() const = 0;
339 
347  bool getB(std::string const &attrib) const {
348  bool val = astGetI(getRawPtr(), attrib.c_str());
349  assertOK();
350  return val;
351  }
352 
360  std::string const getC(std::string const &attrib) const {
361  char const *rawval = astGetC(getRawPtr(), attrib.c_str());
362  assertOK();
363  return std::string(rawval);
364  }
365 
373  double getD(std::string const &attrib) const {
374  double val = astGetD(getRawPtr(), attrib.c_str());
375  assertOK();
376  return val;
377  }
378 
386  float getF(std::string const &attrib) const {
387  float val = astGetF(getRawPtr(), attrib.c_str());
388  assertOK();
389  return val;
390  }
391 
399  int getI(std::string const &attrib) const {
400  int val = astGetI(getRawPtr(), attrib.c_str());
401  assertOK();
402  return val;
403  }
404 
412  long int getL(std::string const &attrib) const {
413  long int val = astGetL(getRawPtr(), attrib.c_str());
414  assertOK();
415  return val;
416  }
417 
439  void set(std::string const &setting) { astSet(getRawPtr(), "%s", setting.c_str()); }
440 
448  void setB(std::string const &attrib, bool value) {
449  astSetI(getRawPtr(), attrib.c_str(), value);
450  assertOK();
451  }
452 
460  void setC(std::string const &attrib, std::string const &value) {
461  astSetC(getRawPtr(), attrib.c_str(), value.c_str());
462  assertOK();
463  }
464 
472  void setD(std::string const &attrib, double value) {
473  astSetD(getRawPtr(), attrib.c_str(), value);
474  assertOK();
475  }
476 
484  void setF(std::string const &attrib, float value) {
485  astSetF(getRawPtr(), attrib.c_str(), value);
486  assertOK();
487  }
488 
496  void setI(std::string const &attrib, int value) {
497  astSetI(getRawPtr(), attrib.c_str(), value);
498  assertOK();
499  }
500 
508  void setL(std::string const &attrib, long int value) {
509  astSetL(getRawPtr(), attrib.c_str(), value);
510  assertOK();
511  }
512 private:
513  /*
514  Given a bare AST object pointer return a shared pointer to an ast::Object of the correct type
515 
516  The returned object takes ownership of the pointer. This is almost always what you want,
517  for instance astDecompose returns shallow copies of the internal pointers.
518 
519  @param[in] rawObj A bare AST object pointer
520  */
521  static std::shared_ptr<Object> _basicFromAstObject(AstObject *rawObj);
522 
523  /*
524  Get a deep copy of the raw AST pointer.
525  */
526  AstObject * getRawPtrCopy() const {
527  AstObject * rawPtrCopy = reinterpret_cast<AstObject *>(astCopy(getRawPtr()));
528  assertOK(rawPtrCopy);
529  return rawPtrCopy;
530  }
531 
532  /*
533  Swap the raw object pointers between this and another object
534  */
535  void swapRawPointers(Object &other) noexcept {
536  swap(_objPtr, other._objPtr);
537  }
538 
539  ObjectPtr _objPtr;
540 };
541 
542 } // namespace ast
543 
544 #endif
bool test(std::string const &attrib) const
Has this attribute been explicitly set (and not subsequently cleared)?
Definition: Object.h:249
AstObject * getRawPtr()
Get the raw AST pointer.
Definition: Object.h:293
Object & operator=(Object const &)=delete
void annulAstObject(AstObject *object)
A wrapper around astAnnul; intended as a custom deleter for std::unique_ptr.
Definition: utils.h:40
AstObject const * getRawPtr() const
Get the raw AST pointer.
Definition: Object.h:291
long int getL(std::string const &attrib) const
Get the value of an attribute as a long int.
Definition: Object.h:412
void setB(std::string const &attrib, bool value)
Set the value of an attribute as a bool.
Definition: Object.h:448
int getI(std::string const &attrib) const
Get the value of an attribute as an int.
Definition: Object.h:399
AST wrapper classes and functions.
std::string getClassName() const
Get Class: the name of the class (e.g.
Definition: Object.h:138
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Throw std::runtime_error if AST&#39;s state is bad.
Definition: base.cc:49
ImageT val
Definition: CR.cc:146
virtual std::shared_ptr< Object > copyPolymorphic() const =0
Return a deep copy of this object.
void setUseDefs(bool usedefs)
Set UseDefs: allow use of default values for Object attributes?
Definition: Object.h:220
std::shared_ptr< T > copyImpl() const
Implementation of deep copy.
Definition: Object.h:319
STL class.
std::shared_ptr< Object > copy() const
Return a deep copy of this object.
Definition: Object.h:108
void setL(std::string const &attrib, long int value)
Set the value of an attribute as a long int.
Definition: Object.h:508
bool hasAttribute(std::string const &attrib) const
Does this object have an attribute with the specified name?
Definition: Object.h:126
static std::shared_ptr< Class > fromAstObject(AstObject *rawObj, bool copy)
Given a bare AST object pointer return a shared pointer to an ast::Object of the correct type...
Definition: Object.cc:132
std::string getID() const
Get ID: object identification string that is not copied.
Definition: Object.h:141
Object(Object const &object)
Copy constructor: make a deep copy.
Definition: Object.h:64
int getObjSize() const
Get ObjSize: the in-memory size of the AST object in bytes.
Definition: Object.h:155
bool getB(std::string const &attrib) const
Get the value of an attribute as a bool.
Definition: Object.h:347
void unlock(bool report=false)
Unlock this object previously locked using lock, so that other threads can use this object...
Definition: Object.h:279
A Mapping split off as a subset of another Mapping.
Definition: MapSplit.h:38
bool getUseDefs() const
Get UseDefs: allow use of default values for Object attributes?
Definition: Object.h:165
void setC(std::string const &attrib, std::string const &value)
Set the value of an attribute as a string.
Definition: Object.h:460
static std::shared_ptr< ShimT > makeShim(AstObject *p)
Functor to make an astshim instance from a raw AST pointer of the corresponding type.
Definition: Object.h:309
T static_pointer_cast(T... args)
void setID(std::string const &id)
Set ID: object identification string that is not copied.
Definition: Object.h:214
virtual ~Object()
Definition: Object.h:61
std::string getIdent() const
Get Ident: object identification string that is copied.
Definition: Object.h:144
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:465
std::string getClassName(AstObject const *rawObj)
Get the AST class name, changing CmpMap to SeriesMap or ParallelMap as appropriate.
Definition: utils.cc:37
void setF(std::string const &attrib, float value)
Set the value of an attribute as a float.
Definition: Object.h:484
bool same(Object const &other) const
Does this contain the same AST object as another?
Definition: Object.h:211
int getRefCount() const
Get RefCount: number of active pointers to the underlying AST object.
Definition: Object.h:162
void setIdent(std::string const &ident)
Set Ident: object identification string that is copied.
Definition: Object.h:217
T c_str(T... args)
void setD(std::string const &attrib, double value)
Set the value of an attribute as a double.
Definition: Object.h:472
int getNObject() const
Get NObject: number of AST objects in existence of the same type as the underlying AST class...
Definition: Object.h:152
double getD(std::string const &attrib) const
Get the value of an attribute as a double.
Definition: Object.h:373
static std::shared_ptr< Object > fromString(std::string const &str)
Construct an Object from a string, using astFromString.
Definition: Object.h:88
ItemVariant const * other
Definition: Schema.cc:56
void show(std::ostream &os, bool showComments=true) const
Print a textual description the object to an ostream.
Definition: Object.cc:152
float getF(std::string const &attrib) const
Get the value of an attribute as a float.
Definition: Object.h:386
void setI(std::string const &attrib, int value)
Set the value of an attribute as an int.
Definition: Object.h:496
std::string const getC(std::string const &attrib) const
Get the value of an attribute as a string.
Definition: Object.h:360
A FrameSet whose frames can be referenced by domain name.
Definition: FrameDict.h:67
Abstract base class for all AST objects.
Definition: Object.h:51
STL class.
bool operator==(Object const &rhs) const
Return True if this and rhs are the equal.
Definition: Object.cc:82
bool operator!=(Object const &rhs) const
Return True if this and rhs are not equal.
Definition: Object.h:82
void clear(std::string const &attrib)
Clear the values of a specified set of attributes for an Object.
Definition: Object.h:118
std::ostream * os
Definition: Schema.cc:746
void lock(bool wait)
Lock this object for exclusive use by the calling thread.
Definition: Object.h:201