aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/data/memory.h
blob: c348b911a4bef09535c96a0b961687b62402c686 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/stllike/string.h>
#include <ostream>

namespace vespalib {

/**
 * Simple wrapper referencing a read-only region of memory.
 **/
struct Memory
{
    const char *data;
    size_t      size;

    Memory() noexcept : data(nullptr), size(0) {}
    Memory(const char *d, size_t s) noexcept : data(d), size(s) {}
    Memory(const char *str) noexcept : data(str), size(strlen(str)) {}
    Memory(const std::string &str) noexcept
        : data(str.data()), size(str.size()) {}
    Memory(const vespalib::string &str) noexcept
        : data(str.data()), size(str.size()) {}
    Memory(vespalib::stringref str_ref) noexcept
        : data(str_ref.data()), size(str_ref.size()) {}
    vespalib::string make_string() const;
    vespalib::stringref make_stringref() const { return stringref(data, size); }
    bool operator == (const Memory &rhs) const noexcept {
        if (size != rhs.size) {
            return false;
        }
        if ((size == 0) || (data == rhs.data)) {
            return true;
        }
        return (memcmp(data, rhs.data, size) == 0);
    }
};

std::ostream &operator<<(std::ostream &os, const Memory &memory);

} // namespace vespalib