aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsa/unaligned.h
blob: e4fac3287be7daa88fa6a3bd788d995610a66ce2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <cstring>

namespace fsa {

template <typename T>
class Unaligned {
private:
    char _data[sizeof(T)];

public:
    Unaligned() = delete;
    Unaligned(const Unaligned &) = delete;
    Unaligned(Unaligned &&) = delete;

    Unaligned &operator=(const Unaligned &) = default;
    Unaligned &operator=(Unaligned &&) = default;

    static_assert(std::is_trivial_v<T>);
    static_assert(alignof(T) > 1, "value is always aligned");

    constexpr static Unaligned &at(void *p) noexcept {
        return *reinterpret_cast<Unaligned*>(p);
    }
    constexpr static const Unaligned &at(const void *p) noexcept {
        return *reinterpret_cast<const Unaligned*>(p);
    }

    constexpr static Unaligned *ptr(void *p) noexcept {
        return reinterpret_cast<Unaligned*>(p);
    }
    constexpr static const Unaligned *ptr(const void *p) noexcept {
        return reinterpret_cast<const Unaligned*>(p);
    }

    T read() const noexcept {
        T value;
        static_assert(sizeof(_data) == sizeof(value));
        memcpy(&value, _data, sizeof(value));
        return value;
    }
    void write(const T &value) noexcept {
        static_assert(sizeof(_data) == sizeof(value));
        memcpy(_data, &value, sizeof(value));
    }
    operator T () const noexcept { return read(); }
    Unaligned &operator=(const T &value) noexcept {
        write(value);
        return *this;
    }
};

}