1/* sane - Scanner Access Now Easy.
2
3   Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4
5   This file is part of the SANE package.
6
7   This program is free software; you can redistribute it and/or
8   modify it under the terms of the GNU General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#ifndef BACKEND_GENESYS_REGISTER_CACHE_H
22#define BACKEND_GENESYS_REGISTER_CACHE_H
23
24#include "register.h"
25
26namespace genesys {
27
28template<class Value>
29class RegisterCache
30{
31public:
32    void update(std::uint16_t address, Value value)
33    {
34        if (regs_.has_reg(address)) {
35            regs_.set(address, value);
36        } else {
37            regs_.init_reg(address, value);
38        }
39    }
40
41    void update(const Genesys_Register_Set& regs)
42    {
43        for (const auto& reg : regs) {
44            update(reg.address, reg.value);
45        }
46    }
47
48    Value get(std::uint16_t address) const
49    {
50        return regs_.get(address);
51    }
52
53private:
54    RegisterContainer<Value> regs_;
55
56    template<class V>
57    friend std::ostream& operator<<(std::ostream& out, const RegisterCache<V>& cache);
58};
59
60template<class Value>
61std::ostream& operator<<(std::ostream& out, const RegisterCache<Value>& cache)
62{
63    out << cache.regs_;
64    return out;
65}
66
67} // namespace genesys
68
69#endif // BACKEND_GENESYS_LINE_BUFFER_H
70