Drop default arguments in C API

PiperOrigin-RevId: 579965820
This commit is contained in:
Sebastian Schmidt 2023-11-06 14:53:42 -08:00 committed by Copybara-Service
parent a8d88bf7cf
commit 2abaabce0e
8 changed files with 49 additions and 47 deletions

View File

@ -60,18 +60,18 @@ struct LanguageDetectorOptions {
// Creates a LanguageDetector from the provided `options`. // Creates a LanguageDetector from the provided `options`.
// Returns a pointer to the language detector on success. // Returns a pointer to the language detector on success.
// If an error occurs, returns `nullptr` and sets the error parameter to an // If an error occurs, returns `nullptr` and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT void* language_detector_create( MP_EXPORT void* language_detector_create(
struct LanguageDetectorOptions* options, char** error_msg = nullptr); struct LanguageDetectorOptions* options, char** error_msg);
// Performs language detection on the input `text`. Returns `0` on success. // Performs language detection on the input `text`. Returns `0` on success.
// If an error occurs, returns an error code and sets the error parameter to an // If an error occurs, returns an error code and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int language_detector_detect(void* detector, const char* utf8_str, MP_EXPORT int language_detector_detect(void* detector, const char* utf8_str,
LanguageDetectorResult* result, LanguageDetectorResult* result,
char** error_msg = nullptr); char** error_msg);
// Frees the memory allocated inside a LanguageDetectorResult result. Does not // Frees the memory allocated inside a LanguageDetectorResult result. Does not
// free the result pointer itself. // free the result pointer itself.
@ -79,10 +79,9 @@ MP_EXPORT void language_detector_close_result(LanguageDetectorResult* result);
// Shuts down the LanguageDetector when all the work is done. Frees all memory. // Shuts down the LanguageDetector when all the work is done. Frees all memory.
// If an error occurs, returns an error code and sets the error parameter to an // If an error occurs, returns an error code and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int language_detector_close(void* detector, MP_EXPORT int language_detector_close(void* detector, char** error_msg);
char** error_msg = nullptr);
#ifdef __cplusplus #ifdef __cplusplus
} // extern C } // extern C

View File

@ -56,16 +56,17 @@ TEST(LanguageDetectorTest, SmokeTest) {
/* category_denylist_count= */ 0}, /* category_denylist_count= */ 0},
}; };
void* detector = language_detector_create(&options); void* detector = language_detector_create(&options, /* error_msg */ nullptr);
EXPECT_NE(detector, nullptr); EXPECT_NE(detector, nullptr);
LanguageDetectorResult result; LanguageDetectorResult result;
language_detector_detect(detector, kTestString, &result); language_detector_detect(detector, kTestString, &result,
/* error_msg */ nullptr);
EXPECT_EQ(std::string(result.predictions[0].language_code), "fr"); EXPECT_EQ(std::string(result.predictions[0].language_code), "fr");
EXPECT_NEAR(result.predictions[0].probability, 0.999781, kPrecision); EXPECT_NEAR(result.predictions[0].probability, 0.999781, kPrecision);
language_detector_close_result(&result); language_detector_close_result(&result);
language_detector_close(detector); language_detector_close(detector, /* error_msg */ nullptr);
} }
TEST(LanguageDetectorTest, ErrorHandling) { TEST(LanguageDetectorTest, ErrorHandling) {

View File

@ -44,18 +44,18 @@ struct TextClassifierOptions {
// Creates a TextClassifier from the provided `options`. // Creates a TextClassifier from the provided `options`.
// Returns a pointer to the text classifier on success. // Returns a pointer to the text classifier on success.
// If an error occurs, returns `nullptr` and sets the error parameter to an // If an error occurs, returns `nullptr` and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT void* text_classifier_create(struct TextClassifierOptions* options, MP_EXPORT void* text_classifier_create(struct TextClassifierOptions* options,
char** error_msg = nullptr); char** error_msg);
// Performs classification on the input `text`. Returns `0` on success. // Performs classification on the input `text`. Returns `0` on success.
// If an error occurs, returns an error code and sets the error parameter to an // If an error occurs, returns an error code and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int text_classifier_classify(void* classifier, const char* utf8_str, MP_EXPORT int text_classifier_classify(void* classifier, const char* utf8_str,
TextClassifierResult* result, TextClassifierResult* result,
char** error_msg = nullptr); char** error_msg);
// Frees the memory allocated inside a TextClassifierResult result. Does not // Frees the memory allocated inside a TextClassifierResult result. Does not
// free the result pointer itself. // free the result pointer itself.
@ -63,10 +63,9 @@ MP_EXPORT void text_classifier_close_result(TextClassifierResult* result);
// Shuts down the TextClassifier when all the work is done. Frees all memory. // Shuts down the TextClassifier when all the work is done. Frees all memory.
// If an error occurs, returns an error code and sets the error parameter to an // If an error occurs, returns an error code and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int text_classifier_close(void* classifier, MP_EXPORT int text_classifier_close(void* classifier, char** error_msg);
char** error_msg = nullptr);
#ifdef __cplusplus #ifdef __cplusplus
} // extern C } // extern C

