LSST Applications g1653933729+34a971ddd9,g1a997c3884+34a971ddd9,g28da252d5a+32b12a4112,g2bbee38e9b+ffc559ee46,g2bc492864f+ffc559ee46,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+ffc559ee46,g35bb328faa+34a971ddd9,g3a166c0a6a+ffc559ee46,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9f5be647b3,g41af890bb2+686ab7cd58,g4e1a3235cc+1fd785b14f,g7b55021d1b+da0d0eec6b,g80478fca09+6e21f9dc20,g82479be7b0+6fa35d7547,g858d7b2824+356eaf3287,g9125e01d80+34a971ddd9,ga5288a1d22+ba1f9bb3ef,gae0086650b+34a971ddd9,gb4748cc0e8+fd26a265a0,gb4ec7eb0ab+356eaf3287,gb58c049af0+ace264a4f2,gc081298178+30f12d1e69,gc28159a63d+ffc559ee46,gcf0d15dbbd+4adb3139bd,gd6b7c0dfd1+aae9efebf5,gda3e153d99+356eaf3287,gda6a2b7d83+4adb3139bd,gdaeeff99f8+7774323b41,gdd5a9049c5+ea78b51778,ge2409df99d+fa478de0b0,ge33fd446bb+356eaf3287,ge79ae78c31+ffc559ee46,gf0baf85859+890af219f9,gf5289d68f6+e11c78aeea,w.2024.41
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)