Bifrost SDK
Bifrost SDK documentation
Message.h
Go to the documentation of this file.
1//-
2// =============================================================================
3// Copyright 2025 Autodesk, Inc. All rights reserved.
4//
5// Use of this software is subject to the terms of the Autodesk license
6// agreement provided at the time of installation or download, or which
7// otherwise accompanies this software in either electronic or hard copy form.
8// =============================================================================
9
10//+
11
14
15#ifndef AMINO_CORE_MESSAGE_H
16#define AMINO_CORE_MESSAGE_H
17
19
21#include <Amino/Core/Source.h>
22#include <Amino/Core/String.h>
24#include <Amino/Core/internal/Format.h>
25#include <Amino/Core/internal/PImpl.h>
26
27#define AMINO_API AMINO_CORE_SHARED_DECL
28
29//==============================================================================
30// NAMESPACE Amino
31//==============================================================================
32
33namespace Amino {
34
35//==============================================================================
36// ENUM MessageKind
37//==============================================================================
38
40enum class MessageKind : int { //
41 eError,
43 eInfo,
44 eDebug
45};
46
47//==============================================================================
48// CLASS MessageClass
49//==============================================================================
50
83class MessageClass final {
84public:
87
89 // NOLINTNEXTLINE(google-explicit-constructor)
90 /*implicit*/ constexpr MessageClass(StringLiteral literal) noexcept
91 : m_name{literal} {}
92
94 StringView getName() const { return m_name; }
95
97 AMINO_API bool isDefault() const;
98
100 bool operator==(MessageClass const& o) const {
101 return static_cast<void const*>(m_name.data()) ==
102 static_cast<void const*>(o.m_name.data());
103 }
104
106 bool operator!=(MessageClass const& o) const { return !operator==(o); }
107
108private:
109 StringView m_name;
110};
111
112//==============================================================================
113// CLASS MessageText
114//==============================================================================
115
135class MessageText final {
136public:
138 // NOLINTNEXTLINE(google-explicit-constructor)
139 /*implicit*/ constexpr MessageText(StringLiteral literal) noexcept
140 : m_format{literal} {} // LCOV_EXCL_BR_LINE
141
143 StringView getFormat() const& { return m_format; }
144
145private:
146 StringView m_format;
147};
148
149//==============================================================================
150// CLASS Message
151//==============================================================================
152
170class Message final {
171public:
172 /*----- static member functions -----*/
173
175 static constexpr StringView toString(MessageKind kind) {
176 using namespace Amino::StringViewLiterals;
177 switch (kind) { // LCOV_EXCL_BR_LINE
178 case MessageKind::eError: return "Error"_asv;
179 case MessageKind::eWarning: return "Warning"_asv;
180 case MessageKind::eInfo: return "Info"_asv;
181 case MessageKind::eDebug: return "Debug"_asv;
182 }
183 return "Unknown"_asv; // LCOV_EXCL_LINE
184 }
185
186 /*----- member functions -----*/
187
190
194 Source source, MessageKind kind, MessageClass cls, MessageText text);
195
202 // LCOV_EXCL_BR_START
203 template <typename Arg, typename... Args>
205 Source source,
206 MessageKind kind,
207 MessageClass cls,
208 MessageText text,
209 Arg const& arg,
210 Args const&... args)
211 : Message{
212 std::move(source), kind, cls, text,
213 SpanParam{Internal::FormatArgs{arg, args...}}} {}
214 // LCOV_EXCL_BR_STOP
215
218
220 AMINO_API Message(Message&& o) noexcept;
221
224
227
230
233 AMINO_API bool isValid() const;
234 explicit operator bool() const { return isValid(); }
236
238 AMINO_API Source const& getSource() const;
239
242
245
248
250 template <typename Os>
251 AMINO_INTERNAL_FORCEINLINE void toStream(
252 Os& os, bool newline = true) const {
253 internal_toStream(toStreamFor<Os>, static_cast<void*>(&os));
254 if (newline) os << '\n';
255 }
256
262
263private:
265
266 /*----- types -----*/
267
268 using FormatFn =
269 void (*)(SpanParam<StringView const> const& strs, void* client);
270
271 /*----- static member functions -----*/
272
273 // LCOV_EXCL_BR_START
274 template <typename Os>
275 static void toStreamFor(
276 SpanParam<StringView const> const& strs, void* client) {
277 Os& os = *static_cast<Os*>(client);
278 for (auto const& s : strs) os.write(s.data(), s.size());
279 }
280 // LCOV_EXCL_BR_STOP
281
282 /*----- member functions -----*/
283
286 Source source,
287 MessageKind kind,
288 MessageClass cls,
289 MessageText text,
291
293 AMINO_API void internal_toStream(FormatFn fn, void* client) const;
294
295 /*----- data members -----*/
296
297 Internal::PImpl<Message, 2> m_impl;
299};
300
301//==============================================================================
302// CLASS MessageFilter
303//==============================================================================
304
306class alignas(void*) MessageFilter {
307public:
308 /*----- types -----*/
309
311 enum Level : int {
313 eError = static_cast<int>(MessageKind::eError),
314 eWarning = static_cast<int>(MessageKind::eWarning),
315 eInfo = static_cast<int>(MessageKind::eInfo),
316 eDebug = static_cast<int>(MessageKind::eDebug)
317 };
318
319 /*----- static member functions -----*/
320
322 static constexpr Level asLevel(MessageKind kind) {
323 return static_cast<Level>(kind);
324 }
325
326 /*----- member functions -----*/
327
333 MessageFilter() noexcept = default;
334
337 explicit MessageFilter(MessageKind lvl) noexcept
338 : MessageFilter(asLevel(lvl)) {}
339 explicit MessageFilter(Level lvl) noexcept : m_level{lvl} {}
341
343 Level getLevel() const { return m_level; }
344
349 void setLevel(Level lvl) { m_level = lvl; }
351
353 bool isEnabled(MessageKind kind) const { return asLevel(kind) <= m_level; }
354
357 bool isEnabled() const { return m_level != eDisabled; }
358
359private:
360 /*----- data members -----*/
361
363 Level m_level = eInfo;
364};
365
366} // namespace Amino
367
368#undef AMINO_API
369
370#endif
Definition of macros for symbol visibility.
#define AMINO_API
Definition: Message.h:27
Forward declarations for task observer classes.
String class.
String view class (similar to std::string_view)
Definition: HostData.h:33
MessageKind
The kind of message (error, warning, info, debug).
Definition: Message.h:40
Definition: Ptr.h:2080
The "class" of the message.
Definition: Message.h:83
bool operator==(MessageClass const &o) const
Equality operator.
Definition: Message.h:100
constexpr MessageClass(StringLiteral literal) noexcept
Constructor from a string literal.
Definition: Message.h:90
AMINO_CORE_SHARED_DECL bool isDefault() const
Returns true if this is the default message class or not.
AMINO_CORE_SHARED_DECL MessageClass()
Default constructor (default message class).
bool operator!=(MessageClass const &o) const
Inequality operator.
Definition: Message.h:106
StringView getName() const
Returns the name of the message class.
Definition: Message.h:94
The text content from which to construct a message (with optional formatting specifiers).
Definition: Message.h:135
constexpr MessageText(StringLiteral literal) noexcept
Constructor from a string literal.
Definition: Message.h:139
StringView getFormat() const &
Get the format string.
Definition: Message.h:143
A Message object that of a specific MessageKind, optional MessageClass, and a MessageText that relate...
Definition: Message.h:170
AMINO_INTERNAL_FORCEINLINE void toStream(Os &os, bool newline=true) const
Write the formatted message to the given output stream.
Definition: Message.h:251
AMINO_CORE_SHARED_DECL bool isValid() const
Whether the message is valid or not (not default constructed).
AMINO_CORE_SHARED_DECL MessageKind getKind() const
Get the kind of message.
AMINO_CORE_SHARED_DECL Message & operator=(Message const &o)
Copy assignment operator.
AMINO_CORE_SHARED_DECL Message()
Default constructor (empty message).
AMINO_CORE_SHARED_DECL ~Message()
Destructor.
AMINO_CORE_SHARED_DECL MessageClass getClass() const
Get the message class.
static constexpr StringView toString(MessageKind kind)
Get the string representation of the MessageKind.
Definition: Message.h:175
Message(Source source, MessageKind kind, MessageClass cls, MessageText text, Arg const &arg, Args const &... args)
Constructs a message with the given Source, MessageKind, MessageClass, and MessageText and format arg...
Definition: Message.h:204
AMINO_CORE_SHARED_DECL Message(Message const &o)
Copy constructor.
AMINO_CORE_SHARED_DECL Message & operator=(Message &&o) noexcept
Move assignment operator.
AMINO_CORE_SHARED_DECL String getFormattedText() const
Get the formatted text of the message.
AMINO_CORE_SHARED_DECL Source const & getSource() const
Get the source of the message.
AMINO_CORE_SHARED_DECL Message(Source source, MessageKind kind, MessageClass cls, MessageText text)
Constructs a message with the given Source, MessageKind, MessageClass, and MessageText.
AMINO_CORE_SHARED_DECL Message(Message &&o) noexcept
Move constructor.
AMINO_CORE_SHARED_DECL String getText() const
Get the message text.
Helper class to filter messages based on their MessageKind.
Definition: Message.h:306
static constexpr Level asLevel(MessageKind kind)
Get the Level corresponding to the given MessageKind.
Definition: Message.h:322
Level getLevel() const
Get the message filter Level.
Definition: Message.h:343
bool isEnabled(MessageKind kind) const
Returns whether the given MessageKind is enabled.
Definition: Message.h:353
MessageFilter() noexcept=default
Default constructor.
Level
Same as MessageKind with the addition of a disabled level.
Definition: Message.h:311
void setLevel(MessageKind lvl)
Enables the MessageKind up to (and including) the given level.
Definition: Message.h:348
bool isEnabled() const
Returns whether logging is enabled (i.e. the level isn't eDisabled).
Definition: Message.h:357
MessageFilter(Level lvl) noexcept
Constructs a message filter from the given level.
Definition: Message.h:339
void setLevel(Level lvl)
Enables the MessageKind up to (and including) the given level.
Definition: Message.h:349
Generic opaque handle to a source (provenance).
Definition: Source.h:35
Same as Span but the constructor from a range is implicit, making it more convenient and safe to use ...
Definition: Span.h:178
The string class used by Amino.
Definition: String.h:46
String view class (similar to std::string_view).
Definition: StringView.h:42
constexpr char const * data() const noexcept
Get the string view data.
Definition: StringView.h:109