pars 0.2.1
Loading...
Searching...
No Matches
flags.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include <spdlog/spdlog.h>
33
34#include <format>
35
36namespace pars
37{
38
39enum class lf
40{
41 app = 1,
42 comp = 2,
43 event = 4,
44 net = 8,
45 user = 1073741824,
46};
47
48constexpr static int operator|(const lf& a, const lf& b)
49{
50 return static_cast<int>(a) | static_cast<int>(b);
51}
52
53constexpr static int operator|(const int& a, const lf& b)
54{
55 return a | static_cast<int>(b);
56}
57
58constexpr static int operator|(const lf& a, const int& b)
59{
60 return static_cast<int>(a) | b;
61}
62
63constexpr static int operator&(const lf& a, const int& b)
64{
65 return static_cast<int>(a) & b;
66}
67
68constexpr static int operator&(const int& a, const lf& b)
69{
70 return a & static_cast<int>(b);
71}
72
73constexpr static int operator&(const lf& a, const lf& b)
74{
75 return static_cast<int>(a) & static_cast<int>(b);
76}
77
78namespace f
79{
80
81struct lf
82{
84 : val{static_cast<int>(flags)}
85 {
86 }
87
88 lf(int flags)
89 : val{flags}
90 {
91 }
92
93 int val;
94};
95
96} // namespace f
97
98} // namespace pars
99
100template<>
101struct std::formatter<::pars::lf> : formatter<std::string>
102{
103 auto format(const ::pars::lf& flag, format_context& ctx) const
104 -> decltype(ctx.out())
105 {
106 switch (flag)
107 {
108 case ::pars::lf::app:
109 return std::format_to(ctx.out(), "app");
110 case ::pars::lf::comp:
111 return std::format_to(ctx.out(), "comp");
112 case ::pars::lf::event:
113 return std::format_to(ctx.out(), "event");
114 case ::pars::lf::net:
115 return std::format_to(ctx.out(), "net");
116 case ::pars::lf::user:
117 return std::format_to(ctx.out(), "user");
118 default:
119 return std::format_to(ctx.out(), "<lf-{}>", static_cast<int>(flag));
120 }
121 }
122};
123
124template<>
125struct std::formatter<::pars::f::lf> : formatter<std::string>
126{
127 auto format(const ::pars::f::lf& flags, format_context& ctx) const
128 -> decltype(ctx.out())
129 {
130 auto it = ctx.out();
131
132 for (const auto& f : {::pars::lf::app, ::pars::lf::comp, ::pars::lf::event,
134 {
135 if (!(flags.val & f))
136 continue;
137
138 it = std::format_to(it, " {}", f);
139 }
140
141 return it;
142 }
143};
lf
Definition flags.h:40
@ comp
Definition flags.h:42
@ net
Definition flags.h:44
@ event
Definition flags.h:43
@ app
Definition flags.h:41
@ user
Definition flags.h:45
lf(int flags)
Definition flags.h:88
int val
Definition flags.h:93
lf(::pars::lf flags)
Definition flags.h:83
auto format(const ::pars::f::lf &flags, format_context &ctx) const -> decltype(ctx.out())
Definition flags.h:127
auto format(const ::pars::lf &flag, format_context &ctx) const -> decltype(ctx.out())
Definition flags.h:103