View File

@ -55,11 +55,12 @@ TEST(TextClassifierTest, SmokeTest) {
/* category_denylist_count= */ 0}, /* category_denylist_count= */ 0},
}; };
void* classifier = text_classifier_create(&options); void* classifier = text_classifier_create(&options, /* error_msg */ nullptr);
EXPECT_NE(classifier, nullptr); EXPECT_NE(classifier, nullptr);
TextClassifierResult result; TextClassifierResult result;
text_classifier_classify(classifier, kTestString, &result); text_classifier_classify(classifier, kTestString, &result,
/* error_msg */ nullptr);
EXPECT_EQ(result.classifications_count, 1); EXPECT_EQ(result.classifications_count, 1);
EXPECT_EQ(result.classifications[0].categories_count, 2); EXPECT_EQ(result.classifications[0].categories_count, 2);
EXPECT_EQ(std::string{result.classifications[0].categories[0].category_name}, EXPECT_EQ(std::string{result.classifications[0].categories[0].category_name},
@ -68,7 +69,7 @@ TEST(TextClassifierTest, SmokeTest) {
kPrecision); kPrecision);
text_classifier_close_result(&result); text_classifier_close_result(&result);
text_classifier_close(classifier); text_classifier_close(classifier, /* error_msg */ nullptr);
} }
TEST(TextClassifierTest, ErrorHandling) { TEST(TextClassifierTest, ErrorHandling) {

View File

@ -47,15 +47,14 @@ struct TextEmbedderOptions {
// an error message (if `error_msg` is not `nullptr`). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT void* text_embedder_create(struct TextEmbedderOptions* options, MP_EXPORT void* text_embedder_create(struct TextEmbedderOptions* options,
char** error_msg = nullptr); char** error_msg);
// Performs embedding extraction on the input `text`. Returns `0` on success. // Performs embedding extraction on the input `text`. Returns `0` on success.
// If an error occurs, returns an error code and sets the error parameter to an // If an error occurs, returns an error code and sets the error parameter to an
// an error message (if `error_msg` is not `nullptr`). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int text_embedder_embed(void* embedder, const char* utf8_str, MP_EXPORT int text_embedder_embed(void* embedder, const char* utf8_str,
TextEmbedderResult* result, TextEmbedderResult* result, char** error_msg);
char** error_msg = nullptr);
// Frees the memory allocated inside a TextEmbedderResult result. Does not // Frees the memory allocated inside a TextEmbedderResult result. Does not
// free the result pointer itself. // free the result pointer itself.
@ -65,7 +64,7 @@ MP_EXPORT void text_embedder_close_result(TextEmbedderResult* result);
// If an error occurs, returns an error code and sets the error parameter to an // If an error occurs, returns an error code and sets the error parameter to an
// an error message (if `error_msg` is not `nullptr`). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int text_embedder_close(void* embedder, char** error_msg = nullptr); MP_EXPORT int text_embedder_close(void* embedder, char** error_msg);
#ifdef __cplusplus #ifdef __cplusplus
} // extern C } // extern C

View File

