summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2023-03-02 12:17:44 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2023-03-02 12:17:44 -0400
commit086b1e48828740ce215ba3609dcb899c8687bfa0 (patch)
treeae4571905d2451c63a1651d0ddfe702adb2feb8f
parent87dbcb62ef00e6509d92ec46d9024e2d1c453df3 (diff)
TArray: Make `{,fast_}fill_with()` type generic
-rw-r--r--context.h41
-rw-r--r--include/pso/TArray.h36
-rw-r--r--src/pso/TSocket.cpp2
3 files changed, 44 insertions, 35 deletions
diff --git a/context.h b/context.h
index 2a3e8dc..09156df 100644
--- a/context.h
+++ b/context.h
@@ -204,6 +204,11 @@ public:
};
template<typename T2>
+ size_t size_as() {
+ return byte_size() / sizeof(T2);
+ };
+
+ template<typename T2>
TArray<T2, (n * sizeof(T))/sizeof(T2)> &to() {
typedef TArray<T2, sizeof(m_data)/sizeof(T2)> to_type;
return reinterpret_cast<to_type &>(*this);
@@ -217,12 +222,14 @@ public:
memset(m_data, val, byte_size());
};
- void fill_with(u8 val) {
- _fill_with<false>(val);
+ template<typename T2>
+ void fill_with(const T2 val) {
+ _fill_with<T2, false>(val);
};
- void fast_fill_with(u8 val) {
- _fill_with<true>(val);
+ template<typename T2>
+ void fast_fill_with(const T2 val) {
+ _fill_with<T2, true>(val);
};
template<typename T2>
@@ -246,26 +253,26 @@ public:
};
private:
- template<bool do_unroll_check>
- void _fill_with(u8 val) {
- u8 *bytes = as_bytes();
- size_t size = byte_size();
+ template<typename T2, bool do_unroll_check>
+ void _fill_with(const T2 val) {
+ T2 *data = as<T2>();
+ size_t size = size_as<T2>();
if (do_unroll_check && size <= 8) {
int i = 0;
switch (size) {
- case 8: bytes[i++] = val;
- case 7: bytes[i++] = val;
- case 6: bytes[i++] = val;
- case 5: bytes[i++] = val;
- case 4: bytes[i++] = val;
- case 3: bytes[i++] = val;
- case 2: bytes[i++] = val;
- case 1: bytes[i++] = val;
+ case 8: data[i++] = val;
+ case 7: data[i++] = val;
+ case 6: data[i++] = val;
+ case 5: data[i++] = val;
+ case 4: data[i++] = val;
+ case 3: data[i++] = val;
+ case 2: data[i++] = val;
+ case 1: data[i++] = val;
default: break;
}
} else {
for (int i = 0; i < size; ++i) {
- bytes[i] = val;
+ data[i] = val;
}
}
};
diff --git a/include/pso/TArray.h b/include/pso/TArray.h
index 7f53785..8dd736b 100644
--- a/include/pso/TArray.h
+++ b/include/pso/TArray.h
@@ -59,12 +59,14 @@ public:
memset(m_data, val, byte_size());
};
- void fill_with(u8 val) {
- _fill_with<false>(val);
+ template<typename T2>
+ void fill_with(const T2 val) {
+ _fill_with<T2, false>(val);
};
- void fast_fill_with(u8 val) {
- _fill_with<true>(val);
+ template<typename T2>
+ void fast_fill_with(const T2 val) {
+ _fill_with<T2, true>(val);
};
template<typename T2>
@@ -88,26 +90,26 @@ public:
};
private:
- template<bool do_unroll_check>
- void _fill_with(u8 val) {
- u8 *bytes = as_bytes();
- size_t size = byte_size();
+ template<typename T2, bool do_unroll_check>
+ void _fill_with(const T2 val) {
+ T2 *data = as<T2>();
+ size_t size = size_as<T2>();
if (do_unroll_check && size <= 8) {
int i = 0;
switch (size) {
- case 8: bytes[i++] = val;
- case 7: bytes[i++] = val;
- case 6: bytes[i++] = val;
- case 5: bytes[i++] = val;
- case 4: bytes[i++] = val;
- case 3: bytes[i++] = val;
- case 2: bytes[i++] = val;
- case 1: bytes[i++] = val;
+ case 8: data[i++] = val;
+ case 7: data[i++] = val;
+ case 6: data[i++] = val;
+ case 5: data[i++] = val;
+ case 4: data[i++] = val;
+ case 3: data[i++] = val;
+ case 2: data[i++] = val;
+ case 1: data[i++] = val;
default: break;
}
} else {
for (int i = 0; i < size; ++i) {
- bytes[i] = val;
+ data[i] = val;
}
}
};
diff --git a/src/pso/TSocket.cpp b/src/pso/TSocket.cpp
index f9e954d..0a0166e 100644
--- a/src/pso/TSocket.cpp
+++ b/src/pso/TSocket.cpp
@@ -38,7 +38,7 @@ int TSocket::resolve_domain(char *domain) {
}
TSocket::TSocket(TObject *parent) : TObject(parent) {
- m_dst_addr.addr_bytes.fast_fill_with(0);
+ m_dst_addr.addr_bytes.fast_fill_with<u8>(0);
m_dst_port = 0;
m_src_port = 0;
m_sock_fd = -1;