aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/update/arithmeticvalueupdate.h
blob: 52744a30804da21f5d1c31b0a9e345c2b32e1a8e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class document::ArithmeticValueUpdate
 * @ingroup document
 *
 * @brief Represent an update that specifies an arithmetic operation that is to
 *        be applied to the weight of a field value.
 */
#pragma once

#include "valueupdate.h"

namespace document {

class ArithmeticValueUpdate final : public ValueUpdate {
public:
    /** Declare all types of arithmetic value updates. */
    enum Operator {
        Add = 0, // Add the operand to the field value.
        Div,     // Divide the field value with the operand.
        Mul,     // Multiply the field value with the operand.
        Sub,     // Subtract the operand from the field value.
        MAX_NUM_OPERATORS
    };

private:
    Operator _operator; // The operator of the arithmetic operation.
    double   _operand; // The operand of the arithmetic operation.

    // Used by ValueUpdate's static factory function
    // Private because it generates an invalid object.
    friend class ValueUpdate;
    ArithmeticValueUpdate()
        : ValueUpdate(Arithmetic),
          _operator(MAX_NUM_OPERATORS),
          _operand(0.0) {}

    ACCEPT_UPDATE_VISITOR;
public:
    using UP = std::unique_ptr<ArithmeticValueUpdate>;

    /**
     * The default constructor requires initial values for all member variables.
     *
     * @param opt The operator of this arithmetic update.
     * @param opn The operand for the operation.
     */
    ArithmeticValueUpdate(Operator opt, double opn)
        : ValueUpdate(Arithmetic),
          _operator(opt),
          _operand(opn) {}

    ArithmeticValueUpdate(const ArithmeticValueUpdate& update) = delete;
    ArithmeticValueUpdate &operator=(const ArithmeticValueUpdate &rhs) = delete;

    bool operator==(const ValueUpdate& other) const override;

    Operator getOperator() const { return _operator; }
    double getOperand() const { return _operand; }

    /**
     * Apply the contained operation on the given double.
     *
     * @param value The value to modify.
     * @return The modified value.
     */
    double applyTo(double value) const;

    /**
     * Apply the contained operation on the given string.
     *
     * @param value The value to modify.
     * @return The modified value.
     */
    std::string applyTo(const std::string& value) const;

    /**
     * Apply the contained operation on the given long.
     *
     * @param value The value to modify.
     * @return The modified value.
     */
    long applyTo(int64_t value) const;

    void checkCompatibility(const Field& field) const override;
    bool applyTo(FieldValue& value) const override;
    void printXml(XmlOutputStream& xos) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void deserialize(const DocumentTypeRepo& repo, const DataType& type, nbostream & buffer) override;
};

} // document