HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
audio_device_id.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../pickle.hpp"
8#include "../exception.hpp"
9#include "../check.hpp"
10#include <array>
11
12namespace tt {
13
15public:
16 static constexpr char none = 0;
17 static constexpr char win32 = 1;
18 static constexpr char macos = 2;
19 static constexpr char asio = 3;
20
21 audio_device_id() noexcept : _v{}
22 {
23 }
24
25 audio_device_id(char type, wchar_t const *id) noexcept;
26 constexpr audio_device_id(audio_device_id const &) noexcept = default;
27 constexpr audio_device_id(audio_device_id &&) noexcept = default;
28 constexpr audio_device_id &operator=(audio_device_id const &) noexcept = default;
29 constexpr audio_device_id &operator=(audio_device_id &&) noexcept = default;
30
31 explicit operator bool() const noexcept
32 {
33 return _v[0] != none;
34 }
35
36 [[nodiscard]] friend bool operator==(audio_device_id const &lhs, audio_device_id const &rhs) noexcept = default;
37
38private:
40
41 friend struct pickle<audio_device_id>;
42};
43
44template<>
46 [[nodiscard]] datum encode(audio_device_id const &rhs) const noexcept
47 {
48 auto r = std::string{};
49
50 auto t = rhs._v[0];
51 if (t == audio_device_id::none) {
52 return datum{std::move(r)};
53 } else if (t == audio_device_id::win32) {
54 r += 'w';
55 for (auto i = 1_uz; i != std::size(rhs._v); ++i) {
56 if (auto c = rhs._v[i]) {
57 r += c;
58 } else {
59 break;
60 }
61 }
62 return datum{std::move(r)};
63
64 } else {
65 tt_not_implemented();
66 }
67 }
68
69 [[nodiscard]] audio_device_id decode(std::string const &rhs) const
70 {
71 auto r = audio_device_id{};
72 if (rhs.empty()) {
73 return r;
74
75 } else {
76 auto t = rhs[0];
77 if (t == 'w') {
78 tt_parse_check(std::size(rhs) <= std::size(r._v), "win32-audio_device_id pickle size to large {}", rhs);
79
80 r._v[0] = audio_device_id::win32;
81 auto i = 1_uz;
82 for (; i != std::size(rhs); ++i) {
83 r._v[i] = rhs[i];
84 }
85 if (i != std::size(r._v)) {
86 r._v[i] = 0;
87 }
88 return r;
89
90 } else {
91 throw parse_error("audio_device_id pickle unknown type {}", t);
92 }
93 }
94 }
95
96 [[nodiscard]] audio_device_id decode(datum rhs) const
97 {
98 if (auto *s = get_if<std::string>(rhs)) {
99 return decode(*s);
100
101 } else {
102 throw parse_error("audio_device_id must be encoded as a string, got {}", rhs);
103 }
104 }
105};
106
107} // namespace tt
Definition audio_device_id.hpp:14
A dynamic data type.
Definition datum.hpp:213
Exception thrown during parsing on an error.
Definition exception.hpp:26
Encode and decode a type to and from a UTF-8 string.
Definition pickle.hpp:22
T decode(datum rhs) const
Decode a UTF-8 string into a value of a given type.
datum encode(T const &rhs) const noexcept
Encode the value of a type into a UTF-8 string.
T empty(T... args)
T move(T... args)