lsst.astshim  master-gd0c6fad5f5+1
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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 
68  static std::shared_ptr<Object> fromString(std::string const &str) {
69  auto *rawPtr = reinterpret_cast<AstObject *>(astFromString(str.c_str()));
70  return Object::_basicFromAstObject(rawPtr);
71  }
72 
84  template <typename Class>
85  static std::shared_ptr<Class> fromAstObject(AstObject *rawObj, bool copy);
86 
88  std::shared_ptr<Object> copy() const { return std::static_pointer_cast<Object>(copyPolymorphic()); }
89 
98  void clear(std::string const &attrib) {
99  astClear(getRawPtr(), attrib.c_str());
100  assertOK();
101  }
102 
106  bool hasAttribute(std::string const &attrib) const {
107  bool ret = astHasAttribute(getRawPtr(), attrib.c_str());
108  assertOK();
109  return ret;
110  }
111 
118  std::string getClassName() const { return detail::getClassName(getRawPtr()); }
119 
121  std::string getID() const { return getC("ID"); }
122 
124  std::string getIdent() const { return getC("Ident"); }
125 
132  int getNObject() const { return getI("NObject"); }
133 
135  int getObjSize() const { return getI("ObjSize"); }
136 
142  int getRefCount() const { return getI("RefCount"); }
143 
145  bool getUseDefs() const { return getB("UseDefs"); }
146 
181  void lock(bool wait) {
182  astLock(getRawPtr(), static_cast<int>(wait));
183  assertOK();
184  }
185 
191  bool same(Object const &other) const { return astSame(getRawPtr(), other.getRawPtr()); }
192 
194  void setID(std::string const &id) { setC("ID", id); }
195 
197  void setIdent(std::string const &ident) { setC("Ident", ident); }
198 
200  void setUseDefs(bool usedefs) { setB("UseDefs", usedefs); }
201 
207  void show(std::ostream &os) const;
208 
214  std::string show() const;
215 
228  bool test(std::string const &attrib) {
229  bool res = astTest(getRawPtr(), attrib.c_str());
230  assertOK();
231  return res;
232  }
233 
258  void unlock(bool report = false) {
259  astUnlock(getRawPtr(), static_cast<int>(report));
260  assertOK();
261  }
262 
270  AstObject const *getRawPtr() const { return &*_objPtr; };
271 
272  AstObject *getRawPtr() { return &*_objPtr; };
274 
275 protected:
279  explicit Object(AstObject *object) : _objPtr(object, &detail::annulAstObject) {
280  assertOK();
281  if (!object) {
282  throw std::runtime_error("Null pointer");
283  }
284  }
285 
292  template <typename ShimT, typename AstT>
293  static std::shared_ptr<ShimT> makeShim(AstObject *p) {
294  return std::shared_ptr<ShimT>(new ShimT(reinterpret_cast<AstT *>(p)));
295  }
296 
302  template <typename T, typename AstT>
303  std::shared_ptr<T> copyImpl() const {
304  auto *rawptr = reinterpret_cast<AstT *>(astCopy(getRawPtr()));
305  auto retptr = std::shared_ptr<T>(new T(rawptr));
306  assertOK();
307  return retptr;
308  }
309 
322  virtual std::shared_ptr<Object> copyPolymorphic() const = 0;
323 
331  bool getB(std::string const &attrib) const {
332  bool val = astGetI(getRawPtr(), attrib.c_str());
333  assertOK();
334  return val;
335  }
336 
344  std::string const getC(std::string const &attrib) const {
345  char const *rawval = astGetC(getRawPtr(), attrib.c_str());
346  assertOK();
347  return std::string(rawval);
348  }
349 
357  double getD(std::string const &attrib) const {
358  double val = astGetD(getRawPtr(), attrib.c_str());
359  assertOK();
360  return val;
361  }
362 
370  float getF(std::string const &attrib) const {
371  float val = astGetF(getRawPtr(), attrib.c_str());
372  assertOK();
373  return val;
374  }
375 
383  int getI(std::string const &attrib) const {
384  int val = astGetI(getRawPtr(), attrib.c_str());
385  assertOK();
386  return val;
387  }
388 
396  long int getL(std::string const &attrib) const {
397  long int val = astGetL(getRawPtr(), attrib.c_str());
398  assertOK();
399  return val;
400  }
401 
423  void set(std::string const &setting) { astSet(getRawPtr(), "%s", setting.c_str()); }
424 
432  void setB(std::string const &attrib, bool value) {
433  astSetI(getRawPtr(), attrib.c_str(), value);
434  assertOK();
435  }
436 
444  void setC(std::string const &attrib, std::string const &value) {
445  astSetC(getRawPtr(), attrib.c_str(), value.c_str());
446  assertOK();
447  }
448 
456  void setD(std::string const &attrib, double value) {
457  astSetD(getRawPtr(), attrib.c_str(), value);
458  assertOK();
459  }
460 
468  void setF(std::string const &attrib, float value) {
469  astSetF(getRawPtr(), attrib.c_str(), value);
470  assertOK();
471  }
472 
480  void setI(std::string const &attrib, int value) {
481  astSetI(getRawPtr(), attrib.c_str(), value);
482  assertOK();
483  }
484 
492  void setL(std::string const &attrib, long int value) {
493  astSetL(getRawPtr(), attrib.c_str(), value);
494  assertOK();
495  }
496 
497 private:
506  static std::shared_ptr<Object> _basicFromAstObject(AstObject *rawObj);
507 
508  ObjectPtr _objPtr;
509 };
510 
511 } // namespace ast
512 
513 #endif
int getObjSize() const
Get ObjSize: the in-memory size of the AST object in bytes.
Definition: Object.h:135
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:200
void setI(std::string const &attrib, int value)
Definition: Object.h:480
static std::shared_ptr< ShimT > makeShim(AstObject *p)
Definition: Object.h:293
Object(AstObject *object)
Definition: Object.h:279
void setF(std::string const &attrib, float value)
Definition: Object.h:468
std::string getIdent() const
Get Ident: object identification string that is copied.
Definition: Object.h:124
std::shared_ptr< Object > copy() const
Return a deep copy of this object.
Definition: Object.h:88
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:49
static std::shared_ptr< Object > fromString(std::string const &str)
Definition: Object.h:68
bool getUseDefs() const
Get UseDefs: allow use of default values for Object attributes?
Definition: Object.h:145
std::string getID() const
Get ID: object identification string that is not copied.
Definition: Object.h:121
bool hasAttribute(std::string const &attrib) const
Definition: Object.h:106
bool same(Object const &other) const
Definition: Object.h:191
std::string const getC(std::string const &attrib) const
Definition: Object.h:344
void setIdent(std::string const &ident)
Set Ident: object identification string that is copied.
Definition: Object.h:197
void setB(std::string const &attrib, bool value)
Definition: Object.h:432
void setID(std::string const &id)
Set ID: object identification string that is not copied.
Definition: Object.h:194
Definition: Object.h:49
std::unique_ptr< AstObject, Deleter > ObjectPtr
unique pointer holding an AST raw pointer
Definition: Object.h:56
void setC(std::string const &attrib, std::string const &value)
Definition: Object.h:444
Definition: MapSplit.h:38
std::string show() const
Definition: Object.cc:152
void unlock(bool report=false)
Definition: Object.h:258
float getF(std::string const &attrib) const
Definition: Object.h:370
double getD(std::string const &attrib) const
Definition: Object.h:357
void clear(std::string const &attrib)
Definition: Object.h:98
std::shared_ptr< T > copyImpl() const
Definition: Object.h:303
void setL(std::string const &attrib, long int value)
Definition: Object.h:492
void setD(std::string const &attrib, double value)
Definition: Object.h:456
static std::shared_ptr< Class > fromAstObject(AstObject *rawObj, bool copy)
Definition: Object.cc:122
void lock(bool wait)
Definition: Object.h:181
bool test(std::string const &attrib)
Definition: Object.h:228
void set(std::string const &setting)
Definition: Object.h:423
long int getL(std::string const &attrib) const
Definition: Object.h:396
int getI(std::string const &attrib) const
Definition: Object.h:383
int getRefCount() const
Definition: Object.h:142
int getNObject() const
Definition: Object.h:132
bool getB(std::string const &attrib) const
Definition: Object.h:331
std::string getClassName() const
Definition: Object.h:118
AstObject const * getRawPtr() const
Definition: Object.h:270