Merge pull request #489 from SeanCurtis-TRI/PR_callback_as_function

Update typedefs of C-style function pointers to std::function
This commit is contained in:
Syoyo Fujita 2024-06-12 04:01:37 +09:00 committed by GitHub
commit 847df8456a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -43,9 +43,11 @@
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <functional>
#include <limits> #include <limits>
#include <map> #include <map>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
// Auto-detect C++14 standard version // Auto-detect C++14 standard version
@ -1263,17 +1265,18 @@ enum SectionCheck {
/// image URIs differently, for example. See /// image URIs differently, for example. See
/// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#uris /// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#uris
/// ///
typedef bool (*URIEncodeFunction)(const std::string &in_uri, using URIEncodeFunction = std::function<bool(
const std::string &object_type, const std::string & /* in_uri */, const std::string & /* object_type */,
std::string *out_uri, void *user_data); std::string * /* out_uri */, void * /* user_data */)>;
/// ///
/// URIDecodeFunction type. Signature for custom URI decoding of external /// URIDecodeFunction type. Signature for custom URI decoding of external
/// resources such as .bin and image files. Used by tinygltf when computing /// resources such as .bin and image files. Used by tinygltf when computing
/// filenames to write resources. /// filenames to write resources.
/// ///
typedef bool (*URIDecodeFunction)(const std::string &in_uri, using URIDecodeFunction =
std::string *out_uri, void *user_data); std::function<bool(const std::string & /* in_uri */,
std::string * /* out_uri */, void * /* user_data */)>;
// Declaration of default uri decode function // Declaration of default uri decode function
bool URIDecode(const std::string &in_uri, std::string *out_uri, bool URIDecode(const std::string &in_uri, std::string *out_uri,
@ -1292,22 +1295,21 @@ struct URICallbacks {
/// ///
/// LoadImageDataFunction type. Signature for custom image loading callbacks. /// LoadImageDataFunction type. Signature for custom image loading callbacks.
/// ///
typedef bool (*LoadImageDataFunction)(Image *, const int, std::string *, using LoadImageDataFunction = std::function<bool(
std::string *, int, int, Image * /* image */, const int /* image_idx */, std::string * /* err */,
const unsigned char *, int, std::string * /* warn */, int /* req_width */, int /* req_height */,
void *user_pointer); const unsigned char * /* bytes */, int /* size */, void * /*user_data */)>;
/// ///
/// WriteImageDataFunction type. Signature for custom image writing callbacks. /// WriteImageDataFunction type. Signature for custom image writing callbacks.
/// The out_uri parameter becomes the URI written to the gltf and may reference /// The out_uri parameter becomes the URI written to the gltf and may reference
/// a file or contain a data URI. /// a file or contain a data URI.
/// ///
typedef bool (*WriteImageDataFunction)(const std::string *basepath, using WriteImageDataFunction = std::function<bool(
const std::string *filename, const std::string * /* basepath */, const std::string * /* filename */,
const Image *image, bool embedImages, const Image *image, bool /* embedImages */,
const URICallbacks *uri_cb, const URICallbacks * /* uri_cb */, std::string * /* out_uri */,
std::string *out_uri, void * /* user_pointer */)>;
void *user_pointer);
#ifndef TINYGLTF_NO_STB_IMAGE #ifndef TINYGLTF_NO_STB_IMAGE
// Declaration of default image loader callback // Declaration of default image loader callback
@ -1324,35 +1326,36 @@ bool WriteImageData(const std::string *basepath, const std::string *filename,
#endif #endif
/// ///
/// FilExistsFunction type. Signature for custom filesystem callbacks. /// FileExistsFunction type. Signature for custom filesystem callbacks.
/// ///
typedef bool (*FileExistsFunction)(const std::string &abs_filename, void *); using FileExistsFunction = std::function<bool(
const std::string & /* abs_filename */, void * /* user_data */)>;
/// ///
/// ExpandFilePathFunction type. Signature for custom filesystem callbacks. /// ExpandFilePathFunction type. Signature for custom filesystem callbacks.
/// ///
typedef std::string (*ExpandFilePathFunction)(const std::string &, void *); using ExpandFilePathFunction =
std::function<std::string(const std::string &, void *)>;
/// ///
/// ReadWholeFileFunction type. Signature for custom filesystem callbacks. /// ReadWholeFileFunction type. Signature for custom filesystem callbacks.
/// ///
typedef bool (*ReadWholeFileFunction)(std::vector<unsigned char> *, using ReadWholeFileFunction = std::function<bool(
std::string *, const std::string &, std::vector<unsigned char> *, std::string *, const std::string &, void *)>;
void *);
/// ///
/// WriteWholeFileFunction type. Signature for custom filesystem callbacks. /// WriteWholeFileFunction type. Signature for custom filesystem callbacks.
/// ///
typedef bool (*WriteWholeFileFunction)(std::string *, const std::string &, using WriteWholeFileFunction =
const std::vector<unsigned char> &, std::function<bool(std::string *, const std::string &,
void *); const std::vector<unsigned char> &, void *)>;
/// ///
/// GetFileSizeFunction type. Signature for custom filesystem callbacks. /// GetFileSizeFunction type. Signature for custom filesystem callbacks.
/// ///
typedef bool (*GetFileSizeFunction)(size_t *filesize_out, std::string *err, using GetFileSizeFunction =
const std::string &abs_filename, std::function<bool(size_t *filesize_out, std::string *err,
void *userdata); const std::string &abs_filename, void *userdata)>;
/// ///
/// A structure containing all required filesystem callbacks and a pointer to /// A structure containing all required filesystem callbacks and a pointer to
@ -1475,7 +1478,8 @@ class TinyGLTF {
void SetParseStrictness(ParseStrictness strictness); void SetParseStrictness(ParseStrictness strictness);
/// ///
/// Set callback to use for loading image data /// Set callback to use for loading image data. Passing the nullptr is akin to
/// calling RemoveImageLoader().
/// ///
void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data); void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data);
@ -1490,14 +1494,18 @@ class TinyGLTF {
void SetImageWriter(WriteImageDataFunction WriteImageData, void *user_data); void SetImageWriter(WriteImageDataFunction WriteImageData, void *user_data);
/// ///
/// Set callbacks to use for URI encoding and decoding and their user data /// Set callbacks to use for URI encoding and decoding and their user data.
/// Returns false if there is an error with the callbacks. If err is not
/// nullptr, explanation will be written there.
/// ///
void SetURICallbacks(URICallbacks callbacks); bool SetURICallbacks(URICallbacks callbacks, std::string* err = nullptr);
/// ///
/// Set callbacks to use for filesystem (fs) access and their user data /// Set callbacks to use for filesystem (fs) access and their user data.
/// Returns false if there is an error with the callbacks. If err is not
/// nullptr, explanation will be written there.
/// ///
void SetFsCallbacks(FsCallbacks callbacks); bool SetFsCallbacks(FsCallbacks callbacks, std::string* err = nullptr);
/// ///
/// Set serializing default values(default = false). /// Set serializing default values(default = false).
@ -2571,7 +2579,11 @@ void TinyGLTF::SetParseStrictness(ParseStrictness strictness) {
} }
void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) { void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) {
LoadImageData = func; if (func == nullptr) {
RemoveImageLoader();
return;
}
LoadImageData = std::move(func);
load_image_user_data_ = user_data; load_image_user_data_ = user_data;
user_image_loader_ = true; user_image_loader_ = true;
} }
@ -2697,7 +2709,7 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err,
#endif #endif
void TinyGLTF::SetImageWriter(WriteImageDataFunction func, void *user_data) { void TinyGLTF::SetImageWriter(WriteImageDataFunction func, void *user_data) {
WriteImageData = func; WriteImageData = std::move(func);
write_image_user_data_ = user_data; write_image_user_data_ = user_data;
} }
@ -2788,14 +2800,36 @@ bool WriteImageData(const std::string *basepath, const std::string *filename,
} }
#endif #endif
void TinyGLTF::SetURICallbacks(URICallbacks callbacks) { bool TinyGLTF::SetURICallbacks(URICallbacks callbacks, std::string* err) {
assert(callbacks.decode); if (callbacks.decode == nullptr) {
if (callbacks.decode) { if (err != nullptr) {
uri_cb = callbacks; *err = "URI Callback require a non-null decode function.";
}
return false;
} }
if (callbacks.decode) {
uri_cb = std::move(callbacks);
}
return true;
} }
void TinyGLTF::SetFsCallbacks(FsCallbacks callbacks) { fs = callbacks; } bool TinyGLTF::SetFsCallbacks(FsCallbacks callbacks, std::string *err) {
// If callbacks are defined at all, they must all be defined.
if (callbacks.FileExists == nullptr || callbacks.ExpandFilePath == nullptr ||
callbacks.ReadWholeFile == nullptr ||
callbacks.WriteWholeFile == nullptr ||
callbacks.GetFileSizeInBytes == nullptr) {
if (err != nullptr) {
*err =
"FS Callbacks must be completely defined. At least one callback is "
"null.";
}
return false;
}
fs = std::move(callbacks);
return true;
}
#ifdef _WIN32 #ifdef _WIN32
static inline std::wstring UTF8ToWchar(const std::string &str) { static inline std::wstring UTF8ToWchar(const std::string &str) {
@ -3200,7 +3234,7 @@ static std::string MimeToExt(const std::string &mimeType) {
static bool UpdateImageObject(const Image &image, std::string &baseDir, static bool UpdateImageObject(const Image &image, std::string &baseDir,
int index, bool embedImages, int index, bool embedImages,
const URICallbacks *uri_cb, const URICallbacks *uri_cb,
WriteImageDataFunction *WriteImageData, const WriteImageDataFunction& WriteImageData,
void *user_data, std::string *out_uri) { void *user_data, std::string *out_uri) {
std::string filename; std::string filename;
std::string ext; std::string ext;
@ -3230,9 +3264,9 @@ static bool UpdateImageObject(const Image &image, std::string &baseDir,
// image data does not exist, this is not considered a failure and the // image data does not exist, this is not considered a failure and the
// original uri should be maintained. // original uri should be maintained.
bool imageWritten = false; bool imageWritten = false;
if (*WriteImageData != nullptr && !filename.empty() && !image.image.empty()) { if (WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
imageWritten = (*WriteImageData)(&baseDir, &filename, &image, embedImages, imageWritten = WriteImageData(&baseDir, &filename, &image, embedImages,
uri_cb, out_uri, user_data); uri_cb, out_uri, user_data);
if (!imageWritten) { if (!imageWritten) {
return false; return false;
} }
@ -4214,7 +4248,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
bool store_original_json_for_extras_and_extensions, bool store_original_json_for_extras_and_extensions,
const std::string &basedir, const size_t max_file_size, const std::string &basedir, const size_t max_file_size,
FsCallbacks *fs, const URICallbacks *uri_cb, FsCallbacks *fs, const URICallbacks *uri_cb,
LoadImageDataFunction *LoadImageData = nullptr, const LoadImageDataFunction& LoadImageData = nullptr,
void *load_image_user_data = nullptr) { void *load_image_user_data = nullptr) {
// A glTF image must either reference a bufferView or an image uri // A glTF image must either reference a bufferView or an image uri
@ -4345,14 +4379,14 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
#endif #endif
} }
if (*LoadImageData == nullptr) { if (LoadImageData == nullptr) {
if (err) { if (err) {
(*err) += "No LoadImageData callback specified.\n"; (*err) += "No LoadImageData callback specified.\n";
} }
return false; return false;
} }
return (*LoadImageData)(image, image_idx, err, warn, 0, 0, &img.at(0), return LoadImageData(image, image_idx, err, warn, 0, 0, &img.at(0),
static_cast<int>(img.size()), load_image_user_data); static_cast<int>(img.size()), load_image_user_data);
} }
static bool ParseTexture(Texture *texture, std::string *err, static bool ParseTexture(Texture *texture, std::string *err,
@ -6341,7 +6375,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
if (!ParseImage(&image, idx, err, warn, o, if (!ParseImage(&image, idx, err, warn, o,
store_original_json_for_extras_and_extensions_, base_dir, store_original_json_for_extras_and_extensions_, base_dir,
max_external_file_size_, &fs, &uri_cb, max_external_file_size_, &fs, &uri_cb,
&this->LoadImageData, load_image_user_data)) { this->LoadImageData, load_image_user_data)) {
return false; return false;
} }
@ -6370,7 +6404,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
} }
const Buffer &buffer = model->buffers[size_t(bufferView.buffer)]; const Buffer &buffer = model->buffers[size_t(bufferView.buffer)];
if (*LoadImageData == nullptr) { if (LoadImageData == nullptr) {
if (err) { if (err) {
(*err) += "No LoadImageData callback specified.\n"; (*err) += "No LoadImageData callback specified.\n";
} }
@ -8513,7 +8547,7 @@ bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream,
// we // we
std::string uri; std::string uri;
if (!UpdateImageObject(model->images[i], dummystring, int(i), true, if (!UpdateImageObject(model->images[i], dummystring, int(i), true,
&uri_cb, &this->WriteImageData, &uri_cb, this->WriteImageData,
this->write_image_user_data_, &uri)) { this->write_image_user_data_, &uri)) {
return false; return false;
} }
@ -8621,7 +8655,7 @@ bool TinyGLTF::WriteGltfSceneToFile(const Model *model,
std::string uri; std::string uri;
if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages, if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages,
&uri_cb, &this->WriteImageData, &uri_cb, this->WriteImageData,
this->write_image_user_data_, &uri)) { this->write_image_user_data_, &uri)) {
return false; return false;
} }