LSST Applications g0f08755f38+51082c0d4d,g1653933729+a905cd61c3,g168dd56ebc+a905cd61c3,g1a2382251a+29afb38aec,g20f6ffc8e0+51082c0d4d,g217e2c1bcf+ab9ba0d5ca,g28da252d5a+81b6c2f226,g2bbee38e9b+cc7bbd92cc,g2bc492864f+cc7bbd92cc,g32e5bea42b+4321971e9a,g347aa1857d+cc7bbd92cc,g35bb328faa+a905cd61c3,g3a166c0a6a+cc7bbd92cc,g3bd4b5ce2c+e795d60641,g3e281a1b8c+2bff41ced5,g414038480c+4de324692b,g41af890bb2+f80cf72528,g43bc871e57+a73212ffc0,g78460c75b0+4ae99bb757,g80478fca09+dbf4c199e3,g82479be7b0+84a80b86d5,g8365541083+a905cd61c3,g858d7b2824+51082c0d4d,g9125e01d80+a905cd61c3,ga5288a1d22+379478ca77,gb58c049af0+84d1b6ec45,gc28159a63d+cc7bbd92cc,gc5452a3dca+b82ec7cc4c,gcab2d0539d+4a1e53d2eb,gcf0d15dbbd+a702646d8b,gda6a2b7d83+a702646d8b,gdaeeff99f8+686ef0dd99,ge79ae78c31+cc7bbd92cc,gef2f8181fd+c1889b0e42,gf0baf85859+f9edac6842,gf1e97e5484+bcd3814849,gfa517265be+51082c0d4d,gfa999e8aa5+d85414070d,w.2025.01
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)