lsst.astshim  14.0-10-ga7aaa25+4
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 
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:
59  using ObjectPtr = std::unique_ptr<AstObject, Deleter>;
60 
61  virtual ~Object() {}
62 
63  Object(Object const &) = delete;
64  Object(Object &&) = default;
65  Object &operator=(Object const &) = delete;
66  Object &operator=(Object &&) = default;
67 
74  bool operator==(Object const &rhs) const;
75 
81  bool operator!=(Object const &rhs) const { return !(*this == rhs); };
82 
86  static std::shared_ptr<Object> fromString(std::string const &str) {
87  auto *rawPtr = reinterpret_cast<AstObject *>(astFromString(str.c_str()));
88  return Object::_basicFromAstObject(rawPtr);
89  }
90 
102  template <typename Class>
103  static std::shared_ptr<Class> fromAstObject(AstObject *rawObj, bool copy);
104 
106  std::shared_ptr<Object> copy() const { return std::static_pointer_cast<Object>(copyPolymorphic()); }
107 
116  void clear(std::string const &attrib) {
117  astClear(getRawPtr(), attrib.c_str());
118  assertOK();
119  }
120 
124  bool hasAttribute(std::string const &attrib) const {
125  bool ret = astHasAttribute(getRawPtr(), attrib.c_str());
126  assertOK();
127  return ret;
128  }
129 
136  std::string getClassName() const { return detail::getClassName(getRawPtr()); }
137 
139  std::string getID() const { return getC("ID"); }
140 
142  std::string getIdent() const { return getC("Ident"); }
143 
150  int getNObject() const { return getI("NObject"); }
151 
153  int getObjSize() const { return getI("ObjSize"); }
154 
160  int getRefCount() const { return getI("RefCount"); }
161 
163  bool getUseDefs() const { return getB("UseDefs"); }
164 
199  void lock(bool wait) {
200  astLock(getRawPtr(), static_cast<int>(wait));
201  assertOK();
202  }
203 
209  bool same(Object const &other) const { return astSame(getRawPtr(), other.getRawPtr()); }
210 
212  void setID(std::string const &id) { setC("ID", id); }
213 
215  void setIdent(std::string const &ident) { setC("Ident", ident); }
216 
218  void setUseDefs(bool usedefs) { setB("UseDefs", usedefs); }
219 
226  void show(std::ostream &os, bool showComments = true) const;
227 
233  std::string show(bool showComments = true) const;
234 
247  bool test(std::string const &attrib) const {
248  bool res = astTest(getRawPtr(), attrib.c_str());
249  assertOK();
250  return res;
251  }
252 
277  void unlock(bool report = false) {
278  astUnlock(getRawPtr(), static_cast<int>(report));
279  assertOK();
280  }
281 
289  AstObject const *getRawPtr() const { return &*_objPtr; };
290 
291  AstObject *getRawPtr() { return &*_objPtr; };
293 
294 protected:
298  explicit Object(AstObject *object) : _objPtr(object, &detail::annulAstObject) {
299  assertOK();
300  if (!object) {
301  throw std::runtime_error("Null pointer");
302  }
303  }
304 
311  template <typename ShimT, typename AstT>
312  static std::shared_ptr<ShimT> makeShim(AstObject *p) {
313  return std::shared_ptr<ShimT>(new ShimT(reinterpret_cast<AstT *>(p)));
314  }
315 
321  template <typename T, typename AstT>
322  std::shared_ptr<T> copyImpl() const {
323  auto *rawptr = reinterpret_cast<AstT *>(astCopy(getRawPtr()));
324  auto retptr = std::shared_ptr<T>(new T(rawptr));
325  assertOK();
326  return retptr;
327  }
328 
341  virtual std::shared_ptr<Object> copyPolymorphic() const = 0;
342 
350  bool getB(std::string const &attrib) const {
351  bool val = astGetI(getRawPtr(), attrib.c_str());
352  assertOK();
353  return val;
354  }
355 
363  std::string const getC(std::string const &attrib) const {
364  char const *rawval = astGetC(getRawPtr(), attrib.c_str());
365  assertOK();
366  return std::string(rawval);
367  }
368 
376  double getD(std::string const &attrib) const {
377  double val = astGetD(getRawPtr(), attrib.c_str());
378  assertOK();
379  return val;
380  }
381 
389  float getF(std::string const &attrib) const {
390  float val = astGetF(getRawPtr(), attrib.c_str());
391  assertOK();
392  return val;
393  }
394 
402  int getI(std::string const &attrib) const {
403  int val = astGetI(getRawPtr(), attrib.c_str());
404  assertOK();
405  return val;
406  }
407 
415  long int getL(std::string const &attrib) const {
416  long int val = astGetL(getRawPtr(), attrib.c_str());
417  assertOK();
418  return val;
419  }
420 
442  void set(std::string const &setting) { astSet(getRawPtr(), "%s", setting.c_str()); }
443 
451  void setB(std::string const &attrib, bool value) {
452  astSetI(getRawPtr(), attrib.c_str(), value);
453  assertOK();
454  }
455 
463  void setC(std::string const &attrib, std::string const &value) {
464  astSetC(getRawPtr(), attrib.c_str(), value.c_str());
465  assertOK();
466  }
467 
475  void setD(std::string const &attrib, double value) {
476  astSetD(getRawPtr(), attrib.c_str(), value);
477  assertOK();
478  }
479 
487  void setF(std::string const &attrib, float value) {
488  astSetF(getRawPtr(), attrib.c_str(), value);
489  assertOK();
490  }
491 
499  void setI(std::string const &attrib, int value) {
500  astSetI(getRawPtr(), attrib.c_str(), value);
501  assertOK();
502  }
503 
511  void setL(std::string const &attrib, long int value) {
512  astSetL(getRawPtr(), attrib.c_str(), value);
513  assertOK();
514  }
515 private:
516  /*
517  Given a bare AST object pointer return a shared pointer to an ast::Object of the correct type
518 
519  The returned object takes ownership of the pointer. This is almost always what you want,
520  for instance astDecompose returns shallow copies of the internal pointers.
521 
522  @param[in] rawObj A bare AST object pointer
523  */
524  static std::shared_ptr<Object> _basicFromAstObject(AstObject *rawObj);
525 
526  /*
527  Swap the raw object pointers between this and another object
528  */
529  void swapRawPointers(Object &other) noexcept {
530  swap(_objPtr, other._objPtr);
531  }
532 
533  ObjectPtr _objPtr;
534 };
535 
536 } // namespace ast
537 
538 #endif
bool test(std::string const &attrib) const
Definition: Object.h:247
AstObject const * getRawPtr() const
Definition: Object.h:289
long int getL(std::string const &attrib) const
Definition: Object.h:415
void setB(std::string const &attrib, bool value)
Definition: Object.h:451
int getI(std::string const &attrib) const
Definition: Object.h:402
AST wrapper classes and functions.
Definition: attributes_channel.dox:1
std::string getClassName() const
Definition: Object.h:136
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:218
std::shared_ptr< T > copyImpl() const
Definition: Object.h:322
std::shared_ptr< Object > copy() const
Return a deep copy of this object.
Definition: Object.h:106
void setL(std::string const &attrib, long int value)
Definition: Object.h:511
std::unique_ptr< AstObject, Deleter > ObjectPtr
unique pointer holding an AST raw pointer
Definition: Object.h:59
bool hasAttribute(std::string const &attrib) const
Definition: Object.h:124
static std::shared_ptr< Class > fromAstObject(AstObject *rawObj, bool copy)
Definition: Object.cc:131
std::string getID() const
Get ID: object identification string that is not copied.
Definition: Object.h:139
int getObjSize() const
Get ObjSize: the in-memory size of the AST object in bytes.
Definition: Object.h:153
bool getB(std::string const &attrib) const
Definition: Object.h:350
void unlock(bool report=false)
Definition: Object.h:277
Definition: MapSplit.h:38
bool getUseDefs() const
Get UseDefs: allow use of default values for Object attributes?
Definition: Object.h:163
void setC(std::string const &attrib, std::string const &value)
Definition: Object.h:463
static std::shared_ptr< ShimT > makeShim(AstObject *p)
Definition: Object.h:312
void setID(std::string const &id)
Set ID: object identification string that is not copied.
Definition: Object.h:212
std::string getIdent() const
Get Ident: object identification string that is copied.
Definition: Object.h:142
void setF(std::string const &attrib, float value)
Definition: Object.h:487
bool same(Object const &other) const
Definition: Object.h:209
int getRefCount() const
Definition: Object.h:160
void setIdent(std::string const &ident)
Set Ident: object identification string that is copied.
Definition: Object.h:215
void setD(std::string const &attrib, double value)
Definition: Object.h:475
int getNObject() const
Definition: Object.h:150
double getD(std::string const &attrib) const
Definition: Object.h:376
static std::shared_ptr< Object > fromString(std::string const &str)
Definition: Object.h:86
void show(std::ostream &os, bool showComments=true) const
Definition: Object.cc:151
float getF(std::string const &attrib) const
Definition: Object.h:389
void setI(std::string const &attrib, int value)
Definition: Object.h:499
Object(AstObject *object)
Definition: Object.h:298
std::string const getC(std::string const &attrib) const
Definition: Object.h:363
Definition: FrameDict.h:65
Definition: Object.h:51
bool operator==(Object const &rhs) const
Definition: Object.cc:81
bool operator!=(Object const &rhs) const
Definition: Object.h:81
void clear(std::string const &attrib)
Definition: Object.h:116
void lock(bool wait)
Definition: Object.h:199