lsst.astshim  master-g0bd0221cc2
Object.h
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 
49 class Object {
50  friend class MapSplit;
51 
52 private:
53  using Deleter = void (*)(AstObject *);
54 
55 public:
56  using ObjectPtr = std::unique_ptr<AstObject, Deleter>;
57 
58  virtual ~Object() {}
59 
60  Object(Object const &) = delete;
61  Object(Object &&) = default;
62  Object &operator=(Object const &) = delete;
63  Object &operator=(Object &&) = default;
64 
71  bool operator==(Object const &rhs) const;
72 
78  bool operator!=(Object const &rhs) const { return !(*this == rhs); };
79 
83  static std::shared_ptr<Object> fromString(std::string const &str) {
84  auto *rawPtr = reinterpret_cast<AstObject *>(astFromString(str.c_str()));
85  return Object::_basicFromAstObject(rawPtr);
86  }
87 
99  template <typename Class>
100  static std::shared_ptr<Class> fromAstObject(AstObject *rawObj, bool copy);
101 
103  std::shared_ptr<Object> copy() const { return std::static_pointer_cast<Object>(copyPolymorphic()); }
104 
113  void clear(std::string const &attrib) {
114  astClear(getRawPtr(), attrib.c_str());
115  assertOK();
116  }
117 
121  bool hasAttribute(std::string const &attrib) const {
122  bool ret = astHasAttribute(getRawPtr(), attrib.c_str());
123  assertOK();
124  return ret;
125  }
126 
133  std::string getClassName() const { return detail::getClassName(getRawPtr()); }
134 
136  std::string getID() const { return getC("ID"); }
137 
139  std::string getIdent() const { return getC("Ident"); }
140 
147  int getNObject() const { return getI("NObject"); }
148 
150  int getObjSize() const { return getI("ObjSize"); }
151 
157  int getRefCount() const { return getI("RefCount"); }
158 
160  bool getUseDefs() const { return getB("UseDefs"); }
161 
196  void lock(bool wait) {
197  astLock(getRawPtr(), static_cast<int>(wait));
198  assertOK();
199  }
200 
206  bool same(Object const &other) const { return astSame(getRawPtr(), other.getRawPtr()); }
207 
209  void setID(std::string const &id) { setC("ID", id); }
210 
212  void setIdent(std::string const &ident) { setC("Ident", ident); }
213 
215  void setUseDefs(bool usedefs) { setB("UseDefs", usedefs); }
216 
223  void show(std::ostream &os, bool showComments = true) const;
224 
230  std::string show(bool showComments = true) const;
231 
244  bool test(std::string const &attrib) {
245  bool res = astTest(getRawPtr(), attrib.c_str());
246  assertOK();
247  return res;
248  }
249 
274  void unlock(bool report = false) {
275  astUnlock(getRawPtr(), static_cast<int>(report));
276  assertOK();
277  }
278 
286  AstObject const *getRawPtr() const { return &*_objPtr; };
287 
288  AstObject *getRawPtr() { return &*_objPtr; };
290 
291 protected:
295  explicit Object(AstObject *object) : _objPtr(object, &detail::annulAstObject) {
296  assertOK();
297  if (!object) {
298  throw std::runtime_error("Null pointer");
299  }
300  }
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>
319  std::shared_ptr<T> copyImpl() const {
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 
513 private:
522  static std::shared_ptr<Object> _basicFromAstObject(AstObject *rawObj);
523 
524  ObjectPtr _objPtr;
525 };
526 
527 } // namespace ast
528 
529 #endif
AstObject const * getRawPtr() const
Definition: Object.h:286
long int getL(std::string const &attrib) const
Definition: Object.h:412
void setB(std::string const &attrib, bool value)
Definition: Object.h:448
int getI(std::string const &attrib) const
Definition: Object.h:399
AST wrapper classes and functions.
Definition: attributes_channel.dox:1
bool test(std::string const &attrib)
Definition: Object.h:244
std::string getClassName() const
Definition: Object.h:133
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:49
virtual std::shared_ptr< Object > copyPolymorphic() const =0
void setUseDefs(bool usedefs)
Set UseDefs: allow use of default values for Object attributes?
Definition: Object.h:215
std::shared_ptr< T > copyImpl() const
Definition: Object.h:319
std::shared_ptr< Object > copy() const
Return a deep copy of this object.
Definition: Object.h:103
void setL(std::string const &attrib, long int value)
Definition: Object.h:508
std::unique_ptr< AstObject, Deleter > ObjectPtr
unique pointer holding an AST raw pointer
Definition: Object.h:56
bool hasAttribute(std::string const &attrib) const
Definition: Object.h:121
static std::shared_ptr< Class > fromAstObject(AstObject *rawObj, bool copy)
Definition: Object.cc:129
std::string getID() const
Get ID: object identification string that is not copied.
Definition: Object.h:136
int getObjSize() const
Get ObjSize: the in-memory size of the AST object in bytes.
Definition: Object.h:150
bool getB(std::string const &attrib) const
Definition: Object.h:347
void unlock(bool report=false)
Definition: Object.h:274
Definition: MapSplit.h:38
bool getUseDefs() const
Get UseDefs: allow use of default values for Object attributes?
Definition: Object.h:160
void setC(std::string const &attrib, std::string const &value)
Definition: Object.h:460
static std::shared_ptr< ShimT > makeShim(AstObject *p)
Definition: Object.h:309
void setID(std::string const &id)
Set ID: object identification string that is not copied.
Definition: Object.h:209
std::string getIdent() const
Get Ident: object identification string that is copied.
Definition: Object.h:139
void setF(std::string const &attrib, float value)
Definition: Object.h:484
bool same(Object const &other) const
Definition: Object.h:206
int getRefCount() const
Definition: Object.h:157
void setIdent(std::string const &ident)
Set Ident: object identification string that is copied.
Definition: Object.h:212
void setD(std::string const &attrib, double value)
Definition: Object.h:472
int getNObject() const
Definition: Object.h:147
double getD(std::string const &attrib) const
Definition: Object.h:373
static std::shared_ptr< Object > fromString(std::string const &str)
Definition: Object.h:83
void show(std::ostream &os, bool showComments=true) const
Definition: Object.cc:149
float getF(std::string const &attrib) const
Definition: Object.h:386
void setI(std::string const &attrib, int value)
Definition: Object.h:496
Object(AstObject *object)
Definition: Object.h:295
std::string const getC(std::string const &attrib) const
Definition: Object.h:360
Definition: Object.h:49
bool operator==(Object const &rhs) const
Definition: Object.cc:80
bool operator!=(Object const &rhs) const
Definition: Object.h:78
void clear(std::string const &attrib)
Definition: Object.h:113
void lock(bool wait)
Definition: Object.h:196