LSST Applications g1653933729+b86e4b8053,g2303c004a3+7a9f81fab8,g28da252d5a+fa526343b8,g2bbee38e9b+2d3ef731f8,g2bc492864f+2d3ef731f8,g2cdde0e794+ed7a12f8c9,g3156d2b45e+b0ccf75d80,g347aa1857d+2d3ef731f8,g35bb328faa+b86e4b8053,g3a166c0a6a+2d3ef731f8,g3e281a1b8c+ffc3dc1f6e,g414038480c+28b4cb0ab6,g42360bd172+7a9f81fab8,g461a3dce89+b86e4b8053,g5c9cb89fa5+9543a9a537,g7f102674c4+ae0b5e0d9c,g7f343241b9+f1af73f4dd,g80478fca09+43612671ef,g82479be7b0+b53791e409,g858d7b2824+7a9f81fab8,g8cd86fa7b1+e690011a40,g9125e01d80+b86e4b8053,g979bb04a14+f1d038bb8c,g9ddcbc5298+b4cb6f9276,ga44b09e51c+892bfc8707,ga5288a1d22+c219b1b90b,gae0086650b+b86e4b8053,gc28159a63d+2d3ef731f8,gcd45df26be+7a9f81fab8,gcf0d15dbbd+fb9f8fc3a2,gda6a2b7d83+fb9f8fc3a2,gdaeeff99f8+b9455e0a82,ge5cf12406b+5667261091,ge79ae78c31+2d3ef731f8,ge7c99ddb59+5fbad68458,geb408df1e3+2fe46b4317,gf048a9a2f4+8beaee1f08,gf0baf85859+122dc4ce2f,w.2024.32
LSST Data Management Base Package
Loading...
Searching...
No Matches
channel.cc
Go to the documentation of this file.
1#include <functional>
2#include <iostream>
3#include <string>
4
6
8
9namespace lsst::gauss2d::fit {
10
11/*
12The registry serves several purposes:
13 - ensures that Channels are unique and do not share names
14 - allows Channels to be easily found by name.
15 - prevents Channels from being implicitly deleted when no objects reference them
16 (though they can be explicitly deleted in that case)
17*/
18static inline Channel::Registry _registry = {};
19
20// https://stackoverflow.com/questions/8147027/
21// how-do-i-call-stdmake-shared-on-a-class-with-only-protected-or-private-const/
22// 8147213#comment58654091_25069711
24 template <typename... Args>
25 Shared_enabler(Args &&...args) : Channel(std::forward<Args>(args)...) {}
26};
27
28Channel::Channel(std::string name_) : name(name_) {
29 static bool none_made = false;
30 if (name_ == NAME_NONE) {
31 if (none_made) {
32 throw std::invalid_argument("Channel('None') always exists and cannot be re-made");
33 } else {
34 none_made = true;
35 }
36 } else {
37 auto found = _registry.find(name);
38 if (found != _registry.end()) {
39 throw std::invalid_argument("Channel(name=" + name + ") already exists as "
40 + found->second->str());
41 }
42 }
43}
44
45const bool Channel::operator<(const Channel &c) const { return name < c.name; }
46
47const bool Channel::operator==(const Channel &c) const { return name == c.name; }
48
49const bool Channel::operator!=(const Channel &c) const { return !(*this == c); }
50
52 if (name == NAME_NONE) throw std::invalid_argument("Can't erase the " + NAME_NONE + " Channel");
54 if (channel == nullptr) throw std::invalid_argument("Can't erase non-existent Channel name=" + name);
55 size_t use_count = channel.use_count();
56 if (use_count < 2) {
57 throw std::logic_error("Failed erasing " + channel->str()
58 + " with unexpected use_count=" + std::to_string(use_count));
59 } else if (use_count == 2) {
60 _registry.erase(name);
61 } else {
62 throw std::runtime_error("Can't erase " + channel->str()
63 + " with use_count=" + std::to_string(use_count));
64 }
65}
66
68 if (name == NAME_NONE) return NONE_PTR();
69 auto channel = _registry.find(name);
70 auto found = channel == _registry.end() ? nullptr : channel->second;
71 return found;
72}
73
75 auto channel = Channel::find_channel(name);
76 if (channel == nullptr) channel = Channel::make(name);
77 return channel;
78}
79
82 for (const auto &[key, value] : _registry) {
83 vec.emplace_back(value);
84 }
86 return vec;
87}
88
90 std::shared_ptr<Channel> c = std::make_shared<Shared_enabler>(name);
91 if (name != NAME_NONE) {
92 _registry.insert({name, c});
93 }
94 return c;
95}
96
98
99std::string Channel::repr(bool name_keywords, std::string_view namespace_separator) const {
100 return type_name_str<Channel>(false, namespace_separator) + "(" + (name_keywords ? "name=" : "") + name
101 + ")";
102}
103
104std::string Channel::str() const { return type_name_str<Channel>(true) + "(name=" + name + ")"; }
105
110
111const Channel &Channel::NONE() { return *NONE_PTR(); }
112
113} // namespace lsst::gauss2d::fit
An observational channel, usually representing some range of wavelengths of light.
Definition channel.h:29
static void erase(std::string name)
Delete a channel with a given name.
Definition channel.cc:51
static const std::shared_ptr< const Channel > make_const(std::string name)
Same as make(), but creating a new Channel.
Definition channel.cc:97
Channel(const Channel &)=delete
const bool operator<(const Channel &c) const
Definition channel.cc:45
std::map< std::string, std::shared_ptr< const Channel > > Registry
Definition channel.h:31
static const Channel & NONE()
Definition channel.cc:111
static const std::string NAME_NONE
Definition channel.h:64
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition channel.cc:99
static const std::shared_ptr< const Channel > get_channel(std::string name)
Get a channel with the given name, creating it if it doesn't exist.
Definition channel.cc:74
static std::vector< std::shared_ptr< const Channel > > get_channels()
Definition channel.cc:80
const bool operator==(const Channel &c) const
Definition channel.cc:47
static const std::shared_ptr< const Channel > NONE_PTR()
Definition channel.cc:106
static const std::shared_ptr< const Channel > find_channel(std::string name)
Find a channel with the given name.
Definition channel.cc:67
const std::string name
Definition channel.h:66
static std::shared_ptr< Channel > make(std::string name)
Construct a new Channel.
Definition channel.cc:89
std::string str() const override
Return a brief, human-readable string representation of this.
Definition channel.cc:104
const bool operator!=(const Channel &c) const
Definition channel.cc:49
T emplace_back(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
T insert(T... args)
STL namespace.
T to_string(T... args)