@ -48,16 +48,16 @@ TEST(TextEmbedderTest, SmokeTest) {
{/* l2_normalize= */ false, /* quantize= */ true}, {/* l2_normalize= */ false, /* quantize= */ true},
}; };
void* embedder = text_embedder_create(&options); void* embedder = text_embedder_create(&options, /* error_msg */ nullptr);
EXPECT_NE(embedder, nullptr); EXPECT_NE(embedder, nullptr);
TextEmbedderResult result; TextEmbedderResult result;
text_embedder_embed(embedder, kTestString, &result); text_embedder_embed(embedder, kTestString, &result, /* error_msg */ nullptr);
EXPECT_EQ(result.embeddings_count, 1); EXPECT_EQ(result.embeddings_count, 1);
EXPECT_EQ(result.embeddings[0].values_count, 512); EXPECT_EQ(result.embeddings[0].values_count, 512);
text_embedder_close_result(&result); text_embedder_close_result(&result);
text_embedder_close(embedder); text_embedder_close(embedder, /* error_msg */ nullptr);
} }
TEST(TextEmbedderTest, ErrorHandling) { TEST(TextEmbedderTest, ErrorHandling) {

View File

@ -108,30 +108,30 @@ struct ImageClassifierOptions {
// Creates an ImageClassifier from provided `options`. // Creates an ImageClassifier from provided `options`.
// Returns a pointer to the image classifier on success. // Returns a pointer to the image classifier on success.
// If an error occurs, returns `nullptr` and sets the error parameter to an // If an error occurs, returns `nullptr` and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT void* image_classifier_create(struct ImageClassifierOptions* options, MP_EXPORT void* image_classifier_create(struct ImageClassifierOptions* options,
char** error_msg = nullptr); char** error_msg);
// Performs image classification on the input `image`. Returns `0` on success. // Performs image classification on the input `image`. Returns `0` on success.
// If an error occurs, returns an error code and sets the error parameter to an // If an error occurs, returns an error code and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int image_classifier_classify_image(void* classifier, MP_EXPORT int image_classifier_classify_image(void* classifier,
const MpImage* image, const MpImage* image,
ImageClassifierResult* result, ImageClassifierResult* result,
char** error_msg = nullptr); char** error_msg);
MP_EXPORT int image_classifier_classify_for_video(void* classifier, MP_EXPORT int image_classifier_classify_for_video(void* classifier,
const MpImage* image, const MpImage* image,
int64_t timestamp_ms, int64_t timestamp_ms,
ImageClassifierResult* result, ImageClassifierResult* result,
char** error_msg = nullptr); char** error_msg);
MP_EXPORT int image_classifier_classify_async(void* classifier, MP_EXPORT int image_classifier_classify_async(void* classifier,
const MpImage* image, const MpImage* image,
int64_t timestamp_ms, int64_t timestamp_ms,
char** error_msg = nullptr); char** error_msg);
// Frees the memory allocated inside a ImageClassifierResult result. // Frees the memory allocated inside a ImageClassifierResult result.
// Does not free the result pointer itself. // Does not free the result pointer itself.
@ -139,10 +139,9 @@ MP_EXPORT void image_classifier_close_result(ImageClassifierResult* result);
// Frees image classifier. // Frees image classifier.
// If an error occurs, returns an error code and sets the error parameter to an // If an error occurs, returns an error code and sets the error parameter to an
// an error message (if `error_msg` is not nullptr). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int image_classifier_close(void* classifier, MP_EXPORT int image_classifier_close(void* classifier, char** error_msg);
char** error_msg = nullptr);
#ifdef __cplusplus #ifdef __cplusplus
} // extern C } // extern C

View File

