aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/fieldvalue/numericfieldvalue.h
blob: b65475e6bb53640b26b4d817a34ca088bca00381 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class document::NumericFieldValue
 * \ingroup fieldvalue
 *
 * \brief Templated parent class for field values of numbers.
 *
 * To prevent code duplication, numeric values are implemented using this
 * template.
 */
#pragma once

#include "fieldvalue.h"
#include <vespa/vespalib/stllike/hash_fun.h>

namespace document {

class NumericFieldValueBase : public FieldValue
{
public:
    void printXml(XmlOutputStream& out) const override;
protected:
    NumericFieldValueBase(Type type) : FieldValue(type) {}
};

template<typename Number>
class NumericFieldValue : public NumericFieldValueBase {
protected:
    explicit NumericFieldValue(Type type, Number value=0) : NumericFieldValueBase(type), _value(value) { }
    Number _value;
public:
    using value_type = Number;

    value_type getValue() const { return _value; }
    void setValue(Number newValue) { _value = newValue; }

    FieldValue& assign(const FieldValue&) override ;
    int compare(const FieldValue& other) const override;
    int fastCompare(const FieldValue& other) const override final;

    FieldValue& operator=(vespalib::stringref) override;
    size_t hash() const override final { return vespalib::hash<Number>()(_value); }

    char getAsByte() const override;
    int32_t getAsInt() const override;
    int64_t getAsLong() const override;
    float getAsFloat() const override;
    double getAsDouble() const override;
    vespalib::string getAsString() const override;

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
};

extern template class NumericFieldValue<float>;
extern template class NumericFieldValue<double>;
extern template class NumericFieldValue<int8_t>;
extern template class NumericFieldValue<int16_t>;
extern template class NumericFieldValue<int32_t>;
extern template class NumericFieldValue<int64_t>;

} // document