summaryrefslogtreecommitdiffstats
path: root/src/libixion/cell_access.cpp
blob: f3c038e3aadded6162ef15fb47253ba799882adc (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
/* -*- 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/.
 */

#include <ixion/cell_access.hpp>
#include <ixion/global.hpp>
#include <ixion/model_context.hpp>
#include <ixion/formula_result.hpp>
#include <ixion/exceptions.hpp>

#include "model_context_impl.hpp"
#include "workbook.hpp"
#include "utils.hpp"
#include "model_types.hpp"

namespace ixion {

struct cell_access::impl
{
    const model_context& cxt;
    column_store_t::const_position_type pos;
    impl(const model_context& _cxt) : cxt(_cxt) {}
};

cell_access::cell_access(const model_context& cxt, const abs_address_t& addr) :
    mp_impl(std::make_unique<impl>(cxt))
{
    mp_impl->pos = cxt.mp_impl->get_cell_position(addr);
}

cell_access::cell_access(cell_access&& other) :
    mp_impl(std::move(other.mp_impl))
{
    other.mp_impl = std::make_unique<impl>(mp_impl->cxt);
}

cell_access& cell_access::operator= (cell_access&& other)
{
    mp_impl = std::move(other.mp_impl);
    other.mp_impl = std::make_unique<impl>(mp_impl->cxt);
    return *this;
}

cell_access::~cell_access() {}

celltype_t cell_access::get_type() const
{
    return detail::to_celltype(mp_impl->pos.first->type);
}

cell_value_t cell_access::get_value_type() const
{
    return detail::to_cell_value_type(
        mp_impl->pos, mp_impl->cxt.get_formula_result_wait_policy());
}

const formula_cell* cell_access::get_formula_cell() const
{
    if (mp_impl->pos.first->type != element_type_formula)
        return nullptr;

    return formula_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second);
}

formula_result cell_access::get_formula_result() const
{
    const formula_cell* fc = get_formula_cell();
    if (!fc)
        throw general_error("cell is not a formula cell.");

    return fc->get_result_cache(mp_impl->cxt.get_formula_result_wait_policy());
}

double cell_access::get_numeric_value() const
{
    switch (mp_impl->pos.first->type)
    {
        case element_type_numeric:
            return numeric_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second);
        case element_type_boolean:
        {
            auto it = boolean_element_block::cbegin(*mp_impl->pos.first->data);
            std::advance(it, mp_impl->pos.second);
            return *it ? 1.0 : 0.0;
        }
        case element_type_formula:
        {
            const formula_cell* p = formula_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second);
            return p->get_value(mp_impl->cxt.get_formula_result_wait_policy());
        }
        default:
            ;
    }
    return 0.0;
}

bool cell_access::get_boolean_value() const
{
    switch (mp_impl->pos.first->type)
    {
        case element_type_numeric:
            return numeric_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second) != 0.0 ? true : false;
        case element_type_boolean:
        {
            auto it = boolean_element_block::cbegin(*mp_impl->pos.first->data);
            std::advance(it, mp_impl->pos.second);
            return *it;
        }
        case element_type_formula:
        {
            const formula_cell* p = formula_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second);
            return p->get_value(mp_impl->cxt.get_formula_result_wait_policy()) == 0.0 ? false : true;
        }
        default:
            ;
    }
    return false;
}

std::string_view cell_access::get_string_value() const
{
    switch (mp_impl->pos.first->type)
    {
        case element_type_string:
        {
            string_id_t sid = string_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second);
            const std::string* p = mp_impl->cxt.get_string(sid);
            return p ? *p : std::string_view{};
        }
        case element_type_formula:
        {
            const formula_cell* p = formula_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second);
            return p->get_string(mp_impl->cxt.get_formula_result_wait_policy());
        }
        case element_type_empty:
            return detail::empty_string;
        default:
            ;
    }

    return std::string_view{};
}

string_id_t cell_access::get_string_identifier() const
{
    switch (mp_impl->pos.first->type)
    {
        case element_type_string:
            return string_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second);
        default:
            ;
    }
    return empty_string_id;
}

formula_error_t cell_access::get_error_value() const
{
    if (mp_impl->pos.first->type != element_type_formula)
        // this is not a formula cell.
        return formula_error_t::no_error;

    const formula_cell* fc = formula_element_block::at(*mp_impl->pos.first->data, mp_impl->pos.second);
    formula_result res = fc->get_result_cache(mp_impl->cxt.get_formula_result_wait_policy());
    if (res.get_type() != formula_result::result_type::error)
        // this formula cell doesn't have an error result.
        return formula_error_t::no_error;

    return res.get_error();
}

}

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