summaryrefslogtreecommitdiffstats
path: root/src/libixion/named_expressions_iterator.cpp
blob: 77ce457eeb09cb628489541baeed881ff8db3619 (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
/* -*- 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/named_expressions_iterator.hpp"
#include "ixion/global.hpp"
#include "model_types.hpp"
#include "model_context_impl.hpp"

namespace ixion {

struct named_expressions_iterator::impl
{
    const detail::named_expressions_t* named_exps;
    detail::named_expressions_t::const_iterator it;
    detail::named_expressions_t::const_iterator it_end;

    impl() : named_exps(nullptr) {}

    impl(const model_context& cxt, sheet_t scope) :
        named_exps(scope >=0 ? &cxt.mp_impl->get_named_expressions(scope) : &cxt.mp_impl->get_named_expressions()),
        it(named_exps->cbegin()),
        it_end(named_exps->cend()) {}

    impl(const impl& other) :
        named_exps(other.named_exps),
        it(other.it),
        it_end(other.it_end) {}
};

named_expressions_iterator::named_expressions_iterator() :
    mp_impl(std::make_unique<impl>()) {}

named_expressions_iterator::named_expressions_iterator(const model_context& cxt, sheet_t scope) :
    mp_impl(std::make_unique<impl>(cxt, scope)) {}

named_expressions_iterator::named_expressions_iterator(const named_expressions_iterator& other) :
    mp_impl(std::make_unique<impl>(*other.mp_impl)) {}

named_expressions_iterator::named_expressions_iterator(named_expressions_iterator&& other) :
    mp_impl(std::move(other.mp_impl)) {}

named_expressions_iterator::~named_expressions_iterator() = default;

size_t named_expressions_iterator::size() const
{
    if (!mp_impl->named_exps)
        return 0;

    return mp_impl->named_exps->size();
}

bool named_expressions_iterator::has() const
{
    if (!mp_impl->named_exps)
        return false;

    return mp_impl->it != mp_impl->it_end;
}

void named_expressions_iterator::next()
{
    ++mp_impl->it;
}

named_expressions_iterator::named_expression named_expressions_iterator::get() const
{
    named_expression ret;
    ret.name = &mp_impl->it->first;
    ret.expression = &mp_impl->it->second;
    return ret;
}

named_expressions_iterator& named_expressions_iterator::operator= (const named_expressions_iterator& other)
{
    mp_impl = std::make_unique<impl>(*other.mp_impl);
    return *this;
}

}

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