-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsane_option.h
99 lines (82 loc) · 3.25 KB
/
sane_option.h
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
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef LORGNETTE_SANE_OPTION_H_
#define LORGNETTE_SANE_OPTION_H_
#include <optional>
#include <string>
#include <vector>
#include <lorgnette/proto_bindings/lorgnette_service.pb.h>
#include <sane/sane.h>
#include "lorgnette/sane_constraint.h"
namespace lorgnette {
// Represents a SANE_Option_Descriptor and its current value.
class SaneOption {
public:
SaneOption(const SANE_Option_Descriptor& opt, int index);
bool Set(bool b);
bool Set(double d);
bool Set(const std::vector<double>& d);
bool Set(int i);
bool Set(const std::vector<int>& i);
bool Set(const std::string& s);
bool Set(const char* s);
bool Set(const ScannerOption& value);
template <typename T>
std::optional<T> Get() const = delete;
template <>
std::optional<bool> Get() const;
template <>
std::optional<int> Get() const;
template <>
std::optional<std::vector<int>> Get() const;
template <>
std::optional<double> Get() const;
template <>
std::optional<std::vector<double>> Get() const;
template <>
std::optional<std::string> Get() const;
// This returns a pointer to the internal storage. Care must be taken that the
// pointer does not outlive the SaneOption.
void* GetPointer();
int GetIndex() const;
std::string GetName() const;
std::string DisplayValue() const;
SANE_Value_Type GetType() const;
size_t GetSize() const;
std::optional<SaneConstraint> GetConstraint() const;
bool IsActive() const;
SANE_Action GetAction() const;
// Wrapper functions to process the embedded constraint.
std::optional<std::vector<std::string>> GetValidStringValues() const;
std::optional<std::vector<uint32_t>> GetValidIntValues() const;
std::optional<OptionRange> GetValidRange() const;
std::optional<ScannerOption> ToScannerOption() const;
private:
void ParseCapabilities(SANE_Int cap);
void ReserveValueSize(const SANE_Option_Descriptor& opt);
std::string name_;
std::string title_;
std::string description_;
int index_;
SANE_Value_Type type_; // The value type used by the backend for this option.
SANE_Unit unit_; // The unit type used by the backend for this option.
bool detectable_; // Capabilities contains CAP_SOFT_DETECT.
bool sw_settable_; // SANE_OPTION_IS_SETTABLE is true for capabilities.
bool hw_settable_; // Capabilities contains CAP_HARD_SELECT.
bool auto_settable_; // Capabilities contains CAP_AUTOMATIC.
bool emulated_; // Capabilities contains CAP_EMULATED.
bool active_; // SANE_OPTION_IS_ACTIVE is true for capabilities.
// Inactive options do not contain a valid value.
bool advanced_; // Capabilities contains CAP_ADVANCED.
SANE_Action action_; // The action needed to set the current value with
// sane_control_option().
std::optional<SaneConstraint> constraint_;
// Only one of these will be set, depending on type_.
std::optional<std::vector<SANE_Int>> int_data_;
std::optional<std::vector<SANE_Fixed>> fixed_data_;
SANE_Bool bool_data_;
std::optional<std::vector<SANE_Char>> string_data_;
};
} // namespace lorgnette
#endif // LORGNETTE_SANE_OPTION_H_