Skip to content

Commit 508f00f

Browse files
committed
Put AVIF_NODISCARD after AVIF_API
It looks more consistent to have AVIF_API at the beginning of a function declaration. It also looks nicer to have AVIF_NODISCARD right before the function return type.
1 parent 2ea07bc commit 508f00f

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

include/avif/avif.h

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
extern "C" {
1212
#endif
1313

14+
// Define the internal macro AVIF_USE_NODISCARD if AVIF_NODISCARD will be defined as [[nodiscard]].
15+
// In this case, also use the standard [[...]] attribute syntax for GCC's visibility("default")
16+
// attribute to avoid compilation errors.
17+
#if defined(AVIF_ENABLE_NODISCARD) || (defined(__cplusplus) && __cplusplus >= 201703L) || \
18+
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)
19+
#define AVIF_USE_NODISCARD 1
20+
#endif
21+
1422
// ---------------------------------------------------------------------------
1523
// Export macros
1624

@@ -31,7 +39,11 @@ extern "C" {
3139
#define AVIF_HELPER_EXPORT __declspec(dllexport)
3240
#define AVIF_HELPER_IMPORT __declspec(dllimport)
3341
#elif defined(__GNUC__) && __GNUC__ >= 4
42+
#if defined(AVIF_USE_NODISCARD)
43+
#define AVIF_HELPER_EXPORT [[gnu::visibility("default")]]
44+
#else
3445
#define AVIF_HELPER_EXPORT __attribute__((visibility("default")))
46+
#endif
3547
#define AVIF_HELPER_IMPORT
3648
#else
3749
#define AVIF_HELPER_EXPORT
@@ -48,8 +60,7 @@ extern "C" {
4860
#define AVIF_API
4961
#endif // defined(AVIF_DLL)
5062

51-
#if defined(AVIF_ENABLE_NODISCARD) || (defined(__cplusplus) && __cplusplus >= 201703L) || \
52-
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)
63+
#if defined(AVIF_USE_NODISCARD)
5364
#define AVIF_NODISCARD [[nodiscard]]
5465
#else
5566
// Starting with 3.9, clang allows defining the warn_unused_result attribute for enums.
@@ -510,13 +521,13 @@ typedef struct avifCropRect
510521

511522
// These will return AVIF_FALSE if the resultant values violate any standards, and if so, the output
512523
// values are not guaranteed to be complete or correct and should not be used.
513-
AVIF_NODISCARD AVIF_API avifBool avifCropRectConvertCleanApertureBox(avifCropRect * cropRect,
524+
AVIF_API AVIF_NODISCARD avifBool avifCropRectConvertCleanApertureBox(avifCropRect * cropRect,
514525
const avifCleanApertureBox * clap,
515526
uint32_t imageW,
516527
uint32_t imageH,
517528
avifPixelFormat yuvFormat,
518529
avifDiagnostics * diag);
519-
AVIF_NODISCARD AVIF_API avifBool avifCleanApertureBoxConvertCropRect(avifCleanApertureBox * clap,
530+
AVIF_API AVIF_NODISCARD avifBool avifCleanApertureBoxConvertCropRect(avifCleanApertureBox * clap,
520531
const avifCropRect * cropRect,
521532
uint32_t imageW,
522533
uint32_t imageH,
@@ -705,10 +716,10 @@ typedef struct avifGainMapMetadataDouble
705716
// Converts a avifGainMapMetadataDouble to avifGainMapMetadata by converting double values
706717
// to the closest uint32_t fractions.
707718
// Returns AVIF_FALSE if some field values are < 0 or > UINT32_MAX.
708-
AVIF_NODISCARD AVIF_API avifBool avifGainMapMetadataDoubleToFractions(avifGainMapMetadata * dst, const avifGainMapMetadataDouble * src);
719+
AVIF_API AVIF_NODISCARD avifBool avifGainMapMetadataDoubleToFractions(avifGainMapMetadata * dst, const avifGainMapMetadataDouble * src);
709720
// Converts a avifGainMapMetadata to avifGainMapMetadataDouble by converting fractions to double values.
710721
// Returns AVIF_FALSE if some denominators are zero.
711-
AVIF_NODISCARD AVIF_API avifBool avifGainMapMetadataFractionsToDouble(avifGainMapMetadataDouble * dst, const avifGainMapMetadata * src);
722+
AVIF_API AVIF_NODISCARD avifBool avifGainMapMetadataFractionsToDouble(avifGainMapMetadataDouble * dst, const avifGainMapMetadata * src);
712723

713724
#endif // AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP
714725

@@ -819,8 +830,8 @@ typedef struct avifImage
819830
} avifImage;
820831

821832
// avifImageCreate() and avifImageCreateEmpty() return NULL if arguments are invalid or if a memory allocation failed.
822-
AVIF_NODISCARD AVIF_API avifImage * avifImageCreate(uint32_t width, uint32_t height, uint32_t depth, avifPixelFormat yuvFormat);
823-
AVIF_NODISCARD AVIF_API avifImage * avifImageCreateEmpty(void); // helper for making an image to decode into
833+
AVIF_API AVIF_NODISCARD avifImage * avifImageCreate(uint32_t width, uint32_t height, uint32_t depth, avifPixelFormat yuvFormat);
834+
AVIF_API AVIF_NODISCARD avifImage * avifImageCreateEmpty(void); // helper for making an image to decode into
824835
// Performs a deep copy of an image, including all metadata and planes, and the gain map metadata/planes if present
825836
// and if AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP is defined.
826837
AVIF_API avifResult avifImageCopy(avifImage * dstImage, const avifImage * srcImage, avifPlanesFlags planes);
@@ -1366,7 +1377,7 @@ AVIF_API avifResult avifDecoderReset(avifDecoder * decoder);
13661377
// frameIndex - 0-based, matching avifDecoder->imageIndex, bound by avifDecoder->imageCount
13671378
// "nearest" keyframe means the keyframe prior to this frame index (returns frameIndex if it is a keyframe)
13681379
// These functions may be used after a successful call (AVIF_RESULT_OK) to avifDecoderParse().
1369-
AVIF_NODISCARD AVIF_API avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex);
1380+
AVIF_API AVIF_NODISCARD avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex);
13701381
AVIF_API uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex);
13711382

