Skip to content

Commit

Permalink
Version 2.0 - Simplify usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ExoSkye committed Apr 14, 2023
1 parent 97dc82f commit 04b9459
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ TL;DR: Don't use this is in a public interface for a library (ie. the installed

// Packed struct

PACKED_STRUCT(
a {
int a;
char b;
short c;
int d;
}
);
packed_struct a {
int a;
char b;
short c;
int d;
};

// Non-packed struct
struct b {
Expand All @@ -41,7 +39,7 @@ This is the same for C++, however, you'd use `sizeof(a)` instead of `sizeof(stru

You only need to copy `packed.h` into your project, all other files aren't essential. (`main.c` and `main.cpp` are just used for testing).

This library is also compatible with the widely used `typedef struct x {} x;` pattern used in C, you just need to replace `struct x {}` in that example with `PACKED_STRUCT(x {})`.
This library is also compatible with the widely used `typedef struct x {} x;` pattern used in C, you just need to replace `struct x {}` in that example with `packed_struct x {}`.

## Compiler Support

Expand Down
6 changes: 2 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include "packed.h"
#include <stdio.h>

typedef PACKED_STRUCT(
a {
typedef packed_struct a {
int a;
char b;
short c;
int d;
}
) a;
} a;

int main() {

Expand Down
6 changes: 2 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
#include <cstdio>

int main() {
PACKED_STRUCT(
a {
packed_struct a {
int a;
char b;
short c;
int d;
}
);
};

struct b {
int a;
Expand Down
18 changes: 9 additions & 9 deletions packed.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@


#ifdef __GNUC__
#define PACKED_OUTER(KEYWORD, ...) KEYWORD __attribute__((packed)) __VA_ARGS__
#define PACKED_OUTER(KEYWORD) KEYWORD __attribute__((packed))
#elif defined(_MSC_VER)
#define PACKED_OUTER(KEYWORD, ...) __pragma( pack(push, 1) ) KEYWORD __VA_ARGS__ __pragma( pack(pop))
#define PACKED_OUTER(KEYWORD) __pragma(pack()) KEYWORD
#elif defined(__clang__)
#define PACKED_OUTER(KEYWORD, ...) KEYWORD __attribute__((packed)) __VA_ARGS__
#define PACKED_OUTER(KEYWORD) KEYWORD __attribute__((packed))
#elif defined(__CC65__)
#warning This compiler (CC65) does not support packed structs
#define PACKED_OUTER(KEYWORD, ...) KEYWORD __VA_ARGS__
#define PACKED_OUTER(KEYWORD) KEYWORD
#elif defined(__SDCC)
#warning This compiler (SDCC) does not support packed structs
#define PACKED_OUTER(KEYWORD, ...) KEYWORD __VA_ARGS__
#define PACKED_OUTER(KEYWORD) KEYWORD
#elif defined(__TINYC__)
#define PACKED_OUTER(KEYWORD, ...) KEYWORD __attribute__((packed)) __VA_ARGS__
#define PACKED_OUTER(KEYWORD) KEYWORD __attribute__((packed))
#else
#ifndef UNSUPPORTED_COMPILER_WARNING
#define UNSUPPORTED_COMPILER_WARNING
#warning This library does not currently have support for your compiler, consider submitting a pull-request for it?
#define PACKED_OUTER(__decl__) KEYWORD __VA_ARGS__
#define PACKED_OUTER(__decl__) KEYWORD
#endif
#endif

#define PACKED_STRUCT(...) PACKED_OUTER(struct, __VA_ARGS__)
#define PACKED_ENUM(...) PACKED_OUTER(enum, __VA_ARGS__)
#define packed_struct PACKED_OUTER(struct)
#define packed_enum PACKED_OUTER(enum)

0 comments on commit 04b9459

Please sign in to comment.