LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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)