13721383
// Timing helper - This does not change the current image or invoke the codec (safe to call repeatedly)
@@ -1510,7 +1521,7 @@ typedef struct avifEncoder
15101521
} avifEncoder;
15111522

15121523
// avifEncoderCreate() returns NULL if a memory allocation failed.
1513-
AVIF_NODISCARD AVIF_API avifEncoder * avifEncoderCreate(void);
1524+
AVIF_API AVIF_NODISCARD avifEncoder * avifEncoderCreate(void);
15141525
AVIF_API avifResult avifEncoderWrite(avifEncoder * encoder, const avifImage * image, avifRWData * output);
15151526
AVIF_API void avifEncoderDestroy(avifEncoder * encoder);
15161527

@@ -1575,8 +1586,8 @@ AVIF_API size_t avifEncoderGetGainMapSizeBytes(avifEncoder * encoder);
15751586
#endif
15761587

15771588
// Helpers
1578-
AVIF_NODISCARD AVIF_API avifBool avifImageUsesU16(const avifImage * image);
1579-
AVIF_NODISCARD AVIF_API avifBool avifImageIsOpaque(const avifImage * image);
1589+
AVIF_API AVIF_NODISCARD avifBool avifImageUsesU16(const avifImage * image);
1590+
AVIF_API AVIF_NODISCARD avifBool avifImageIsOpaque(const avifImage * image);
15801591
// channel can be an avifChannelIndex.
15811592
AVIF_API uint8_t * avifImagePlane(const avifImage * image, int channel);
15821593
AVIF_API uint32_t avifImagePlaneRowBytes(const avifImage * image, int channel);
@@ -1585,7 +1596,7 @@ AVIF_API uint32_t avifImagePlaneHeight(const avifImage * image, int channel);
15851596

15861597
// Returns AVIF_TRUE if input begins with a valid FileTypeBox (ftyp) that supports
15871598
// either the brand 'avif' or 'avis' (or both), without performing any allocations.
1588-
AVIF_NODISCARD AVIF_API avifBool avifPeekCompatibleFileType(const avifROData * input);
1599+
AVIF_API AVIF_NODISCARD avifBool avifPeekCompatibleFileType(const avifROData * input);
15891600

15901601
#if defined(AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP)
15911602
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)