@ -63,7 +63,7 @@ TEST(ImageClassifierTest, ImageModeTest) {
/* category_denylist_count= */ 0}, /* category_denylist_count= */ 0},
}; };
void* classifier = image_classifier_create(&options); void* classifier = image_classifier_create(&options, /* error_msg */ nullptr);
EXPECT_NE(classifier, nullptr); EXPECT_NE(classifier, nullptr);
const auto& image_frame = image->GetImageFrameSharedPtr(); const auto& image_frame = image->GetImageFrameSharedPtr();
@ -75,7 +75,8 @@ TEST(ImageClassifierTest, ImageModeTest) {
.height = image_frame->Height()}}; .height = image_frame->Height()}};
ImageClassifierResult result; ImageClassifierResult result;
image_classifier_classify_image(classifier, &mp_image, &result); image_classifier_classify_image(classifier, &mp_image, &result,
/* error_msg */ nullptr);
EXPECT_EQ(result.classifications_count, 1); EXPECT_EQ(result.classifications_count, 1);
EXPECT_EQ(result.classifications[0].categories_count, 1001); EXPECT_EQ(result.classifications[0].categories_count, 1001);
EXPECT_EQ(std::string{result.classifications[0].categories[0].category_name}, EXPECT_EQ(std::string{result.classifications[0].categories[0].category_name},
@ -83,7 +84,7 @@ TEST(ImageClassifierTest, ImageModeTest) {
EXPECT_NEAR(result.classifications[0].categories[0].score, 0.7939f, EXPECT_NEAR(result.classifications[0].categories[0].score, 0.7939f,
kPrecision); kPrecision);
image_classifier_close_result(&result); image_classifier_close_result(&result);
image_classifier_close(classifier); image_classifier_close(classifier, /* error_msg */ nullptr);
} }
TEST(ImageClassifierTest, VideoModeTest) { TEST(ImageClassifierTest, VideoModeTest) {
@ -107,7 +108,7 @@ TEST(ImageClassifierTest, VideoModeTest) {
/* result_callback= */ nullptr, /* result_callback= */ nullptr,
}; };
void* classifier = image_classifier_create(&options); void* classifier = image_classifier_create(&options, /* error_msg */ nullptr);
EXPECT_NE(classifier, nullptr); EXPECT_NE(classifier, nullptr);
const auto& image_frame = image->GetImageFrameSharedPtr(); const auto& image_frame = image->GetImageFrameSharedPtr();
@ -120,7 +121,8 @@ TEST(ImageClassifierTest, VideoModeTest) {
for (int i = 0; i < kIterations; ++i) { for (int i = 0; i < kIterations; ++i) {
ImageClassifierResult result; ImageClassifierResult result;
image_classifier_classify_for_video(classifier, &mp_image, i, &result); image_classifier_classify_for_video(classifier, &mp_image, i, &result,
/* error_msg */ nullptr);
EXPECT_EQ(result.classifications_count, 1); EXPECT_EQ(result.classifications_count, 1);
EXPECT_EQ(result.classifications[0].categories_count, 3); EXPECT_EQ(result.classifications[0].categories_count, 3);
EXPECT_EQ( EXPECT_EQ(
@ -130,7 +132,7 @@ TEST(ImageClassifierTest, VideoModeTest) {
kPrecision); kPrecision);
image_classifier_close_result(&result); image_classifier_close_result(&result);
} }
image_classifier_close(classifier); image_classifier_close(classifier, /* error_msg */ nullptr);
} }
// A structure to support LiveStreamModeTest below. This structure holds a // A structure to support LiveStreamModeTest below. This structure holds a
@ -180,7 +182,7 @@ TEST(ImageClassifierTest, LiveStreamModeTest) {
/* result_callback= */ LiveStreamModeCallback::Fn, /* result_callback= */ LiveStreamModeCallback::Fn,
}; };
void* classifier = image_classifier_create(&options); void* classifier = image_classifier_create(&options, /* error_msg */ nullptr);
EXPECT_NE(classifier, nullptr); EXPECT_NE(classifier, nullptr);
const auto& image_frame = image->GetImageFrameSharedPtr(); const auto& image_frame = image->GetImageFrameSharedPtr();
@ -192,9 +194,11 @@ TEST(ImageClassifierTest, LiveStreamModeTest) {
.height = image_frame->Height()}}; .height = image_frame->Height()}};
for (int i = 0; i < kIterations; ++i) { for (int i = 0; i < kIterations; ++i) {
EXPECT_GE(image_classifier_classify_async(classifier, &mp_image, i), 0); EXPECT_GE(image_classifier_classify_async(classifier, &mp_image, i,
/* error_msg */ nullptr),
0);
} }
image_classifier_close(classifier); image_classifier_close(classifier, /* error_msg */ nullptr);
// Due to the flow limiter, the total of outputs might be smaller than the // Due to the flow limiter, the total of outputs might be smaller than the
// number of iterations. // number of iterations.
@ -237,7 +241,7 @@ TEST(ImageClassifierTest, FailedClassificationHandling) {
/* category_denylist_count= */ 0}, /* category_denylist_count= */ 0},
}; };
void* classifier = image_classifier_create(&options); void* classifier = image_classifier_create(&options, /* error_msg */ nullptr);
EXPECT_NE(classifier, nullptr); EXPECT_NE(classifier, nullptr);
const MpImage mp_image = {.type = MpImage::GPU_BUFFER, .gpu_buffer = {}}; const MpImage mp_image = {.type = MpImage::GPU_BUFFER, .gpu_buffer = {}};
@ -246,7 +250,7 @@ TEST(ImageClassifierTest, FailedClassificationHandling) {
image_classifier_classify_image(classifier, &mp_image, &result, &error_msg); image_classifier_classify_image(classifier, &mp_image, &result, &error_msg);
EXPECT_THAT(error_msg, HasSubstr("GPU Buffer not supported yet")); EXPECT_THAT(error_msg, HasSubstr("GPU Buffer not supported yet"));
free(error_msg); free(error_msg);
image_classifier_close(classifier); image_classifier_close(classifier, /* error_msg */ nullptr);
} }
} // namespace } // namespace