| 1 | #ifndef COMMON_HPP |
| 2 | #define COMMON_HPP |
| 3 | |
| 4 | #include <iostream> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "format.hpp" |
| 8 | |
| 9 | namespace |
| 10 | { |
| 11 | const char GI_PTR = '*'; |
| 12 | const std::string GI_SUFFIX_REF = "_Ref"; |
| 13 | const std::string GI_SUFFIX_CF_CTYPE = "_CF_CType"; |
| 14 | const std::string GI_SUFFIX_CB_TRAIT = "_CB_Trait"; |
| 15 | |
| 16 | const std::string GIR_GOBJECT("GObject.Object"); |
| 17 | const std::string GIR_GINITIALLYUNOWNED("GObject.InitiallyUnowned"); |
| 18 | const std::string GIR_GVARIANT("GLib.Variant"); |
| 19 | const std::string GIR_VOID("none"); |
| 20 | const std::string GIR_GDESTROYNOTIFY("GLib.DestroyNotify"); |
| 21 | |
| 22 | const std::string GDESTROYNOTIFY("GDestroyNotify"); |
| 23 | const std::string CPP_VOID("void"); |
| 24 | const std::string GIR_SUFFIX(".gir"); |
| 25 | |
| 26 | const std::string PT_ATTR("<xmlattr>"); |
| 27 | const std::string GI_NS("gi"); |
| 28 | const std::string GI_NS_INTERNAL("internal"); |
| 29 | const std::string GI_NS_IMPL("impl"); |
| 30 | const std::string GI_NS_ARGS("callargs"); |
| 31 | const std::string GI_SCOPE("::"); |
| 32 | const std::string GI_NS_SCOPED("gi::"); |
| 33 | const std::string GI_NS_DETAIL_SCOPED("gi::detail::"); |
| 34 | const std::string GI_REPOSITORY_NS("repository"); |
| 35 | const std::string GI_INLINE("GI_INLINE_DECL"); |
| 36 | const std::string GI_CLASS_IMPL_BEGIN("GI_CLASS_IMPL_BEGIN"); |
| 37 | const std::string GI_CLASS_IMPL_END("GI_CLASS_IMPL_END"); |
| 38 | const std::string GI_DISABLE_DEPRECATED_WARN_BEGIN( |
| 39 | "GI_DISABLE_DEPRECATED_WARN_BEGIN"); |
| 40 | const std::string GI_DISABLE_DEPRECATED_WARN_END( |
| 41 | "GI_DISABLE_DEPRECATED_WARN_END"); |
| 42 | const std::string EMPTY; |
| 43 | |
| 44 | const std::string EL_REPOSITORY("repository"); |
| 45 | const std::string EL_CINCLUDE("c:include"); |
| 46 | const std::string EL_ALIAS("alias"); |
| 47 | const std::string EL_ENUM("enumeration"); |
| 48 | const std::string EL_FLAGS("bitfield"); |
| 49 | const std::string EL_MEMBER("member"); |
| 50 | const std::string EL_CONST("constant"); |
| 51 | const std::string EL_OBJECT("class"); |
| 52 | const std::string EL_INTERFACE("interface"); |
| 53 | const std::string EL_RECORD("record"); |
| 54 | const std::string EL_CALLBACK("callback"); |
| 55 | |
| 56 | const std::string EL_FUNCTION("function"); |
| 57 | const std::string EL_CONSTRUCTOR("constructor"); |
| 58 | const std::string EL_METHOD("method"); |
| 59 | const std::string EL_VIRTUAL_METHOD("virtual-method"); |
| 60 | const std::string EL_FIELD("field"); |
| 61 | const std::string EL_PROPERTY("property"); |
| 62 | const std::string EL_SIGNAL("glib:signal"); |
| 63 | |
| 64 | const std::string EL_RETURN("return-value"); |
| 65 | const std::string EL_PARAMETERS("parameters"); |
| 66 | const std::string EL_INSTANCE_PARAMETER("instance-parameter"); |
| 67 | const std::string EL_PARAMETER("parameter"); |
| 68 | |
| 69 | const std::string EL_TYPE("type"); |
| 70 | const std::string EL_ARRAY("array"); |
| 71 | const std::string EL_VARARGS("varargs"); |
| 72 | const std::string EL_IMPLEMENTS("implements"); |
| 73 | |
| 74 | const std::string AT_SHARED_LIBRARY("shared-library"); |
| 75 | const std::string AT_DEPRECATED("deprecated"); |
| 76 | const std::string AT_INTROSPECTABLE("introspectable"); |
| 77 | const std::string AT_FOREIGN("foreign"); |
| 78 | const std::string AT_SHADOWS("shadows"); |
| 79 | const std::string AT_SHADOWED_BY("shadowed-by"); |
| 80 | const std::string AT_DISGUISED("disguised"); |
| 81 | const std::string AT_MOVED_TO("moved-to"); |
| 82 | const std::string AT_VERSION("version"); |
| 83 | |
| 84 | const std::string AT_NAME("name"); |
| 85 | const std::string AT_TRANSFER("transfer-ownership"); |
| 86 | const std::string AT_DIRECTION("direction"); |
| 87 | const std::string AT_CLOSURE("closure"); |
| 88 | const std::string AT_DESTROY("destroy"); |
| 89 | const std::string AT_SCOPE("scope"); |
| 90 | const std::string AT_NULLABLE("nullable"); |
| 91 | const std::string AT_OPTIONAL("optional"); |
| 92 | const std::string AT_ALLOW_NONE("allow-none"); |
| 93 | const std::string AT_CALLER_ALLOCATES("caller-allocates"); |
| 94 | const std::string AT_GLIB_GET_TYPE("glib:get-type"); |
| 95 | const std::string AT_GLIB_FUNDAMENTAL("glib:fundamental"); |
| 96 | const std::string AT_GLIB_TYPE_STRUCT("glib:type-struct"); |
| 97 | const std::string AT_GLIB_IS_TYPE_STRUCT_FOR("glib:is-gtype-struct-for"); |
| 98 | |
| 99 | const std::string AT_PARENT("parent"); |
| 100 | |
| 101 | const std::string AT_LENGTH("length"); |
| 102 | const std::string AT_ZERO_TERMINATED("zero-terminated"); |
| 103 | const std::string AT_FIXED_SIZE("fixed-size"); |
| 104 | |
| 105 | const std::string AT_THROWS("throws"); |
| 106 | const std::string AT_CTYPE("c:type"); |
| 107 | const std::string AT_CIDENTIFIER("c:identifier"); |
| 108 | |
| 109 | const std::string TRANSFER_NOTHING("none"); |
| 110 | const std::string TRANSFER_FULL("full"); |
| 111 | const std::string TRANSFER_CONTAINER("container"); |
| 112 | |
| 113 | const std::string SCOPE_NOTIFIED("notified"); |
| 114 | const std::string SCOPE_ASYNC("async"); |
| 115 | const std::string SCOPE_CALL("call"); |
| 116 | |
| 117 | const std::string DIR_IN("in"); |
| 118 | const std::string DIR_OUT("out"); |
| 119 | const std::string DIR_INOUT("inout"); |
| 120 | const std::string DIR_RETURN("return"); |
| 121 | |
| 122 | const std::string AT_READABLE("readable"); |
| 123 | const std::string AT_WRITABLE("writable"); |
| 124 | const std::string AT_PRIVATE("private"); |
| 125 | } // namespace |
| 126 | |
| 127 | enum class Log { NONE, ERROR, WARNING, INFO, DEBUG, LOG }; |
| 128 | |
| 129 | extern Log _loglevel; |
| 130 | |
| 131 | template<typename T> |
| 132 | void |
| 133 | logger(Log level, const T &m) |
| 134 | { |
| 135 | static std::string lvl[] = {"NONE", "ERROR", "WARN", "INFO", "DEBUG", "LOG"}; |
| 136 | if (level <= _loglevel) |
| 137 | std::cerr << lvl[(int)level] << " " << m << std::endl; |
| 138 | } |
| 139 | |
| 140 | template<typename T, typename ARG, typename... ARGS> |
| 141 | void |
| 142 | logger(Log level, const T &m, ARG &&arg, ARGS &&...args) |
| 143 | { |
| 144 | logger(level, |
| 145 | fmt::format(m, std::forward<ARG>(arg), std::forward<ARGS>(args)...)); |
| 146 | } |
| 147 | |
| 148 | inline bool |
| 149 | is_qualified(const std::string &name) |
| 150 | { |
| 151 | return (name.find(':') != name.npos) || (name.find('.') != name.npos); |
| 152 | } |
| 153 | |
| 154 | inline std::string |
| 155 | toupper(const std::string &s) |
| 156 | { |
| 157 | auto c = s; |
| 158 | for (auto &ch : c) |
| 159 | ch = std::toupper(ch); |
| 160 | return c; |
| 161 | } |
| 162 | |
| 163 | inline std::string |
| 164 | tolower(const std::string &s) |
| 165 | { |
| 166 | auto c = s; |
| 167 | for (auto &ch : c) |
| 168 | ch = std::tolower(ch); |
| 169 | return c; |
| 170 | } |
| 171 | |
| 172 | #endif // COMMON_HPP |