Sygaldry
Loading...
Searching...
No Matches
describe.hpp
1#pragma once
2/*
3Copyright 2023 Travis J. West, https://traviswest.ca, Input Devices and Music Interaction Laboratory
4(IDMIL), Centre for Interdisciplinary Research in Music Media and Technology
5(CIRMMT), McGill University, Montréal, Canada, and Univ. Lille, Inria, CNRS,
6Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France
7
8SPDX-License-Identifier: MIT
9*/
10
11#include <boost/pfr.hpp>
12#include "sygah-consteval.hpp"
13#include "sygac-components.hpp"
14#include "sygac-metadata.hpp"
15#include "sygac-endpoints.hpp"
16#include "sygbp-osc_string_constants.hpp"
17#include "sygbp-osc_match_pattern.hpp"
18
19namespace sygaldry { namespace sygbp {
22
24{
25 static _consteval auto name() { return "/describe"; }
26 static _consteval auto usage() { return "osc-address-pattern"; }
27 static _consteval auto description() { return "Convey metadata about entities that match the given address pattern"; }
28
29 template<typename T>
30 void describe_entity_type(auto& log, T& entity)
31 {
32 if constexpr (Bang<T>) log.println("bang");
33 else if constexpr (has_value<T>)
34 {
35 if constexpr (OccasionalValue<T>)
36 {
37 if constexpr (array_like<value_t<T>>)
38 log.print("array of ");
39 else log.print("occasional ");
40 }
41 else if constexpr (PersistentValue<T>)
42 {
43 if constexpr (array_like<value_t<T>>)
44 log.print("array of ");
45 else log.print("persistent ");
46 }
47 if constexpr (std::integral<element_t<T>>)
48 log.println("int");
49 else if constexpr (std::floating_point<element_t<T>>)
50 log.println("float");
51 else if constexpr (string_like<element_t<T>>)
52 log.println("text");
53 else log.println("unknown value type");
54 }
55 else if constexpr (Component<T>) log.println("component");
56 else log.println("unknown");
57 }
58
59 template<typename T>
60 void describe_entity_value(auto& log, T& entity)
61 {
62 if constexpr (Bang<T>)
63 {
64 if (flag_state_of(entity)) log.println("(! bang !)");
65 else log.println("()");
66 }
67 else if constexpr (OccasionalValue<T>)
68 {
69 if (flag_state_of(entity)) log.println("(! ", value_of(entity), " !)");
70 else log.println("(", value_of(entity), ")");
71 }
72 else if constexpr (PersistentValue<T>)
73 {
74 if constexpr (tagged_write_only<T>) log.println("WRITE ONLY");
75 else if constexpr (string_like<value_t<T>>)
76 log.println("\"", value_of(entity), "\"");
77 else log.println(value_of(entity));
78 }
79 }
80
81 template<typename T, typename Components>
82 void describe_entity(auto& log, auto preface, T& entity, auto ... indents)
83 {
84 static_assert(has_name<T>);
85 log.println(indents..., preface, (const char *)osc_path_v<T, Components>);
86 log.println(indents..., " name: \"", entity.name(), "\"");
87 log.print(indents..., " type: ");
88 describe_entity_type(log, entity);
89 if constexpr (has_range<T>)
90 {
91 log.print(indents..., " range: ");
92 auto range = get_range<T>();
93 log.println(range.min, " to ", range.max, " (init: ", range.init, ")");
94 }
95 if constexpr (has_value<T>)
96 {
97 log.print(indents..., " value: ");
98 describe_entity_value(log, entity);
99 }
100 if constexpr (Component<T>)
101 {
102 auto describe_group = [&](auto& group, auto groupname)
103 {
104 boost::pfr::for_each_field(group, [&]<typename Y>(Y& endpoint)
105 {
106 describe_entity<Y, Components>(log, groupname, endpoint, " ", indents...);
107 });
108 };
109 if constexpr (has_inputs<T>) describe_group(inputs_of(entity), "input: ");
110 if constexpr (has_outputs<T>) describe_group(outputs_of(entity), "output: ");
111 }
112 }
113
114 template<typename Components>
115 int main(int argc, char** argv, auto& log, Components& components)
116 {
117 if (argc < 2) return 2;
118 for_each_node(components, [&]<typename T>(T& node, auto)
119 {
120 if constexpr (has_name<T>)
121 if (osc_match_pattern(argv[1], osc_path_v<T, Components>))
122 describe_entity<T, Components>(log, "entity: ", node);
123 });
124 return 0;
125 };
126
127};
128
130} } // namespaces
Definition sygac-endpoints.hpp:92
Check if T is a Sygaldry component.
Definition sygac-components.hpp:124
Definition sygac-endpoints.hpp:90
Definition sygac-endpoints.hpp:63
Definition sygac-endpoints.hpp:166
Definition sygac-endpoints.hpp:25
Definition sygac-endpoints.hpp:121
Definition sygac-endpoints.hpp:161
bool osc_match_pattern(const char *pattern, const char *address)
Match an OSC address pattern against the given address.
Definition sygbp-osc_match_pattern.cpp:12
Definition describe.hpp:24