From 31c7d4e31b4608260d366acc215b0114661ea42e Mon Sep 17 00:00:00 2001 From: timerix Date: Thu, 19 Jan 2023 20:58:30 +0600 Subject: [PATCH] PACK_ENUM --- src/Filesystem/file.h | 4 ++-- src/base/errors.h | 4 ++-- src/base/std.h | 4 ++++ src/kprint/kprint_colors.h | 10 +++++----- src/kprint/kprint_format.h | 4 ++-- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Filesystem/file.h b/src/Filesystem/file.h index 1f0f045..da0f45a 100644 --- a/src/Filesystem/file.h +++ b/src/Filesystem/file.h @@ -13,7 +13,7 @@ Array_declare(FilePath); typedef FILE File; ktid_declare(File); -typedef enum FileOpenMode{ +PACK_ENUM(FileOpenMode, // open a file for reading FileOpenMode_Read=1, // (re)create a file for writing @@ -24,7 +24,7 @@ typedef enum FileOpenMode{ FileOpenMode_ReadWrite=FileOpenMode_Read|FileOpenMode_Write, // opens file for readng/writing additional data to the end / creates new file FileOpenMode_ReadAppend=FileOpenMode_Read|FileOpenMode_Append -} FileOpenMode; + ) /// @brief opens file diff --git a/src/base/errors.h b/src/base/errors.h index d3cb5ac..7c46733 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -7,13 +7,13 @@ extern "C" { #include "std.h" #include "type_system/unitype.h" -typedef enum ErrorId { +PACK_ENUM(ErrorId, SUCCESS, // not an error ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR, ERR_KEYNOTFOUND, ERR_FORMAT, ERR_UNEXPECTEDVAL, ERR_IO, ERR_IO_EOF -} ErrorId; +) char* errname(ErrorId err); diff --git a/src/base/std.h b/src/base/std.h index 0dc29bc..bc45b55 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -118,6 +118,10 @@ You can even embed it into macro in header (see kprint.h) CODE;\ PRAGMA_WARNING_POP +#define PACK_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME {\ + ENUM_MEMBERS\ +} __attribute__((__packed__)) ENUM_NAME; + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/kprint/kprint_colors.h b/src/kprint/kprint_colors.h index acc15ac..5fc26f3 100644 --- a/src/kprint/kprint_colors.h +++ b/src/kprint/kprint_colors.h @@ -8,7 +8,7 @@ extern "C" { // ^ ^^^^ // | color num // fgColorSet flag -typedef enum kp_fgColor{ +PACK_ENUM(kp_fgColor, /// black foreground kp_fgBlack = 0x80000000, /// dark red foreground @@ -41,12 +41,12 @@ typedef enum kp_fgColor{ kp_fgCyan = 0x8e000000, /// white foreground kp_fgWhite = 0x8f000000 -} kp_fgColor; +) // 01000000 00000000 00000000 00000000 // ^ ^^^^ // bgColorSet flag color num -typedef enum kp_bgColor{ +PACK_ENUM(kp_bgColor, /// black background kp_bgBlack = 0x40000000, /// dark red background @@ -78,8 +78,8 @@ typedef enum kp_bgColor{ /// cyan background kp_bgCyan = 0x40e00000, /// white background - kp_bgWhite = 0x40f00000 -} kp_bgColor; + kp_bgWhite = 0x40f00000 +) #if __cplusplus } diff --git a/src/kprint/kprint_format.h b/src/kprint/kprint_format.h index 8f72c94..9efceae 100644 --- a/src/kprint/kprint_format.h +++ b/src/kprint/kprint_format.h @@ -10,7 +10,7 @@ extern "C" { /// kprint_format typedef uint32 kp_fmt; -typedef enum kp_dataFmt{ +PACK_ENUM(kp_dataFmt, // 00000000 00000000 00000000 00000000 // ^^^^ // type @@ -32,7 +32,7 @@ typedef enum kp_dataFmt{ // ^ // uppercase flag kp_upper=0x10000000 -} kp_dataFmt; +) #define kp_fmt_fgColorSet(FMT) (bool)((FMT&0x80000000)!=0) #define kp_fmt_bgColorSet(FMT) (bool)((FMT&0x40000000)!=0)