lsst.astshim  14.0-9-g580965d
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) : FrameSet(std::move(*frameSet.copy())), _domainIndexDict() {
109  _domainIndexDict = _makeNewDict(*this);
110  }
111 
112  virtual ~FrameDict() {}
113 
114  FrameDict(FrameDict const &) = delete;
115  FrameDict(FrameDict &&) = default;
116  FrameDict &operator=(FrameDict const &) = delete;
117  FrameDict &operator=(FrameDict &&) = default;
118 
120  std::shared_ptr<FrameDict> copy() const { return std::static_pointer_cast<FrameDict>(copyPolymorphic()); }
121 
128  void addFrame(int iframe, Mapping const &map, Frame const &frame) override;
129 
134  void addFrame(std::string const &domain, Mapping const &map, Frame const &frame);
135 
139  std::set<std::string> getAllDomains() const;
140 
141  using FrameSet::getFrame;
142 
148  std::shared_ptr<Frame> getFrame(std::string const &domain, bool copy = true) const {
149  return getFrame(getIndex(domain), copy);
150  }
151 
152  using FrameSet::getMapping;
153 
159  std::shared_ptr<Mapping> getMapping(int from, std::string const &to) const {
160  return getMapping(from, getIndex(to));
161  }
162 
168  std::shared_ptr<Mapping> getMapping(std::string const &from, int to) const {
169  return getMapping(getIndex(from), to);
170  }
171 
177  std::shared_ptr<Mapping> getMapping(std::string const &from, std::string const &to) const {
178  return getMapping(getIndex(from), getIndex(to));
179  }
180 
186  int getIndex(std::string const &domain) const {
187  auto domainUpper = detail::stringToUpper(domain);
188  auto it = _domainIndexDict.find(domainUpper);
189  if (it == _domainIndexDict.end()) {
190  throw std::out_of_range("No frame found with domain " + domain);
191  }
192  return it->second;
193  }
194 
198  bool hasDomain(std::string const &domain) const { return _domainIndexDict.count(domain) > 0; }
199 
201 
207  void mirrorVariants(std::string const &domain) { mirrorVariants(getIndex(domain)); }
208 
209  using FrameSet::remapFrame;
210 
216  void remapFrame(std::string const &domain, Mapping &map) { remapFrame(getIndex(domain), map); }
217 
219  void removeFrame(int iframe) override;
220 
226  void removeFrame(std::string const &domain);
227 
228  using FrameSet::setBase;
229 
235  void setBase(std::string const &domain) { setBase(getIndex(domain)); }
236 
237  using FrameSet::setCurrent;
238 
244  void setCurrent(std::string const &domain) { setCurrent(getIndex(domain)); }
245 
251  void setDomain(std::string const &domain) override;
252 
253 protected:
254  virtual std::shared_ptr<Object> copyPolymorphic() const override {
255  return copyImpl<FrameDict, AstFrameSet>();
256  }
257 
258  /*
259  Return a copy as a FrameSet
260 
261  This is used internally for operations that modify the contents:
262  - Retrieve a copy as a FrameSet
263  - Modify the copy
264  - Call _update to construct a new domain:index dict from the copy and swap in the new data
265 
266  This could be a public member function, but wait until a need is identified.
267 
268  Warning: this must return a FrameSet, not a FrameDict in order to avoid an infinite loop.
269  */
270  std::shared_ptr<FrameSet> getFrameSet() const {
271  return std::static_pointer_cast<FrameSet>(copyImpl<FrameSet, AstFrameSet>());
272  }
273 
275  explicit FrameDict(AstFrameSet *rawptr);
276 
277 private:
278  /*
279  Build a new domain:index dict from a FrameSet
280  */
281  static std::unordered_map<std::string, int> _makeNewDict(FrameSet const &frameSet);
282 
283  /*
284  Update the internal data using the supplied frameSet, which is swapped with the old data.
285 
286  Build a new domain::index dict from frameSet. If successful, replace the current dict
287  and swap frameSet with the old frame set data.
288 
289  If any exception is thrown this FrameDict (and the frameSet argument) is unchanged.
290  */
291  void _update(FrameSet &frameSet) {
292  _domainIndexDict = _makeNewDict(frameSet);
293  this->swapRawPointers(frameSet);
294  }
295 
296  std::unordered_map<std::string, int> _domainIndexDict; // Dict of frame domain:index
297 };
298 
299 } // namespace ast
300 
301 #endif
int getIndex(std::string const &domain) const
Definition: FrameDict.h:186
virtual std::shared_ptr< Object > copyPolymorphic() const override
Definition: FrameDict.h:254
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:168
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:235
void setCurrent(std::string const &domain)
Definition: FrameDict.h:244
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:216
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:148
std::shared_ptr< FrameDict > copy() const
Return a deep copy of this object.
Definition: FrameDict.h:120
std::shared_ptr< Mapping > getMapping(int from, std::string const &to) const
Definition: FrameDict.h:159
void setDomain(std::string const &domain) override
Definition: FrameDict.cc:61
void mirrorVariants(std::string const &domain)
Definition: FrameDict.h:207
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:177
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:198
FrameDict(Frame const &frame, std::string const &options="")
Definition: FrameDict.h:79