LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
Object.cc
Go to the documentation of this file.
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 #include <algorithm>
23 #include <functional>
24 #include <ostream>
25 #include <sstream>
26 #include <stdexcept>
27 #include <string>
28 #include <unordered_map>
29 
30 #include "astshim/base.h"
31 #include "astshim/detail/utils.h"
32 #include "astshim/Object.h"
33 #include "astshim/Channel.h"
34 #include "astshim/ChebyMap.h"
35 #include "astshim/CmpFrame.h"
36 #include "astshim/Frame.h"
37 #include "astshim/FrameSet.h"
38 #include "astshim/FrameDict.h"
39 #include "astshim/KeyMap.h"
40 #include "astshim/LutMap.h"
41 #include "astshim/MathMap.h"
42 #include "astshim/MatrixMap.h"
43 #include "astshim/NormMap.h"
44 #include "astshim/ParallelMap.h"
45 #include "astshim/PcdMap.h"
46 #include "astshim/PermMap.h"
47 #include "astshim/PolyMap.h"
48 #include "astshim/RateMap.h"
49 #include "astshim/SeriesMap.h"
50 #include "astshim/ShiftMap.h"
51 #include "astshim/SkyFrame.h"
52 #include "astshim/SlaMap.h"
53 #include "astshim/SpecFrame.h"
54 #include "astshim/SphMap.h"
55 #include "astshim/TimeFrame.h"
56 #include "astshim/TimeMap.h"
57 #include "astshim/TranMap.h"
58 #include "astshim/UnitMap.h"
59 #include "astshim/UnitNormMap.h"
60 #include "astshim/WcsMap.h"
61 #include "astshim/WinMap.h"
62 #include "astshim/ZoomMap.h"
63 
64 namespace ast {
65 
66 namespace {
67 
75 extern "C" void sinkToOstream(const char *text) {
76  auto osptr = reinterpret_cast<std::ostream *>(astChannelData);
77  (*osptr) << text << std::endl;
78 }
79 
80 } // anonymous namespace
81 
82 bool Object::operator==(Object const &rhs) const {
83  auto thisStr = this->show(false);
84  auto rhsStr = rhs.show(false);
85  return rhsStr == thisStr;
86 }
87 
88 std::shared_ptr<Object> Object::_basicFromAstObject(AstObject *rawObj) {
90  ClassCasterMap = {
91  {"ChebyMap", makeShim<ChebyMap, AstChebyMap>},
92  {"CmpFrame", makeShim<CmpFrame, AstCmpFrame>},
93  {"Frame", makeShim<Frame, AstFrame>},
94  {"FrameSet", makeShim<FrameSet, AstFrameSet>},
95  {"FrameDict", makeShim<FrameDict, AstFrameSet>},
96  {"KeyMap", makeShim<KeyMap, AstKeyMap>},
97  {"LutMap", makeShim<LutMap, AstLutMap>},
98  {"MathMap", makeShim<MathMap, AstMathMap>},
99  {"MatrixMap", makeShim<MatrixMap, AstMatrixMap>},
100  {"NormMap", makeShim<NormMap, AstNormMap>},
101  {"ParallelMap", makeShim<ParallelMap, AstCmpMap>},
102  {"PcdMap", makeShim<PcdMap, AstPcdMap>},
103  {"PermMap", makeShim<PermMap, AstPermMap>},
104  {"PolyMap", makeShim<PolyMap, AstPolyMap>},
105  {"RateMap", makeShim<RateMap, AstRateMap>},
106  {"SeriesMap", makeShim<SeriesMap, AstCmpMap>},
107  {"ShiftMap", makeShim<ShiftMap, AstShiftMap>},
108  {"SkyFrame", makeShim<SkyFrame, AstSkyFrame>},
109  {"SlaMap", makeShim<SlaMap, AstSlaMap>},
110  {"SpecFrame", makeShim<SpecFrame, AstSpecFrame>},
111  {"SphMap", makeShim<SphMap, AstSphMap>},
112  {"TimeFrame", makeShim<TimeFrame, AstTimeFrame>},
113  {"TimeMap", makeShim<TimeMap, AstTimeMap>},
114  {"TranMap", makeShim<TranMap, AstTranMap>},
115  {"UnitMap", makeShim<UnitMap, AstUnitMap>},
116  {"UnitNormMap", makeShim<UnitNormMap, AstUnitNormMap>},
117  {"WcsMap", makeShim<WcsMap, AstWcsMap>},
118  {"WinMap", makeShim<WinMap, AstWinMap>},
119  {"ZoomMap", makeShim<ZoomMap, AstZoomMap>},
120  };
121  assertOK(rawObj);
122  auto className = detail::getClassName(rawObj);
123  auto name_caster = ClassCasterMap.find(className);
124  if (name_caster == ClassCasterMap.end()) {
125  astAnnul(rawObj);
126  throw std::runtime_error("Class " + className + " not supported");
127  }
128  return name_caster->second(rawObj);
129 }
130 
131 template <typename Class>
133  AstObject *rawObjCopy = rawObj;
134  if (copy) {
135  rawObjCopy = reinterpret_cast<AstObject *>(astCopy(rawObj));
136  astAnnul(rawObj);
137  }
138  assertOK(rawObjCopy);
139 
140  // Make the appropriate ast shim object and dynamically cast to the desired output type
141  auto retObjectBeforeCast = Object::_basicFromAstObject(rawObjCopy);
142  auto retObject = std::dynamic_pointer_cast<Class>(retObjectBeforeCast);
143  if (!retObject) {
145  os << "The component is of type " << retObject->getClassName()
146  << ", which could not be cast to the desired type " << typeid(Class).name();
147  throw std::runtime_error(os.str());
148  }
149  return retObject;
150 }
151 
152 void Object::show(std::ostream &os, bool showComments) const {
153  Stream stream(nullptr, &os);
154  Channel ch(stream, showComments ? "" : "Comment=0");
155  ch.write(*this);
156  assertOK();
157 }
158 
159 std::string Object::show(bool showComments) const {
161  show(os, showComments);
162  return os.str();
163 }
164 
165 Object::Object(AstObject *object) : _objPtr(object, &detail::annulAstObject) {
166  assertOK();
167  if (!object) {
168  throw std::runtime_error("Null pointer");
169  }
170 }
171 
172 // Explicit instantiations
173 template std::shared_ptr<FrameSet> Object::fromAstObject<FrameSet>(AstObject *, bool);
174 template std::shared_ptr<Frame> Object::fromAstObject<Frame>(AstObject *, bool);
175 template std::shared_ptr<Mapping> Object::fromAstObject<Mapping>(AstObject *, bool);
176 template std::shared_ptr<Object> Object::fromAstObject<Object>(AstObject *, bool);
177 
178 } // namespace ast
void annulAstObject(AstObject *object)
A wrapper around astAnnul; intended as a custom deleter for std::unique_ptr.
Definition: utils.h:40
AST wrapper classes and functions.
T endl(T... args)
Channel provides input/output of AST objects.
Definition: Channel.h:60
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Throw std::runtime_error if AST&#39;s state is bad.
Definition: base.cc:49
STL class.
std::shared_ptr< Object > copy() const
Return a deep copy of this object.
Definition: Object.h:109
static std::shared_ptr< Class > fromAstObject(AstObject *rawObj, bool copy)
Given a bare AST object pointer return a shared pointer to an ast::Object of the correct type...
Definition: Object.cc:132
Object(Object const &object)
Copy constructor: make a deep copy.
Definition: Object.h:64
int write(Object const &object)
Write an object to a channel.
Definition: Channel.cc:61
T str(T... args)
T dynamic_pointer_cast(T... args)
std::string getClassName(AstObject const *rawObj)
Get the AST class name, changing CmpMap to SeriesMap or ParallelMap as appropriate.
Definition: utils.cc:37
void show(std::ostream &os, bool showComments=true) const
Print a textual description the object to an ostream.
Definition: Object.cc:152
Abstract base class for all AST objects.
Definition: Object.h:51
STL class.
bool operator==(Object const &rhs) const
Return True if this and rhs are the equal.
Definition: Object.cc:82
A stream for ast::Channel.
Definition: Stream.h:41
std::ostream * os
Definition: Schema.cc:746