p2p/stingray_sdk/plugin_foundation/id_string.inl
Lucas Schwiderski 2c9ce46dd2
chore: Rework project structure
There likely won't be much need for multiple separate crates.
2023-05-26 23:42:01 +02:00

91 lines
1.9 KiB
C++

#include <stdint.h>
#include "hash_function.h"
#include "array.h"
namespace stingray_plugin_foundation {
inline IdString64::IdString64() : _id(0)
{
}
inline IdString64::IdString64(uint64_t id) : _id(id)
{
}
inline IdString64::IdString64(const char *s)
{
_id = murmur_hash_64(s, (unsigned)strlen(s), 0);
}
inline IdString64::IdString64(unsigned len, const char *s)
{
_id = murmur_hash_64(s, len, 0);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
inline IdString64::IdString64(const char *s, uint64_t id)
{
_id = id;
#if defined(_DEBUG) && defined(XASSERT)
XASSERT(_id == murmur_hash_64(s, (unsigned)strlen(s), 0), "Bad static idstring `%s`", s);
#endif
}
#pragma GCC diagnostic pop
inline const char *IdString64::to_id_hex() const
{
static char buffer[200];
static char *p = &buffer[0];
if (p + HEX_BUFFER_SIZE + 10 > &buffer[200])
p = &buffer[0];
char hex[HEX_BUFFER_SIZE];
to_hex(hex);
sprintf(p, "#ID[%s]", hex);
char *s = p;
p += strlen(p) + 1;
return s;
}
inline IdString32::IdString32() : _id(0) {}
inline IdString32::IdString32(unsigned id) : _id(id) {}
inline IdString32::IdString32(const char *s)
{
uint64_t id64 = murmur_hash_64(s, (unsigned)strlen(s), 0);
_id = (id64 >> 32);
}
inline IdString32::IdString32(unsigned len, const char *s)
{
uint64_t id64 = murmur_hash_64(s, len, 0);
_id = (id64 >> 32);
}
inline const char *IdString32::to_id_hex() const
{
static char buffer[200];
static char *p = &buffer[0];
if (p + HEX_BUFFER_SIZE + 10 > &buffer[200])
p = &buffer[0];
char hex[HEX_BUFFER_SIZE];
to_hex(hex);
sprintf(p, "#ID[%s]", hex);
char *s = p;
p += strlen(p) + 1;
return s;
}
template <>
template <class STREAM> void Array<IdString64>::serialize(STREAM &stream) {
raw_array_serialize(stream, *this);
}
template <>
template <class STREAM> void Array<IdString32>::serialize(STREAM &stream) {
raw_array_serialize(stream, *this);
}
}