LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
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  assertOK(rawPtr);
91  return Object::_basicFromAstObject(rawPtr);
92  }
93 
105  template <typename Class>
106  static std::shared_ptr<Class> fromAstObject(AstObject *rawObj, bool copy);
107 
109  std::shared_ptr<Object> copy() const { return std::static_pointer_cast<Object>(copyPolymorphic()); }
110 
119  void clear(std::string const &attrib) {
120  astClear(getRawPtr(), attrib.c_str());
121  assertOK();
122  }
123 
127  bool hasAttribute(std::string const &attrib) const {
128  bool ret = astHasAttribute(getRawPtr(), attrib.c_str());
129  assertOK();
130  return ret;
131  }
132 
140 
142  std::string getID() const { return getC("ID"); }
143 
145  std::string getIdent() const { return getC("Ident"); }
146 
153  int getNObject() const { return getI("NObject"); }
154 
156  int getObjSize() const { return getI("ObjSize"); }
157 
163  int getRefCount() const { return getI("RefCount"); }
164 
166  bool getUseDefs() const { return getB("UseDefs"); }
167 
202  void lock(bool wait) {
203  astLock(getRawPtr(), static_cast<int>(wait));
204  assertOK();
205  }
206 
212  bool same(Object const &other) const { return astSame(getRawPtr(), other.getRawPtr()); }
213 
215  void setID(std::string const &id) { setC("ID", id); }
216 
218  void setIdent(std::string const &ident) { setC("Ident", ident); }
219 
221  void setUseDefs(bool usedefs) { setB("UseDefs", usedefs); }
222 
229  void show(std::ostream &os, bool showComments = true) const;
230 
236  std::string show(bool showComments = true) const;
237 
250  bool test(std::string const &attrib) const {
251  bool res = astTest(getRawPtr(), attrib.c_str());
252  assertOK();
253  return res;
254  }
255 
280  void unlock(bool report = false) {
281  astUnlock(getRawPtr(), static_cast<int>(report));
282  assertOK();
283  }
284 
292  AstObject const *getRawPtr() const { return &*_objPtr; };
293 
294  AstObject *getRawPtr() { return &*_objPtr; };
296 
297 protected:
301  explicit Object(AstObject *object);
302 
309  template <typename ShimT, typename AstT>
310  static std::shared_ptr<ShimT> makeShim(AstObject *p) {
311  return std::shared_ptr<ShimT>(new ShimT(reinterpret_cast<AstT *>(p)));
312  }
313 
319  template <typename T, typename AstT>
321  auto *rawptr = reinterpret_cast<AstT *>(astCopy(getRawPtr()));
322  auto retptr = std::shared_ptr<T>(new T(rawptr));
323  assertOK();
324  return retptr;
325  }
326 
340 
348  bool getB(std::string const &attrib) const {
349  bool val = astGetI(getRawPtr(), attrib.c_str());
350  assertOK();
351  return val;
352  }
353 
361  std::string const getC(std::string const &attrib) const {
362  char const *rawval = astGetC(getRawPtr(), attrib.c_str());
363  assertOK();
364  return std::string(rawval);
365  }
366 
374  double getD(std::string const &attrib) const {
375  double val = astGetD(getRawPtr(), attrib.c_str());
376  assertOK();
377  return val;
378  }
379 
387  float getF(std::string const &attrib) const {
388  float val = astGetF(getRawPtr(), attrib.c_str());
389  assertOK();
390  return val;
391  }
392 
400  int getI(std::string const &attrib) const {
401  int val = astGetI(getRawPtr(), attrib.c_str());
402  assertOK();
403  return val;
404  }
405 
413  long int getL(std::string const &attrib) const {
414  long int val = astGetL(getRawPtr(), attrib.c_str());
415  assertOK();
416  return val;
417  }
418 
440  void set(std::string const &setting) {
441  astSet(getRawPtr(), "%s", setting.c_str());
442  assertOK();
443  }
444 
452  void setB(std::string const &attrib, bool value) {
453  astSetI(getRawPtr(), attrib.c_str(), value);
454  assertOK();
455  }
456 
464  void setC(std::string const &attrib, std::string const &value) {
465  astSetC(getRawPtr(), attrib.c_str(), value.c_str());
466  assertOK();
467  }
468 
476  void setD(std::string const &attrib, double value) {
477  astSetD(getRawPtr(), attrib.c_str(), value);
478  assertOK();
479  }
480 
488  void setF(std::string const &attrib, float value) {
489  astSetF(getRawPtr(), attrib.c_str(), value);
490  assertOK();
491  }
492 
500  void setI(std::string const &attrib, int value) {
501  astSetI(getRawPtr(), attrib.c_str(), value);
502  assertOK();
503  }
504 
512  void setL(std::string const &attrib, long int value) {
513  astSetL(getRawPtr(), attrib.c_str(), value);
514  assertOK();
515  }
516 private:
517  /*
518  Given a bare AST object pointer return a shared pointer to an ast::Object of the correct type
519 
520  The returned object takes ownership of the pointer. This is almost always what you want,
521  for instance astDecompose returns shallow copies of the internal pointers.
522 
523  @param[in] rawObj A bare AST object pointer
524  */
525  static std::shared_ptr<Object> _basicFromAstObject(AstObject *rawObj);
526 
527  /*
528  Get a deep copy of the raw AST pointer.
529  */
530  AstObject * getRawPtrCopy() const {
531  AstObject * rawPtrCopy = reinterpret_cast<AstObject *>(astCopy(getRawPtr()));
532  assertOK(rawPtrCopy);
533  return rawPtrCopy;
534  }
535 
536  /*
537  Swap the raw object pointers between this and another object
538  */
539  void swapRawPointers(Object &other) noexcept {
540  swap(_objPtr, other._objPtr);
541  }
542 
543  ObjectPtr _objPtr;
544 };
545 
546 } // namespace ast
547 
548 #endif
ItemVariant const * other
Definition: Schema.cc:56
std::ostream * os
Definition: Schema.cc:746
T c_str(T... args)
A FrameSet whose frames can be referenced by domain name.
Definition: FrameDict.h:67
A Mapping split off as a subset of another Mapping.
Definition: MapSplit.h:38
Abstract base class for all AST objects.
Definition: Object.h:51
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:138
Object(Object &&)=default
void lock(bool wait)
Lock this object for exclusive use by the calling thread.
Definition: Object.h:202
void show(std::ostream &os, bool showComments=true) const
Print a textual description the object to an ostream.
Definition: Object.cc:158
AstObject * getRawPtr()
Get the raw AST pointer.
Definition: Object.h:294
bool same(Object const &other) const
Does this contain the same AST object as another?
Definition: Object.h:212
Object & operator=(Object const &)=delete
void setD(std::string const &attrib, double value)
Set the value of an attribute as a double.
Definition: Object.h:476
bool operator!=(Object const &rhs) const
Return True if this and rhs are not equal.
Definition: Object.h:82
double getD(std::string const &attrib) const
Get the value of an attribute as a double.
Definition: Object.h:374
void setL(std::string const &attrib, long int value)
Set the value of an attribute as a long int.
Definition: Object.h:512
long int getL(std::string const &attrib) const
Get the value of an attribute as a long int.
Definition: Object.h:413
std::string const getC(std::string const &attrib) const
Get the value of an attribute as a string.
Definition: Object.h:361
std::string getClassName() const
Get Class: the name of the class (e.g.
Definition: Object.h:139
std::shared_ptr< Object > copy() const
Return a deep copy of this object.
Definition: Object.h:109
Object & operator=(Object &&)=default
void setI(std::string const &attrib, int value)
Set the value of an attribute as an int.
Definition: Object.h:500
static std::shared_ptr< Object > fromString(std::string const &str)
Construct an Object from a string, using astFromString.
Definition: Object.h:88
bool test(std::string const &attrib) const
Has this attribute been explicitly set (and not subsequently cleared)?
Definition: Object.h:250
bool getB(std::string const &attrib) const
Get the value of an attribute as a bool.
Definition: Object.h:348
bool getUseDefs() const
Get UseDefs: allow use of default values for Object attributes?
Definition: Object.h:166
void setID(std::string const &id)
Set ID: object identification string that is not copied.
Definition: Object.h:215
std::string getID() const
Get ID: object identification string that is not copied.
Definition: Object.h:142
AstObject const * getRawPtr() const
Get the raw AST pointer.
Definition: Object.h:292
void clear(std::string const &attrib)
Clear the values of a specified set of attributes for an Object.
Definition: Object.h:119
std::shared_ptr< T > copyImpl() const
Implementation of deep copy.
Definition: Object.h:320
virtual ~Object()
Definition: Object.h:61
int getI(std::string const &attrib) const
Get the value of an attribute as an int.
Definition: Object.h:400
void setF(std::string const &attrib, float value)
Set the value of an attribute as a float.
Definition: Object.h:488
void unlock(bool report=false)
Unlock this object previously locked using lock, so that other threads can use this object.
Definition: Object.h:280
std::unique_ptr< AstObject, Deleter > ObjectPtr
unique pointer holding an AST raw pointer
Definition: Object.h:59
void setC(std::string const &attrib, std::string const &value)
Set the value of an attribute as a string.
Definition: Object.h:464
float getF(std::string const &attrib) const
Get the value of an attribute as a float.
Definition: Object.h:387
Object(Object const &object)
Copy constructor: make a deep copy.
Definition: Object.h:64
virtual std::shared_ptr< Object > copyPolymorphic() const =0
Return a deep copy of this object.
std::string getIdent() const
Get Ident: object identification string that is copied.
Definition: Object.h:145
void set(std::string const &setting)
Assign a set of attribute values, over-riding any previous values.
Definition: Object.h:440
int getObjSize() const
Get ObjSize: the in-memory size of the AST object in bytes.
Definition: Object.h:156
void setB(std::string const &attrib, bool value)
Set the value of an attribute as a bool.
Definition: Object.h:452
int getNObject() const
Get NObject: number of AST objects in existence of the same type as the underlying AST class.
Definition: Object.h:153
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:310
void setIdent(std::string const &ident)
Set Ident: object identification string that is copied.
Definition: Object.h:218
bool hasAttribute(std::string const &attrib) const
Does this object have an attribute with the specified name?
Definition: Object.h:127
void setUseDefs(bool usedefs)
Set UseDefs: allow use of default values for Object attributes?
Definition: Object.h:221
bool operator==(Object const &rhs) const
Return True if this and rhs are the equal.
Definition: Object.cc:85
int getRefCount() const
Get RefCount: number of active pointers to the underlying AST object.
Definition: Object.h:163
void annulAstObject(AstObject *object)
A wrapper around astAnnul; intended as a custom deleter for std::unique_ptr.
Definition: utils.h:40
std::string getClassName(AstObject const *rawObj)
Get the AST class name, changing CmpMap to SeriesMap or ParallelMap as appropriate.
Definition: utils.cc:37
AST wrapper classes and functions.
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Throw std::runtime_error if AST's state is bad.
Definition: base.cc:49
void swap(CameraSys &a, CameraSys &b)
Definition: CameraSys.h:157
ImageT val
Definition: CR.cc:146