newtx/sdk/sane_opt_json/base_opt.h

91 lines
3.2 KiB
C++

#pragma once
// SANE-Option
//
// created on 2022-10-24
//
#include <string>
#include <map>
#include <base/utils.h> // for refer
#include <sane/sane_ex.h>
class gb_json;
class sane_opt_provider : public refer
{
bool is_in_another_module_ = false;
std::string opt_jsn_txt_;
std::string where_;
protected:
std::map<std::string, sane_opt_provider*> following_;
public:
sane_opt_provider();
static std::string sane_value_2_readable_text(SANE_Value_Type type, void* value); // convert to readable text. e.g. bool to "true" or "false", ...
static std::string sane_value_2_readable_text(const char* type, void* value); // convert to readable text. e.g. bool to "true" or "false", ...
static std::string sane_value_from_readable_text(SANE_Value_Type type, const char* text); // convert from readable text. e.g. bool to "true" or "false", ...
static std::string sane_value_from_readable_text(const char* type, const char* text); // convert from readable text. e.g. bool to "true" or "false", ...
static bool set_opt_value(gb_json* opt, void* value, const char* key = "cur");
static bool is_opt_value_equal(gb_json* opt, void* v1, void* v2);
static std::string option_value(gb_json* jsn, bool def_val, SANE_Value_Type* type = nullptr);
static std::string get_all_values(gb_json* opt, SANE_Constraint_Type* constraint, SANE_Value_Type* type);
protected:
virtual ~sane_opt_provider();
bool set_opt_json_text(char* txt);
void set_where(const char* where);
public:
const char* get_opt_json(void); // if no content, return "" plz.
const char* from(void); // if no content, return "" plz.
bool is_in_another_module(void) { return is_in_another_module_; }
void set_following_provider(const char* name, sane_opt_provider* following); // when option has provided by more than one
sane_opt_provider* get_following(const char* name); // caller should ->release returned value
public:
// return malloc(), real data size stored in parameter 'size'. invoker should free() the returned value
virtual char* get_value(const char* name, void* value, size_t* size, int* err = nullptr);
virtual int set_value(const char* name/*nullptr for all options*/, void* val/*nullptr for restore*/);
virtual void enable(const char* name, bool able);
};
#define ADD_THIS_JSON() \
{ \
std::string t(""); \
for(auto& v: device_opt_json) \
t += v; \
set_opt_json_text(&t[0]); \
}
// change given options 'cur' value and add my JSON
// vals: std::map<std::string, void*>. void* is bool*, int*, double* or char*
#define ADD_THIS_JSON_WITH_VALUES(vals) \
{ \
std::string t(""); \
for(auto& v: device_opt_json) \
t += v; \
gb_json *jsn = new gb_json(); \
if(jsn->attatch(&t[0])) \
{ \
for(auto& v: vals) \
{ \
gb_json *child = nullptr; \
jsn->get_value(v.first.c_str(), child); \
if(child) \
{ \
sane_opt_provider::set_opt_value(child, v.second); \
child->release(); \
} \
} \
t = jsn->to_string(); \
} \
jsn->release(); \
set_opt_json_text(&t[0]); \
}