lsst.astshim  14.0-8-g6380fcc
FrameDict.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_FRAMEDICT_H
23 #define ASTSHIM_FRAMEDICT_H
24 
25 #include <cassert>
26 #include <memory>
27 #include <set>
28 #include <sstream>
29 #include <stdexcept>
30 #include <unordered_map>
31 
32 #include "astshim/base.h"
33 #include "astshim/detail/utils.h"
34 #include "astshim/Frame.h"
35 #include "astshim/FrameSet.h"
36 
37 namespace ast {
38 
65 class FrameDict : public FrameSet {
66  friend class Object;
67 
68 public:
79  explicit FrameDict(Frame const &frame, std::string const &options = "") : FrameSet(frame, options) {
80  _domainIndexDict = _makeNewDict(*this);
81  }
82 
95  explicit FrameDict(Frame const &baseFrame, Mapping const &mapping, Frame const &currentFrame,
96  std::string const &options = "")
97  : FrameSet(baseFrame, mapping, currentFrame) {
98  _domainIndexDict = _makeNewDict(*this);
99  }
100 
108  explicit FrameDict(FrameSet const &frameSet)
109  : FrameDict(reinterpret_cast<AstFrameSet *>(frameSet.copy()->getRawPtr())) {}
110  virtual ~FrameDict() {}
111 
112  FrameDict(FrameDict const &) = delete;
113  FrameDict(FrameDict &&) = default;
114  FrameDict &operator=(FrameDict const &) = delete;
115  FrameDict &operator=(FrameDict &&) = default;
116 
118  std::shared_ptr<FrameDict> copy() const { return std::static_pointer_cast<FrameDict>(copyPolymorphic()); }
119 
126  void addFrame(int iframe, Mapping const &map, Frame const &frame) override;
127 
132  void addFrame(std::string const &domain, Mapping const &map, Frame const &frame);
133 
137  std::set<std::string> getAllDomains() const;
138 
139  using FrameSet::getFrame;
140 
146  std::shared_ptr<Frame> getFrame(std::string const &domain, bool copy = true) const {
147  return getFrame(getIndex(domain), copy);
148  }
149 
150  using FrameSet::getMapping;
151 
157  std::shared_ptr<Mapping> getMapping(int from, std::string const &to) const {
158  return getMapping(from, getIndex(to));
159  }
160 
166  std::shared_ptr<Mapping> getMapping(std::string const &from, int to) const {
167  return getMapping(getIndex(from), to);
168  }
169 
175  std::shared_ptr<Mapping> getMapping(std::string const &from, std::string const &to) const {
176  return getMapping(getIndex(from), getIndex(to));
177  }
178 
184  int getIndex(std::string const &domain) const {
185  auto domainUpper = detail::stringToUpper(domain);
186  auto it = _domainIndexDict.find(domainUpper);
187  if (it == _domainIndexDict.end()) {
188  throw std::out_of_range("No frame found with domain " + domain);
189  }
190  return it->second;
191  }
192 
196  bool hasDomain(std::string const &domain) const { return _domainIndexDict.count(domain) > 0; }
197 
199 
205  void mirrorVariants(std::string const &domain) { mirrorVariants(getIndex(domain)); }
206 
207  using FrameSet::remapFrame;
208 
214  void remapFrame(std::string const &domain, Mapping &map) { remapFrame(getIndex(domain), map); }
215 
217  void removeFrame(int iframe) override;
218 
224  void removeFrame(std::string const &domain);
225 
226  using FrameSet::setBase;
227 
233  void setBase(std::string const &domain) { setBase(getIndex(domain)); }
234 
235  using FrameSet::setCurrent;
236 
242  void setCurrent(std::string const &domain) { setCurrent(getIndex(domain)); }
243 
249  void setDomain(std::string const &domain) override;
250 
251 protected:
252  virtual std::shared_ptr<Object> copyPolymorphic() const override {
253  return copyImpl<FrameDict, AstFrameSet>();
254  }
255 
256  /*
257  Return a copy as a FrameSet
258 
259  This is used internally for operations that modify the contents:
260  - Retrieve a copy as a FrameSet
261  - Modify the copy
262  - Call _update to construct a new domain:index dict from the copy and swap in the new data
263 
264  This could be a public member function, but wait until a need is identified.
265 
266  Warning: this must return a FrameSet, not a FrameDict in order to avoid an infinite loop.
267  */
268  std::shared_ptr<FrameSet> getFrameSet() const {
269  return std::static_pointer_cast<FrameSet>(copyImpl<FrameSet, AstFrameSet>());
270  }
271 
273  explicit FrameDict(AstFrameSet *rawptr);
274 
275 private:
276  /*
277  Build a new domain:index dict from a FrameSet
278  */
279  static std::unordered_map<std::string, int> _makeNewDict(FrameSet const &frameSet);
280 
281  /*
282  Update the internal data using the supplied frameSet, which is swapped with the old data.
283 
284  Build a new domain::index dict from frameSet. If successful, replace the current dict
285  and swap frameSet with the old frame set data.
286 
287  If any exception is thrown this FrameDict (and the frameSet argument) is unchanged.
288  */
289  void _update(FrameSet &frameSet) {
290  _domainIndexDict = _makeNewDict(frameSet);
291  this->swapRawPointers(frameSet);
292  }
293 
294  std::unordered_map<std::string, int> _domainIndexDict; // Dict of frame domain:index
295 };
296 
297 } // namespace ast
298 
299 #endif
AstObject const * getRawPtr() const
Definition: Object.h:289
int getIndex(std::string const &domain) const
Definition: FrameDict.h:184
virtual std::shared_ptr< Object > copyPolymorphic() const override
Definition: FrameDict.h:252
AST wrapper classes and functions.
Definition: attributes_channel.dox:1
std::shared_ptr< Mapping > getMapping(std::string const &from, int to) const
Definition: FrameDict.h:166
std::shared_ptr< Frame > getFrame(int iframe, bool copy=true) const
Definition: FrameSet.h:268
FrameDict(FrameSet const &frameSet)
Definition: FrameDict.h:108
void setBase(int ind)
Definition: FrameSet.h:469
void mirrorVariants(int iframe)
Definition: FrameSet.h:365
Definition: Mapping.h:59
std::set< std::string > getAllDomains() const
Definition: FrameDict.cc:45
void setBase(std::string const &domain)
Definition: FrameDict.h:233
void setCurrent(std::string const &domain)
Definition: FrameDict.h:242
FrameDict(Frame const &baseFrame, Mapping const &mapping, Frame const &currentFrame, std::string const &options="")
Definition: FrameDict.h:95
void remapFrame(std::string const &domain, Mapping &map)
Definition: FrameDict.h:214
void addFrame(int iframe, Mapping const &map, Frame const &frame) override
Definition: FrameDict.cc:32
void remapFrame(int iframe, Mapping &map)
Definition: FrameSet.h:408
Definition: Frame.h:157
std::shared_ptr< Frame > getFrame(std::string const &domain, bool copy=true) const
Definition: FrameDict.h:146
std::shared_ptr< FrameDict > copy() const
Return a deep copy of this object.
Definition: FrameDict.h:118
std::shared_ptr< Mapping > getMapping(int from, std::string const &to) const
Definition: FrameDict.h:157
void setDomain(std::string const &domain) override
Definition: FrameDict.cc:61
void mirrorVariants(std::string const &domain)
Definition: FrameDict.h:205
void setCurrent(int ind)
Definition: FrameSet.h:474
std::shared_ptr< Mapping > getMapping(std::string const &from, std::string const &to) const
Definition: FrameDict.h:175
Definition: FrameDict.h:65
void removeFrame(int iframe) override
Definition: FrameDict.cc:53
Definition: Object.h:51
Definition: FrameSet.h:99
std::shared_ptr< Mapping > getMapping(int from=BASE, int to=CURRENT) const
Definition: FrameSet.h:302
bool hasDomain(std::string const &domain) const
Definition: FrameDict.h:196
FrameDict(Frame const &frame, std::string const &options="")
Definition: FrameDict.h:79