LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
base.cc
Go to the documentation of this file.
1#include "astshim/base.h"
2
3#include <sstream>
4#include <stdexcept>
5#include <string>
6#include <vector>
7
8namespace ast {
9namespace {
10
11static std::ostringstream errorMsgStream;
12
13/*
14Write an error message to `errorMsgStream`
15
16Intended to be registered as an error handler to AST by calling `astSetPutErr(reportError)`.
17*/
18void reportError(int errNum, const char *errMsg) { errorMsgStream << errMsg; }
19
20/*
21Instantiate this class to register `reportError` as an AST error handler.
22*/
23class ErrorHandler {
24public:
25 ErrorHandler() { astSetPutErr(reportError); }
26
27 ErrorHandler(ErrorHandler const &) = delete;
28 ErrorHandler(ErrorHandler &&) = delete;
29 ErrorHandler &operator=(ErrorHandler const &) = delete;
30 ErrorHandler &operator=(ErrorHandler &&) = delete;
31
32 static std::string getErrMsg() {
33 auto errMsg = errorMsgStream.str();
34 // clear status bits
35 errorMsgStream.clear();
36 if (errMsg.empty()) {
37 errMsg = "Failed with AST status = " + std::to_string(astStatus);
38 } else {
39 // empty the stream
40 errorMsgStream.str("");
41 }
42 astClearStatus;
43 return errMsg;
44 }
45};
46
47} // namespace
48
49void assertOK(AstObject *rawPtr1, AstObject *rawPtr2) {
50 // Construct ErrorHandler once, the first time this function is called.
51 // This is done to initialize `errorMsgStream` and register `reportError` as the AST error handler.
52 // See https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
53 static ErrorHandler *errHandler = new ErrorHandler();
54 if (!astOK) {
55 if (rawPtr1) {
56 astAnnul(rawPtr1);
57 }
58 if (rawPtr2) {
59 astAnnul(rawPtr2);
60 }
61 throw std::runtime_error(errHandler->getErrMsg());
62 }
63}
64
66 return static_cast<ConstArray2D>(arrayFromVector(const_cast<std::vector<double> &>(vec), nAxes));
67}
68
70 int nPoints = vec.size() / nAxes;
71 if (nPoints * nAxes != vec.size()) {
73 os << "vec length = " << vec.size() << " not a multiple of nAxes = " << nAxes;
74 throw std::runtime_error(os.str());
75 }
76 Array2D::Index shape = ndarray::makeVector(nAxes, nPoints);
77 Array2D::Index strides = ndarray::makeVector(nPoints, 1);
78 return external(vec.data(), shape, strides);
79}
80
81} // namespace ast
std::ostream * os
Definition Schema.cc:557
T clear(T... args)
T data(T... args)
AST wrapper classes and functions.
ndarray::Array< const double, 2, 2 > ConstArray2D
2D array of const double; typically used for lists of const points
Definition base.h:46
ndarray::Array< double, 2, 2 > Array2D
2D array of const double; typically used for lists of const points
Definition base.h:42
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Throw std::runtime_error if AST's state is bad.
Definition base.cc:49
ConstArray2D arrayFromVector(std::vector< double > const &vec, int nAxes)
Reshape a vector as a 2-dimensional array that shares the same memory.
Definition base.cc:65
T size(T... args)
T str(T... args)
T to_string(T... args)