summaryrefslogtreecommitdiffstats
path: root/include/ixion/formula_tokens.hpp
blob: d843dbdc6e3f3d1cb0c6728a14e9838ba082cd55 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_FORMULA_TOKENS_HPP
#define INCLUDED_FORMULA_TOKENS_HPP

#include "address.hpp"
#include "table.hpp"
#include "formula_opcode.hpp"
#include "formula_function_opcode.hpp"
#include "formula_tokens_fwd.hpp"

#include <string>
#include <variant>

namespace ixion {

/**
 * Get a printable name for a formula opcode.  The printable name is to be
 * used only for informational purposes.
 *
 * @param oc formula opcode
 *
 * @return printable name for a formula opcode.
 */
IXION_DLLPUBLIC std::string_view get_opcode_name(fopcode_t oc);

/**
 * Get the string representation of a simple formula opcode.  This function
 * will return a non-empty string only for operator opcodes.
 *
 * @param oc formula opcode
 *
 * @return string representation of a formula opcode.
 */
IXION_DLLPUBLIC std::string_view get_formula_opcode_string(fopcode_t oc);

/**
 * Represents a single formula token.
 */
struct IXION_DLLPUBLIC formula_token final
{
    using value_type = std::variant<
        address_t, range_t, table_t, formula_function_t,
        double, string_id_t, std::string>;

    /**
     * Opcode that specifies the type of token.  The value of this data member
     * should <i>not</i> be modified after construction.
     */
    const fopcode_t opcode;

    /**
     * Value stored in the token. The type of this value varies depending on the
     * token opcode value.
     */
    value_type value;

    formula_token() = delete;

    /**
     * Constructor for opcode-only token.
     *
     * @param op formula opcode.
     */
    formula_token(fopcode_t op);

    /**
     * Constructor for a single-cell reference token.  The opcode will be
     * implicitly set to fop_single_ref.
     *
     * @param addr single-cell reference.
     */
    formula_token(const address_t& addr);

    /**
     * Constructor for a range reference token.  The opcode will be
     * implicitly set to fop_range_ref.
     *
     * @param range range reference.
     */
    formula_token(const range_t& range);

    /**
     * Constructor for a table reference token.  The opcode will be implicitly
     * set to fop_table_ref.
     *
     * @param table table reference.
     */
    formula_token(const table_t& table);

    /**
     * Constructor for a formula function token.  The opcode will be implicitly
     * set to fop_function.
     *
     * @param func function name enum value.
     */
    formula_token(formula_function_t func);

    /**
     * Constructor for a numeric value token.  The opcode will be implicitly set
     * to fop_value.
     *
     * @param v numeric value to be stored in the token.
     */
    formula_token(double v);

    /**
     * Constructor for a string value token.  The opcode will be implicitly
     * set to fop_string.
     *
     * @param sid string ID to be stored in the token.
     */
    formula_token(string_id_t sid);

    /**
     * Constructor for a named-expression token.  The opcode will be implicitly
     * set to fop_named_expression.
     *
     * @param name named expression to be stored in the token.
     */
    formula_token(std::string name);

    /**
     * Copy constructor.
     */
    formula_token(const formula_token& r);

    /**
     * Move constructor.
     *
     * @note This will be the same as the copy constructor if the stored value
     *       is not movable.
     */
    formula_token(formula_token&& r);

    ~formula_token();

    bool operator== (const formula_token& r) const;
    bool operator!= (const formula_token& r) const;
};

/**
 * Storage for a series of formula tokens.
 */
class IXION_DLLPUBLIC formula_tokens_store
{
    friend void intrusive_ptr_add_ref(formula_tokens_store*);
    friend void intrusive_ptr_release(formula_tokens_store*);

    struct impl;
    std::unique_ptr<impl> mp_impl;

    void add_ref();
    void release_ref();

    formula_tokens_store();

public:

    static formula_tokens_store_ptr_t create();

    ~formula_tokens_store();

    formula_tokens_store(const formula_tokens_store&) = delete;
    formula_tokens_store& operator= (const formula_tokens_store&) = delete;

    size_t get_reference_count() const;

    formula_tokens_t& get();
    const formula_tokens_t& get() const;
};

inline void intrusive_ptr_add_ref(formula_tokens_store* p)
{
    p->add_ref();
}

inline void intrusive_ptr_release(formula_tokens_store* p)
{
    p->release_ref();
}

/**
 * Represents a named expression which stores a series of formula tokens.
 */
struct IXION_DLLPUBLIC named_expression_t
{
    /**
     * Origin cell position which affects any relative references stored in
     * the named expression.
     */
    abs_address_t origin;

    /** Formula tokens. */
    formula_tokens_t tokens;

    named_expression_t();
    named_expression_t(const abs_address_t& _origin, formula_tokens_t _tokens);
    named_expression_t(const named_expression_t&) = delete;
    named_expression_t(named_expression_t&& other);
    ~named_expression_t();
};

IXION_DLLPUBLIC std::ostream& operator<< (std::ostream& os, const formula_token& ft);

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */