-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsanecpp.h
204 lines (159 loc) · 4.75 KB
/
sanecpp.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
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
/*
AirSane Imaging Daemon
Copyright (C) 2018-2023 Simul Piscator
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SANE_CPP_H
#define SANE_CPP_H
#include <iostream>
#include <map>
#include <memory>
#include <sane/sane.h>
#include <string>
#include <vector>
namespace sanecpp {
// string/number conversions using the C locale
double
strtod_c(const std::string&);
std::string
dtostr_c(double);
// sanecpp::log.rdbuf(std::cerr.rdbuf());
// will enable logging to stderr
extern std::ostream log;
struct init
{
init();
~init();
};
struct device_info
{
std::string name, vendor, model, type;
};
std::vector<device_info>
enumerate_devices(bool localonly = true);
typedef std::shared_ptr<void> device_handle;
device_handle
open(const std::string&, SANE_Status* = nullptr);
device_handle
open(const device_info&, SANE_Status* = nullptr);
std::ostream&
print(std::ostream&, SANE_Status);
std::ostream&
print(std::ostream&, SANE_Unit);
class option_set;
class option
{
private:
option(option_set*, const SANE_Option_Descriptor*, SANE_Int);
public:
option();
option& operator=(const std::string&);
option& operator=(double);
operator std::string() const;
operator double() const;
bool is_null() const;
bool is_active() const;
bool is_settable() const;
bool is_string() const;
bool is_numeric() const;
int array_size() const;
bool set_value(int index, const std::string& value);
bool set_value(const std::string& s) { return set_value(0, s); }
bool set_value(int index, double value);
bool set_value(double d) { return set_value(0, d); }
std::string value(int = 0) const;
bool set_string_value(int index, const std::string& value);
bool set_string_value(const std::string& value)
{
return set_string_value(0, value);
}
std::string string_value(int index = 0) const;
std::vector<std::string> allowed_string_values() const;
bool set_numeric_value(int index, double);
bool set_numeric_value(double value) { return set_numeric_value(0, value); }
double numeric_value(int index = 0) const;
std::vector<double> allowed_numeric_values() const;
double min() const;
double max() const;
double quant() const;
SANE_Unit unit() const;
private:
friend class option_set;
option_set* m_set;
const SANE_Option_Descriptor* m_desc;
SANE_Int m_index;
};
class option_set
{
option_set(const option_set&) = delete;
option_set& operator=(const option_set&) = delete;
public:
option_set();
explicit option_set(device_handle);
void init(device_handle);
void reload();
std::ostream& print(std::ostream&) const;
void clear() { m_options.clear(); }
bool empty() const { return m_options.empty(); }
size_t size() const { return m_options.size(); }
option& operator[](const std::string&);
const option& operator[](const std::string&) const;
typedef std::map<std::string, option>::iterator iterator;
iterator begin() { return m_options.begin(); }
iterator end() { return m_options.end(); }
typedef std::map<std::string, option>::const_iterator const_iterator;
const_iterator begin() const { return m_options.begin(); }
const_iterator end() const { return m_options.end(); }
private:
friend class option;
device_handle m_device;
std::map<std::string, option> m_options;
static option s_nulloption;
};
class session
{
public:
explicit session(const std::string& devicename);
explicit session(device_handle);
~session();
option_set& options() { return m_options; }
const option_set& options() const { return m_options; }
SANE_Status status() const { return m_status; }
const SANE_Parameters* parameters() const { return &m_parameters; }
session& start();
session& cancel();
session& read(std::vector<char>&);
const session& dump_options() const;
private:
void init();
device_handle m_device;
option_set m_options;
SANE_Status m_status;
SANE_Parameters m_parameters;
};
} // namespace sanecpp
inline std::ostream&
operator<<(std::ostream& os, SANE_Status status)
{
return sanecpp::print(os, status);
}
inline std::ostream&
operator<<(std::ostream& os, SANE_Unit unit)
{
return sanecpp::print(os, unit);
}
inline std::ostream&
operator<<(std::ostream& os, const sanecpp::option_set& opt)
{
return opt.print(os);
}
#endif // SANE_CPP_H