lsst.astshim  15.0
FrameSet.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_FRAMESET_H
23 #define ASTSHIM_FRAMESET_H
24 
25 #include <memory>
26 #include <sstream>
27 #include <stdexcept>
28 
29 #include "astshim/base.h"
30 #include "astshim/Frame.h"
31 
32 namespace ast {
33 
99 class FrameSet : public Frame {
100  friend class Frame; // so Frame can call the protected raw ptr constructor
101  friend class Object;
102 
103 public:
104  static int constexpr BASE = AST__BASE;
105  static int constexpr CURRENT = AST__CURRENT;
106  static int constexpr NOFRAME = AST__NOFRAME;
107 
117  explicit FrameSet(Frame const &frame, std::string const &options = "")
118  : FrameSet(astFrameSet(frame.copy()->getRawPtr(), "%s", options.c_str())) {}
119 
130  explicit FrameSet(Frame const &baseFrame, Mapping const &mapping, Frame const &currentFrame,
131  std::string const &options = "")
132  : FrameSet(baseFrame, options) {
133  _basicAddFrame(1, mapping, currentFrame);
134  }
135 
136  virtual ~FrameSet() {}
137 
139  FrameSet(FrameSet const &) = default;
140  FrameSet(FrameSet &&) = default;
141  FrameSet &operator=(FrameSet const &) = delete;
142  FrameSet &operator=(FrameSet &&) = default;
143 
145  std::shared_ptr<FrameSet> copy() const { return std::static_pointer_cast<FrameSet>(copyPolymorphic()); }
146 
159  void addAxes(Frame const &frame) {
160  astAddFrame(getRawPtr(), AST__ALLFRAMES, nullptr, frame.getRawPtr());
161  assertOK();
162  }
163 
209  virtual void addFrame(int iframe, Mapping const &map, Frame const &frame) {
210  _basicAddFrame(iframe, map, frame);
211  }
212 
233  void addVariant(Mapping const &map, std::string const &name) {
234  astAddVariant(getRawPtr(), map.getRawPtr(), name.c_str());
235  assertOK();
236  }
237 
242  std::string getAllVariants() const { return getC("AllVariants"); }
243 
247  int getBase() const { return getI("Base"); }
248 
252  int getCurrent() const { return getI("Current"); }
253 
269  std::shared_ptr<Frame> getFrame(int iframe, bool copy = true) const {
270  auto *rawFrame = reinterpret_cast<AstObject *>(astGetFrame(getRawPtr(), iframe));
271  assertOK(rawFrame);
272  if (!rawFrame) {
273  throw std::runtime_error("getFrame failed (returned a null frame)");
274  }
275  return Object::fromAstObject<Frame>(rawFrame, copy);
276  }
277 
303  std::shared_ptr<Mapping> getMapping(int from = BASE, int to = CURRENT) const {
304  AstObject *rawMap = reinterpret_cast<AstObject *>(astGetMapping(getRawPtr(), from, to));
305  assertOK(rawMap);
306  if (!rawMap) {
307  throw std::runtime_error("getMapping failed (returned a null mapping)");
308  }
309  return Object::fromAstObject<Mapping>(rawMap, true);
310  }
311 
315  int getNFrame() const { return getI("NFrame"); }
316 
322  std::string getVariant() const { return getC("Variant"); }
323 
366  void mirrorVariants(int iframe) {
367  astMirrorVariants(getRawPtr(), iframe);
368  assertOK();
369  }
370 
409  void remapFrame(int iframe, Mapping &map) {
410  astRemapFrame(getRawPtr(), iframe, map.copy()->getRawPtr());
411  assertOK();
412  };
413 
441  virtual void removeFrame(int iframe) {
442  astRemoveFrame(getRawPtr(), iframe);
443  assertOK();
444  }
445 
462  void renameVariant(std::string const &name) {
463  astAddVariant(getRawPtr(), nullptr, name.c_str());
464  assertOK();
465  }
466 
470  void setBase(int ind) { setI("Base", ind); }
471 
475  void setCurrent(int ind) { setI("Current", ind); }
476 
477 protected:
478  virtual std::shared_ptr<Object> copyPolymorphic() const override {
479  return copyImpl<FrameSet, AstFrameSet>();
480  }
481 
489  explicit FrameSet(AstFrameSet *rawPtr) : Frame(reinterpret_cast<AstFrame *>(rawPtr)) {
490  if (!astIsAFrameSet(getRawPtr())) {
491  std::ostringstream os;
492  os << "this is a " << getClassName() << ", which is not a FrameSet";
493  throw std::invalid_argument(os.str());
494  }
495  }
496 
497 private:
498  // non-virtual version of addFrame for use by constructors
499  void _basicAddFrame(int iframe, Mapping const &map, Frame const &frame) {
500  if (iframe == AST__ALLFRAMES) {
501  throw std::runtime_error("iframe = AST__ALLFRAMES; call addAxes instead");
502  }
503  // astAddFrame makes deep copies of the map and frame, so no need to do anything extra
504  astAddFrame(getRawPtr(), iframe, map.getRawPtr(), frame.getRawPtr());
505  assertOK();
506  }
507 
508 };
509 
510 } // namespace ast
511 
512 #endif
int getBase() const
Definition: FrameSet.h:247
virtual void removeFrame(int iframe)
Definition: FrameSet.h:441
AstObject const * getRawPtr() const
Definition: Object.h:291
int getNFrame() const
Definition: FrameSet.h:315
int getI(std::string const &attrib) const
Definition: Object.h:399
AST wrapper classes and functions.
Definition: attributes_channel.dox:1
int getCurrent() const
Definition: FrameSet.h:252
std::string getClassName() const
Definition: Object.h:138
std::shared_ptr< Frame > getFrame(int iframe, bool copy=true) const
Definition: FrameSet.h:269
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:49
virtual std::shared_ptr< Object > copyPolymorphic() const override
Definition: FrameSet.h:478
void setBase(int ind)
Definition: FrameSet.h:470
std::string getVariant() const
Definition: FrameSet.h:322
void mirrorVariants(int iframe)
Definition: FrameSet.h:366
static int constexpr CURRENT
index of current frame
Definition: FrameSet.h:105
void addVariant(Mapping const &map, std::string const &name)
Definition: FrameSet.h:233
Definition: Mapping.h:59
std::shared_ptr< FrameSet > copy() const
Return a deep copy of this object.
Definition: FrameSet.h:145
void remapFrame(int iframe, Mapping &map)
Definition: FrameSet.h:409
Definition: Frame.h:157
static int constexpr NOFRAME
an invalid frame index
Definition: FrameSet.h:106
std::shared_ptr< Mapping > copy() const
Return a deep copy of this object.
Definition: Mapping.h:72
void renameVariant(std::string const &name)
Definition: FrameSet.h:462
FrameSet(Frame const &baseFrame, Mapping const &mapping, Frame const &currentFrame, std::string const &options="")
Definition: FrameSet.h:130
void addAxes(Frame const &frame)
Definition: FrameSet.h:159
std::string getAllVariants() const
Definition: FrameSet.h:242
void setCurrent(int ind)
Definition: FrameSet.h:475
FrameSet(Frame const &frame, std::string const &options="")
Definition: FrameSet.h:117
static int constexpr BASE
index of base frame
Definition: FrameSet.h:104
void setI(std::string const &attrib, int value)
Definition: Object.h:496
std::string const getC(std::string const &attrib) const
Definition: Object.h:360
virtual void addFrame(int iframe, Mapping const &map, Frame const &frame)
Definition: FrameSet.h:209
Definition: Object.h:51
FrameSet(AstFrameSet *rawPtr)
Definition: FrameSet.h:489
Definition: FrameSet.h:99
std::shared_ptr< Mapping > getMapping(int from=BASE, int to=CURRENT) const
Definition: FrameSet.h:303