Shaka Player Embedded
macros.h
Go to the documentation of this file.
1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_UTIL_MACROS_H_
16 #define SHAKA_EMBEDDED_UTIL_MACROS_H_
17 
18 #include <glog/logging.h>
19 
20 #include <iostream>
21 #include <string>
22 
23 #include "shaka/macros.h"
24 
25 #define LOG_ONCE(severity) LOG_FIRST_N(severity, 1)
26 
27 
28 #ifdef __GNUC__
29 # define PRINTF_FORMAT(format_arg, dots_arg) \
30  __attribute__((format(printf, format_arg, dots_arg)))
31 # define NO_SANITIZE(name) __attribute__((no_sanitize(name)))
32 # define FALL_THROUGH_INTENDED [[clang::fallthrough]] // NOLINT
33 # define BEGIN_ALLOW_COMPLEX_STATICS \
34  _Pragma("clang diagnostic push") \
35  _Pragma("clang diagnostic ignored \"-Wexit-time-destructors\"") \
36  _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"")
37 # define END_ALLOW_COMPLEX_STATICS _Pragma("clang diagnostic pop")
38 #else
39 # define PRINTF_FORMAT(format, dots)
40 # define NO_SANITIZE(name)
41 # define FALL_THROUGH_INTENDED
42 # define BEGIN_ALLOW_COMPLEX_STATICS
43 # define END_ALLOW_COMPLEX_STATICS
44 #endif
45 
46 #ifdef __GNUC__
47 # define MUST_USE_RESULT __attribute__((warn_unused_result))
48 #elif defined(_MSC_VER) && _MSC_VER >= 1700
49 # include <sal.h>
50 # define MUST_USE_RESULT _Check_return_
51 #else
52 # define MUST_USE_RESULT
53 #endif
54 
55 // Type is the name of the generated type.
56 // DEFINE_ENUM is a macro that will define the enum; it will be called twice
57 // given either ENUM_IMPL or CASE_IMPL.
58 // ENUM_IMPL and CASE_IMPL are macros that are passed as arguments to
59 // DEFINE_ENUM.
60 #define DEFINE_ENUM_AND_TO_STRING_GENERIC_(Type, DEFINE_ENUM, ENUM_IMPL, \
61  CASE_IMPL) \
62  enum class Type { DEFINE_ENUM(ENUM_IMPL) }; \
63  inline std::string to_string(Type e) noexcept { \
64  using Enum = Type; \
65  switch (e) { \
66  DEFINE_ENUM(CASE_IMPL) \
67  default: \
68  LOG(FATAL) << "Unknown enum value " << static_cast<int>(e); \
69  } \
70  } \
71  inline std::ostream& operator<<(std::ostream& os, Type e) { \
72  return os << to_string(e); \
73  }
74 
75 #define DEFINE_ENUM_IMPL_(name) name,
76 #define DEFINE_CASE_IMPL_(name) case Enum::name: return #name;
77 
89 #define DEFINE_ENUM_AND_TO_STRING(Type, DEFINE_ENUM) \
90  DEFINE_ENUM_AND_TO_STRING_GENERIC_(Type, DEFINE_ENUM, DEFINE_ENUM_IMPL_, \
91  DEFINE_CASE_IMPL_)
92 
93 #define DEFINE_ENUM_IMPL_2_(name, str) name,
94 #define DEFINE_CASE_IMPL_2_(name, str) case Enum::name: return str;
95 
107 #define DEFINE_ENUM_AND_TO_STRING_2(Type, DEFINE_ENUM) \
108  DEFINE_ENUM_AND_TO_STRING_GENERIC_(Type, DEFINE_ENUM, DEFINE_ENUM_IMPL_2_, \
109  DEFINE_CASE_IMPL_2_)
110 
111 #endif // SHAKA_EMBEDDED_UTIL_MACROS_H_