LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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)