comparison foosdk/sdk/foobar2000/SDK/service_by_guid.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2 #include <pfc/map.h>
3
4
5
6 template<typename what>
7 static bool service_by_guid_fallback(service_ptr_t<what> & out, const GUID & id) {
8 for (auto ptr : what::enumerate()) {
9 if (ptr->get_guid() == id) { out = ptr; return true; }
10 }
11 return false;
12 }
13
14 template<typename what>
15 class service_by_guid_data {
16 public:
17 service_by_guid_data() : m_inited(), m_servClass() {}
18
19 bool ready() const {return m_inited;}
20
21 // Caller must ensure initialize call before create() as well as thread safety of initialize() calls. The rest of this class is thread safe (only reads member data).
22 void initialize() {
23 if (m_inited) return;
24 pfc::assert_same_type< what, typename what::t_interface_entrypoint >();
25 m_servClass = service_factory_base::enum_find_class(what::class_guid);
26 const t_size servCount = service_factory_base::enum_get_count(m_servClass);
27 for(t_size walk = 0; walk < servCount; ++walk) {
28 service_ptr_t<what> temp;
29 if (_service_instantiate_helper(temp, m_servClass, walk)) {
30 m_order.set(temp->get_guid(), walk);
31 }
32 }
33 m_inited = true;
34 }
35
36 bool create(service_ptr_t<what> & out, const GUID & theID) const {
37 PFC_ASSERT(m_inited);
38 t_size index;
39 if (!m_order.query(theID,index)) return false;
40 return _service_instantiate_helper(out, m_servClass, index);
41 }
42 service_ptr_t<what> create(const GUID & theID) const {
43 service_ptr_t<what> temp; if (!crete(temp,theID)) throw exception_service_not_found(); return temp;
44 }
45
46 private:
47 volatile bool m_inited;
48 pfc::map_t<GUID,t_size> m_order;
49 service_class_ref m_servClass;
50 };
51
52 template<typename what>
53 class _service_by_guid_data_container {
54 public:
55 static service_by_guid_data<what> data;
56 };
57 template<typename what> service_by_guid_data<what> _service_by_guid_data_container<what>::data;
58
59
60 template<typename what>
61 static void service_by_guid_init() {
62 service_by_guid_data<what> & data = _service_by_guid_data_container<what>::data;
63 data.initialize();
64 }
65 template<typename what>
66 static bool service_by_guid(service_ptr_t<what> & out, const GUID & theID) {
67 pfc::assert_same_type< what, typename what::t_interface_entrypoint >();
68 service_by_guid_data<what> & data = _service_by_guid_data_container<what>::data;
69 if (data.ready()) {
70 //fall-thru
71 } else if (core_api::is_main_thread()) {
72 data.initialize();
73 } else {
74 #if PFC_DEBUG
75 FB2K_DebugLog() << "Warning: service_by_guid() used in non-main thread without initialization, using fallback";
76 #endif
77 return service_by_guid_fallback(out,theID);
78 }
79 return data.create(out,theID);
80 }
81 template<typename what>
82 static service_ptr_t<what> service_by_guid(const GUID & theID) {
83 service_ptr_t<what> temp;
84 if (!service_by_guid(temp, theID)) {
85 #if PFC_DEBUG
86 FB2K_DebugLog() << "service_by_guid failure: " << what::debugServiceName() << " : " << pfc::print_guid( theID );
87 #endif
88 throw exception_service_not_found();
89 }
90 return temp;
91 }
92
93
94
95
96 class comparator_service_guid {
97 public:
98 template<typename what> static int compare(const what & v1, const what & v2) { return pfc::compare_t(v1->get_guid(), v2->get_guid()); }
99 };
100