use new typing and functions for more "safety" (#332)

This commit is contained in:
bjcscat 2022-12-03 13:49:28 -06:00 committed by GitHub
parent 9ddc32193f
commit 1037e8a9a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 79 additions and 50 deletions

View file

@ -8,8 +8,8 @@ using namespace std;
using namespace vips;
char *Blur(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
bool sharp = MAP_GET(Arguments, "sharp", bool);
ArgumentMap Arguments, size_t *DataSize) {
bool sharp = GetArgument<bool>(Arguments, "sharp");
VOption *options = VImage::option()->set("access", "sequential");
VImage in =

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Blur(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Blur(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -8,11 +8,11 @@ using namespace std;
using namespace vips;
char *Caption(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
string caption = MAP_GET(Arguments, "caption", string);
string font = MAP_GET(Arguments, "font", string);
string basePath = MAP_GET(Arguments, "basePath", string);
string caption = GetArgument<string>(Arguments, "caption");
string font = GetArgument<string>(Arguments, "font");
string basePath = GetArgument<string>(Arguments, "basePath");
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Caption(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Caption(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -8,12 +8,12 @@ using namespace std;
using namespace vips;
char *CaptionTwo(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
bool top = MAP_GET(Arguments, "top", bool);
string caption = MAP_GET(Arguments, "caption", string);
string font = MAP_GET(Arguments, "font", string);
string basePath = MAP_GET(Arguments, "basePath", string);
bool top = GetArgument<bool>(Arguments, "top");
string caption = GetArgument<string>(Arguments, "caption");
string font = GetArgument<string>(Arguments, "font");
string basePath = GetArgument<string>(Arguments, "basePath");
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* CaptionTwo(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* CaptionTwo(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -11,7 +11,7 @@ using namespace std;
using namespace Magick;
char *Circle(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
Blob blob;

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Circle(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Circle(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -11,9 +11,9 @@ VImage sepia = VImage::new_matrixv(3, 3, 0.3588, 0.7044, 0.1368, 0.2990, 0.5870,
0.1140, 0.2392, 0.4696, 0.0912);
char *Colors(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
string color = MAP_GET(Arguments, "color", string);
string color = GetArgument<string>(Arguments, "color");
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Colors(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Colors(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -2,8 +2,34 @@
#include <variant>
#include <string>
#include <map>
#include <unordered_map>
using std::string;
using std::variant;
using std::map;
typedef variant<string, float, bool, int> ArgumentVariant;
typedef map<string, ArgumentVariant> ArgumentMap;
template <typename T>
T GetArgument(ArgumentMap map, string key) {
try {
return std::get<T>(map.at(key));
} catch (std::bad_variant_access&) {
throw "Invalid requested type from variant.";
}
}
template <typename T>
T GetArgumentWithFallback(ArgumentMap map, string key, T fallback) {
try {
return std::get<T>(map.at(key));
} catch (...) { // this is, not great...
return fallback;
}
}
#define MAP_HAS(ARRAY, KEY) (ARRAY.count(KEY) > 0)
#define MAP_GET(ARRAY, KEY, TYPE) (MAP_HAS(ARRAY, KEY) ? get<TYPE>(ARRAY.at(KEY)) : NULL) // C++ has forced my hand
#define MAP_GET_FALLBACK(ARRAY, KEY, TYPE, FALLBACK) (MAP_HAS(ARRAY, KEY) ? get<TYPE>(ARRAY.at(KEY)) : FALLBACK)

View file

@ -7,7 +7,7 @@ using namespace std;
using namespace vips;
char *Crop(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Crop(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Crop(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -6,7 +6,7 @@ using namespace std;
using namespace vips;
char *Deepfry(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Deepfry(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Deepfry(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -12,10 +12,10 @@ using namespace std;
using namespace Magick;
char *Explode(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
int amount = MAP_GET(Arguments, "amount", int);
int delay = MAP_GET_FALLBACK(Arguments, "delay", int, 0);
int amount = GetArgument<int>(Arguments, "amount");
int delay = GetArgumentWithFallback<int>(Arguments, "delay", 0);
Blob blob;

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Explode(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Explode(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -8,10 +8,10 @@ using namespace std;
using namespace vips;
char *Flag(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
string overlay = MAP_GET(Arguments, "overlay", string);
string basePath = MAP_GET(Arguments, "basePath", string);
string overlay = GetArgument<string>(Arguments, "overlay");
string basePath = GetArgument<string>(Arguments, "basePath");
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Flag(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Flag(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -8,9 +8,9 @@ using namespace std;
using namespace vips;
char *Flip(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
bool flop = MAP_GET(Arguments, "flop", bool);
bool flop = GetArgument<bool>(Arguments, "flop");
VImage in = VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif"

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Flip(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Flip(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -51,7 +51,7 @@
using namespace std;
std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize)> FunctionMap = {
std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize)> FunctionMap = {
{"blur", &Blur},
{"caption", &Caption},
{"captionTwo", &CaptionTwo},
@ -117,7 +117,7 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) {
Napi::Array properties = obj.GetPropertyNames();
std::map<string, ARG_TYPES> Arguments;
ArgumentMap Arguments;
for (unsigned int i = 0; i < properties.Length(); i++) {
string property = properties.Get(uint32_t(i)).As<Napi::String>().Utf8Value();
@ -138,6 +138,9 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) {
} else {
Arguments[property] = num.FloatValue();
}
} else {
throw "Unimplemented value type passed to image native.";
//Arguments[property] = val;
}
}

View file

@ -7,9 +7,9 @@ using namespace std;
using namespace vips;
char *Uncaption(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
float tolerance = MAP_GET_FALLBACK(Arguments, "tolerance", float, 0.5);
float tolerance = GetArgumentWithFallback<float>(Arguments, "tolerance", 0.5);
VOption *options = VImage::option();

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Uncaption(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Uncaption(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -7,23 +7,23 @@ using namespace std;
using namespace vips;
char *Watermark(string type, char *BufferData, size_t BufferLength,
map<string, ARG_TYPES> Arguments, size_t *DataSize) {
ArgumentMap Arguments, size_t *DataSize) {
string water = MAP_GET(Arguments, "water", string);
int gravity = MAP_GET(Arguments, "gravity", int);
string water = GetArgument<string>(Arguments, "water");
int gravity = GetArgument<int>(Arguments, "gravity");
bool resize = MAP_GET_FALLBACK(Arguments, "resize", bool, false);
;
float yscale = MAP_GET_FALLBACK(Arguments, "yscale", float, false);
bool resize = GetArgumentWithFallback<bool>(Arguments, "resize", false);
bool append = MAP_GET_FALLBACK(Arguments, "append", bool, false);
float yscale = GetArgumentWithFallback<float>(Arguments, "yscale", false);
bool alpha = MAP_GET_FALLBACK(Arguments, "alpha", bool, false);
bool flip = MAP_GET_FALLBACK(Arguments, "flip", bool, false);
bool append = GetArgumentWithFallback<bool>(Arguments, "append", false);
bool alpha = GetArgumentWithFallback<bool>(Arguments, "alpha", false);
bool flip = GetArgumentWithFallback<bool>(Arguments, "flip", false);
bool mc = MAP_HAS(Arguments, "mc");
string basePath = MAP_GET(Arguments, "basePath", string);
string basePath = GetArgument<string>(Arguments, "basePath");
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -6,4 +6,4 @@
using std::map;
using std::string;
char* Watermark(string type, char* BufferData, size_t BufferLength, map<string, ARG_TYPES> Arguments, size_t* DataSize);
char* Watermark(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);