lsst.astshim  master-ga73c468035
 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 getClass() const { return detail::getClassName(getRawPtr()); }
119 
121  std::string getID() const { return getC("ID"); }
122 
124  std::string getIdent() const { return getC("Ident"); }
125 
127  int getNobject() const { return getI("Nobject"); }
128 
130  int getObjSize() const { return getI("ObjSize"); }
131 
133  int getRefCount() const { return getI("RefCount"); }
134 
136  bool getUseDefs() const { return getB("UseDefs"); }
137 
172  void lock(bool wait) {
173  astLock(getRawPtr(), static_cast<int>(wait));
174  assertOK();
175  }
176 
182  bool same(Object const &other) const { return astSame(getRawPtr(), other.getRawPtr()); }
183 
185  void setID(std::string const &id) { setC("ID", id); }
186 
188  void setIdent(std::string const &ident) { setC("Ident", ident); }
189 
191  void setUseDefs(bool usedefs) { setB("UseDefs", usedefs); }
192 
198  void show(std::ostream &os) const;
199 
205  std::string show() const;
206 
219  bool test(std::string const &attrib) {
220  bool res = astTest(getRawPtr(), attrib.c_str());
221  assertOK();
222  return res;
223  }
224 
249  void unlock(bool report = false) {
250  astUnlock(getRawPtr(), static_cast<int>(report));
251  assertOK();
252  }
253 
261  AstObject const *getRawPtr() const { return &*_objPtr; };
262 
263  AstObject *getRawPtr() { return &*_objPtr; };
265 
266 protected:
270  explicit Object(AstObject *object) : _objPtr(object, &detail::annulAstObject) {
271  assertOK();
272  if (!object) {
273  throw std::runtime_error("Null pointer");
274  }
275  }
276 
283  template <typename ShimT, typename AstT>
284  static std::shared_ptr<ShimT> makeShim(AstObject *p) {
285  return std::shared_ptr<ShimT>(new ShimT(reinterpret_cast<AstT *>(p)));
286  }
287 
293  template <typename T, typename AstT>
294  std::shared_ptr<T> copyImpl() const {
295  auto *rawptr = reinterpret_cast<AstT *>(astCopy(getRawPtr()));
296  auto retptr = std::shared_ptr<T>(new T(rawptr));
297  assertOK();
298  return retptr;
299  }
300 
313  virtual std::shared_ptr<Object> copyPolymorphic() const = 0;
314 
322  bool getB(std::string const &attrib) const {
323  bool val = astGetI(getRawPtr(), attrib.c_str());
324  assertOK();
325  return val;
326  }
327 
335  std::string const getC(std::string const &attrib) const {
336  char const *rawval = astGetC(getRawPtr(), attrib.c_str());
337  assertOK();
338  return std::string(rawval);
339  }
340 
348  double getD(std::string const &attrib) const {
349  double val = astGetD(getRawPtr(), attrib.c_str());
350  assertOK();
351  return val;
352  }
353 
361  float getF(std::string const &attrib) const {
362  float val = astGetF(getRawPtr(), attrib.c_str());
363  assertOK();
364  return val;
365  }
366 
374  int getI(std::string const &attrib) const {
375  int val = astGetI(getRawPtr(), attrib.c_str());
376  assertOK();
377  return val;
378  }
379 
387  long int getL(std::string const &attrib) const {
388  long int val = astGetL(getRawPtr(), attrib.c_str());
389  assertOK();
390  return val;
391  }
392 
414  void set(std::string const &setting) { astSet(getRawPtr(), "%s", setting.c_str()); }
415 
423  void setB(std::string const &attrib, bool value) {
424  astSetI(getRawPtr(), attrib.c_str(), value);
425  assertOK();
426  }
427 
435  void setC(std::string const &attrib, std::string const &value) {
436  astSetC(getRawPtr(), attrib.c_str(), value.c_str());
437  assertOK();
438  }
439 
447  void setD(std::string const &attrib, double value) {
448  astSetD(getRawPtr(), attrib.c_str(), value);
449  assertOK();
450  }
451 
459  void setF(std::string const &attrib, float value) {
460  astSetF(getRawPtr(), attrib.c_str(), value);
461  assertOK();
462  }
463 
471  void setI(std::string const &attrib, int value) {
472  astSetI(getRawPtr(), attrib.c_str(), value);
473  assertOK();
474  }
475 
483  void setL(std::string const &attrib, long int value) {
484  astSetL(getRawPtr(), attrib.c_str(), value);
485  assertOK();
486  }
487 
488 private:
497  static std::shared_ptr<Object> _basicFromAstObject(AstObject *rawObj);
498 
499  ObjectPtr _objPtr;
500 };
501 
502 } // namespace ast
503 
504 #endif
int getObjSize() const
Get ObjSize: the in-memory size of the Object in bytes.
Definition: Object.h:130
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:191
void setI(std::string const &attrib, int value)
Definition: Object.h:471
static std::shared_ptr< ShimT > makeShim(AstObject *p)
Definition: Object.h:284
int getNobject() const
Get Nobject: number of Objects in class.
Definition: Object.h:127
Object(AstObject *object)
Definition: Object.h:270
void setF(std::string const &attrib, float value)
Definition: Object.h:459
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:136
std::string getClass() const
Definition: Object.h:118
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:182
std::string const getC(std::string const &attrib) const
Definition: Object.h:335
void setIdent(std::string const &ident)
Set Ident: object identification string that is copied.
Definition: Object.h:188
void setB(std::string const &attrib, bool value)
Definition: Object.h:423
void setID(std::string const &id)
Set ID: object identification string that is not copied.
Definition: Object.h:185
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:435
Definition: MapSplit.h:38
std::string show() const
Definition: Object.cc:152
void unlock(bool report=false)
Definition: Object.h:249
float getF(std::string const &attrib) const
Definition: Object.h:361
double getD(std::string const &attrib) const
Definition: Object.h:348
void clear(std::string const &attrib)
Definition: Object.h:98
std::shared_ptr< T > copyImpl() const
Definition: Object.h:294
void setL(std::string const &attrib, long int value)
Definition: Object.h:483
void setD(std::string const &attrib, double value)
Definition: Object.h:447
static std::shared_ptr< Class > fromAstObject(AstObject *rawObj, bool copy)
Definition: Object.cc:122
void lock(bool wait)
Definition: Object.h:172
bool test(std::string const &attrib)
Definition: Object.h:219
void set(std::string const &setting)
Definition: Object.h:414
long int getL(std::string const &attrib) const
Definition: Object.h:387
int getI(std::string const &attrib) const
Definition: Object.h:374
int getRefCount() const
Get RefCount: count of active Object pointers.
Definition: Object.h:133
bool getB(std::string const &attrib) const
Definition: Object.h:322
AstObject const * getRawPtr() const
Definition: Object.h:261