aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/fieldvalue/literalfieldvalue.cpp
blob: 84de2e3b16c2c4aa7b7f2227a8f5708daf29d987 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "literalfieldvalue.hpp"
#include <vespa/document/util/stringutil.h>
#include <vespa/vespalib/util/xmlstream.h>

using namespace vespalib::xml;

namespace document {

LiteralFieldValueB::LiteralFieldValueB(Type type) :
    FieldValue(type),
    _value(),
    _backing()
{
    _value = _backing;
}

LiteralFieldValueB::~LiteralFieldValueB() = default;

LiteralFieldValueB::LiteralFieldValueB(const LiteralFieldValueB& other)
    : FieldValue(other),
      _value(),
      _backing(other.getValueRef())
{
    _value = _backing;
}

LiteralFieldValueB::LiteralFieldValueB(Type type, const stringref & value)
    : FieldValue(type),
      _value(),
      _backing(value)
{
    _value = _backing;
}

LiteralFieldValueB &
LiteralFieldValueB::operator=(const LiteralFieldValueB& other)
{
    FieldValue::operator=(other);
    _backing = other.getValueRef();
    _value = _backing;
    return *this;
}

FieldValue&
LiteralFieldValueB::assign(const FieldValue& value)
{
    if (value.getDataType() == getDataType()) {
        return operator=(static_cast<const LiteralFieldValueB&>(value));
    }
    return FieldValue::assign(value);
}

int
LiteralFieldValueB::compare(const FieldValue& other) const
{
    if (*getDataType() == *other.getDataType()) {
        const LiteralFieldValueB& sval(static_cast<const LiteralFieldValueB&>(other));
        return getValueRef().compare(sval.getValueRef());
    }
    return (getDataType()->getId() - other.getDataType()->getId());
}

int
LiteralFieldValueB::fastCompare(const FieldValue& other) const
{
    const LiteralFieldValueB& sval(static_cast<const LiteralFieldValueB&>(other));
    return getValueRef().compare(sval.getValueRef());
}

void
LiteralFieldValueB::printXml(XmlOutputStream& out) const
{
    out << XmlContentWrapper(_value.data(), _value.size());
}

void
LiteralFieldValueB::
print(std::ostream& out, bool, const std::string&) const
{
    vespalib::string escaped;
    out << StringUtil::escape(getValue(), escaped);
}

FieldValue&
LiteralFieldValueB::operator=(vespalib::stringref value)
{
    setValue(value);
    return *this;
}

vespalib::string
LiteralFieldValueB::getAsString() const
{
    return getValue();
}

std::pair<const char*, size_t>
LiteralFieldValueB::getAsRaw() const
{
    return std::make_pair(_value.data(), _value.size());
}

void
LiteralFieldValueB::syncBacking() const
{
    _backing = _value;
    _value = _backing;
}

template class LiteralFieldValue<RawFieldValue, DataType::T_RAW>;
template class LiteralFieldValue<StringFieldValue, DataType::T_STRING>;

}  // namespace document