summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorÁlvaro Fernández Rojas2025-11-10 16:36:59 +0000
committerÁlvaro Fernández Rojas2025-11-14 15:14:50 +0000
commita75209f62982f7218f73b9b4fd9b705e19f5f94a (patch)
tree27e34c84cdd6767e2b684bf65800c95a1082b5e7
parent97d1839295e5d8a28087d8a883d6405d900c8e82 (diff)
downloadlibubox-a75209f62982f7218f73b9b4fd9b705e19f5f94a.tar.gz
blobmsg: refactor blobmsg_cast_u64/s64
Instead of calling blobmsg_type() for each if/else block, just call it once and use it with a switch. Link: https://github.com/openwrt/libubox/pull/24 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
-rw-r--r--blobmsg.h46
1 files changed, 33 insertions, 13 deletions
diff --git a/blobmsg.h b/blobmsg.h
index f2fc0d0..a34c4f7 100644
--- a/blobmsg.h
+++ b/blobmsg.h
@@ -305,32 +305,52 @@ static inline uint64_t blobmsg_get_u64(struct blob_attr *attr)
static inline uint64_t blobmsg_cast_u64(struct blob_attr *attr)
{
- uint64_t tmp = 0;
+ const int type = blobmsg_type(attr);
+ uint64_t tmp;
- if (blobmsg_type(attr) == BLOBMSG_TYPE_INT64)
+ switch (type) {
+ case BLOBMSG_TYPE_INT64:
tmp = blobmsg_get_u64(attr);
- else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT32)
+ break;
+ case BLOBMSG_TYPE_INT32:
tmp = blobmsg_get_u32(attr);
- else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT16)
+ break;
+ case BLOBMSG_TYPE_INT16:
tmp = blobmsg_get_u16(attr);
- else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8)
+ break;
+ case BLOBMSG_TYPE_INT8:
tmp = blobmsg_get_u8(attr);
+ break;
+ default:
+ tmp = 0;
+ break;
+ }
return tmp;
}
static inline int64_t blobmsg_cast_s64(struct blob_attr *attr)
{
- int64_t tmp = 0;
+ const int type = blobmsg_type(attr);
+ int64_t tmp;
- if (blobmsg_type(attr) == BLOBMSG_TYPE_INT64)
+ switch (type) {
+ case BLOBMSG_TYPE_INT64:
tmp = blobmsg_get_u64(attr);
- else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT32)
- tmp = (int32_t)blobmsg_get_u32(attr);
- else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT16)
- tmp = (int16_t)blobmsg_get_u16(attr);
- else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8)
- tmp = (int8_t)blobmsg_get_u8(attr);
+ break;
+ case BLOBMSG_TYPE_INT32:
+ tmp = (int32_t) blobmsg_get_u32(attr);
+ break;
+ case BLOBMSG_TYPE_INT16:
+ tmp = (int16_t) blobmsg_get_u16(attr);
+ break;
+ case BLOBMSG_TYPE_INT8:
+ tmp = (int8_t) blobmsg_get_u8(attr);
+ break;
+ default:
+ tmp = 0;
+ break;
+ }
return tmp;
}