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
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)