lsst.astshim  master-gd1207e8857
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Object.h
1 /*
2  * LSST Data Management System
3  * Copyright 2016 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 private:
52  using Deleter = void (*)(AstObject *);
53 
54 public:
55  using ObjectPtr = std::unique_ptr<AstObject, Deleter>;
56 
57  virtual ~Object() {}
58 
59  Object(Object const &) = delete;
60  Object(Object &&) = default;
61  Object & operator=(Object const &) = delete;
62  Object & operator=(Object &&) = default;
63 
67  static std::shared_ptr<Object> fromString(std::string const & str) {
68  auto * rawPtr = reinterpret_cast<AstObject *>(astFromString(str.c_str()));
69  return Object::_basicFromAstObject(rawPtr);
70  }
71 
83  template <typename Class>
84  static std::shared_ptr<Class> fromAstObject(AstObject *rawObj, bool copy);
85 
87  std::shared_ptr<Object> copy() const { return std::static_pointer_cast<Object>(_copyPolymorphic()); }
88 
97  void clear(std::string const & attrib) {
98  astClear(getRawPtr(), attrib.c_str());
99  assertOK();
100  }
101 
105  bool hasAttribute(std::string const & attrib) const {
106  bool ret = astHasAttribute(getRawPtr(), attrib.c_str());
107  assertOK();
108  return ret;
109  }
110 
117  std::string getClass() const { return detail::getClassName(getRawPtr()); }
118 
120  std::string getID() const { return getC("ID"); }
121 
123  std::string getIdent() const { return getC("Ident"); }
124 
126  int getNobject() const { return getI("Nobject"); }
127 
129  int getObjSize() const { return getI("ObjSize"); }
130 
132  int getRefCount() const { return getI("RefCount"); }
133 
135  bool getUseDefs() const { return getB("UseDefs"); }
136 
171  void lock(bool wait) {
172  astLock(getRawPtr(), static_cast<int>(wait));
173  assertOK();
174  }
175 
181  bool same(Object const & other) const { return astSame(getRawPtr(), other.getRawPtr()); }
182 
184  void setID(std::string const & id) { setC("ID", id); }
185 
187  void setIdent(std::string const & ident) { setC("Ident", ident); }
188 
190  void setUseDefs(bool usedefs) { setB("UseDefs", usedefs); }
191 
197  void show(std::ostream & os) const;
198 
204  std::string show() const;
205 
218  bool test(std::string const & attrib) {
219  bool res = astTest(getRawPtr(), attrib.c_str());
220  assertOK();
221  return res;
222  }
223 
248  void unlock(bool report=false) {
249  astUnlock(getRawPtr(), static_cast<int>(report));
250  assertOK();
251  }
252 
260  AstObject const * getRawPtr() const { return &*_objPtr; };
261 
262  AstObject * getRawPtr() { return &*_objPtr; };
264 
265 protected:
266 
270  explicit Object(AstObject * object) :
271  _objPtr(object, &detail::annulAstObject)
272  {
273  assertOK();
274  if (!object) {
275  throw std::runtime_error("Null pointer");
276  }
277  }
278 
287  static std::shared_ptr<Object> _basicFromAstObject(AstObject *rawObj);
288 
295  template <typename ShimT, typename AstT>
296  static std::shared_ptr<ShimT> makeShim(AstObject * p) {
297  return std::shared_ptr<ShimT>(new ShimT(reinterpret_cast<AstT*>(p)));
298  }
299 
305  template<typename T, typename AstT>
306  std::shared_ptr<T> _copyImpl() const {
307  auto * rawptr = reinterpret_cast<AstT *>(astCopy(getRawPtr()));
308  auto retptr = std::shared_ptr<T>(new T(rawptr));
309  assertOK();
310  return retptr;
311  }
312 
325  virtual std::shared_ptr<Object> _copyPolymorphic() const = 0;
326 
334  bool getB(std::string const & attrib) const {
335  bool val = astGetI(getRawPtr(), attrib.c_str());
336  assertOK();
337  return val;
338  }
339 
347  std::string const getC(std::string const & attrib) const {
348  char const * rawval = astGetC(getRawPtr(), attrib.c_str());
349  assertOK();
350  return std::string(rawval);
351  }
352 
360  double getD(std::string const & attrib) const {
361  double val = astGetD(getRawPtr(), attrib.c_str());
362  assertOK();
363  return val;
364  }
365 
373  float getF(std::string const & attrib) const {
374  float val = astGetF(getRawPtr(), attrib.c_str());
375  assertOK();
376  return val;
377  }
378 
386  int getI(std::string const & attrib) const {
387  int val = astGetI(getRawPtr(), attrib.c_str());
388  assertOK();
389  return val;
390  }
391 
399  long int getL(std::string const & attrib) const {
400  long int val = astGetL(getRawPtr(), attrib.c_str());
401  assertOK();
402  return val;
403  }
404 
426  void set(std::string const & setting) { astSet(getRawPtr(), setting.c_str()); }
427 
435  void setB(std::string const & attrib, bool value) {
436  astSetI(getRawPtr(), attrib.c_str(), value);
437  assertOK();
438  }
439 
447  void setC(std::string const & attrib, std::string const & value) {
448  astSetC(getRawPtr(), attrib.c_str(), value.c_str());
449  assertOK();
450  }
451 
459  void setD(std::string const & attrib, double value) {
460  astSetD(getRawPtr(), attrib.c_str(), value);
461  assertOK();
462  }
463 
471  void setF(std::string const & attrib, float value) {
472  astSetF(getRawPtr(), attrib.c_str(), value);
473  assertOK();
474  }
475 
483  void setI(std::string const & attrib, int value) {
484  astSetI(getRawPtr(), attrib.c_str(), value);
485  assertOK();
486  }
487 
495  void setL(std::string const & attrib, long int value) {
496  astSetL(getRawPtr(), attrib.c_str(), value);
497  assertOK();
498  }
499 
500 private:
501  ObjectPtr _objPtr;
502 };
503 
504 } // namespace ast
505 
506 #endif
int getObjSize() const
Get ObjSize: the in-memory size of the Object in bytes.
Definition: Object.h:129
void setUseDefs(bool usedefs)
Set UseDefs: allow use of default values for Object attributes?
Definition: Object.h:190
void setI(std::string const &attrib, int value)
Definition: Object.h:483
static std::shared_ptr< ShimT > makeShim(AstObject *p)
Definition: Object.h:296
int getNobject() const
Get Nobject: number of Objects in class.
Definition: Object.h:126
Object(AstObject *object)
Definition: Object.h:270
void setF(std::string const &attrib, float value)
Definition: Object.h:471
std::string getIdent() const
Get Ident: object identification string that is copied.
Definition: Object.h:123
std::shared_ptr< Object > copy() const
Return a deep copy of this object.
Definition: Object.h:87
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:53
static std::shared_ptr< Object > fromString(std::string const &str)
Definition: Object.h:67
bool getUseDefs() const
Get UseDefs: allow use of default values for Object attributes?
Definition: Object.h:135
std::string getClass() const
Definition: Object.h:117
std::string getID() const
Get ID: object identification string that is not copied.
Definition: Object.h:120
bool hasAttribute(std::string const &attrib) const
Definition: Object.h:105
virtual std::shared_ptr< Object > _copyPolymorphic() const =0
bool same(Object const &other) const
Definition: Object.h:181
std::string const getC(std::string const &attrib) const
Definition: Object.h:347
void setIdent(std::string const &ident)
Set Ident: object identification string that is copied.
Definition: Object.h:187
void setB(std::string const &attrib, bool value)
Definition: Object.h:435
void setID(std::string const &id)
Set ID: object identification string that is not copied.
Definition: Object.h:184
Definition: Object.h:49
std::unique_ptr< AstObject, Deleter > ObjectPtr
unique pointer holding an AST raw pointer
Definition: Object.h:55
static std::shared_ptr< Object > _basicFromAstObject(AstObject *rawObj)
Definition: Object.cc:78
void setC(std::string const &attrib, std::string const &value)
Definition: Object.h:447
Definition: MapSplit.h:38
std::string show() const
Definition: Object.cc:150
void unlock(bool report=false)
Definition: Object.h:248
float getF(std::string const &attrib) const
Definition: Object.h:373
double getD(std::string const &attrib) const
Definition: Object.h:360
void clear(std::string const &attrib)
Definition: Object.h:97
void setL(std::string const &attrib, long int value)
Definition: Object.h:495
void setD(std::string const &attrib, double value)
Definition: Object.h:459
static std::shared_ptr< Class > fromAstObject(AstObject *rawObj, bool copy)
Definition: Object.cc:120
void lock(bool wait)
Definition: Object.h:171
bool test(std::string const &attrib)
Definition: Object.h:218
void set(std::string const &setting)
Definition: Object.h:426
long int getL(std::string const &attrib) const
Definition: Object.h:399
std::shared_ptr< T > _copyImpl() const
Definition: Object.h:306
int getI(std::string const &attrib) const
Definition: Object.h:386
int getRefCount() const
Get RefCount: count of active Object pointers.
Definition: Object.h:132
bool getB(std::string const &attrib) const
Definition: Object.h:334
AstObject const * getRawPtr() const
Definition: Object.h:260