/*
 * Copyright (C) 2023 Hasl Developers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#ifndef HASL_VERSION_H
#define HASL_VERSION_H

#include <glib.h>

#include <hasl/haslversionconsts.h>

/* clang-format tries to remove the space after 'if', which confuses
 * gobject-introspection, so turn it off temporarily. */
/* clang-format off */
#if (defined(_WIN32) || defined(__CYGWIN__)) && \
	!defined(HASL_STATIC_COMPILATION)
/* clang-format on */
#define _HASL_EXPORT __declspec(dllexport)
#define _HASL_IMPORT __declspec(dllimport)
#elif __GNUC__ >= 4
#define _HASL_EXPORT __attribute__((visibility("default")))
#define _HASL_IMPORT
#else
#define _HASL_EXPORT
#define _HASL_IMPORT
#endif
#ifdef HASL_COMPILATION
#define _HASL_API _HASL_EXPORT
#else
#define _HASL_API _HASL_IMPORT
#endif

#define _HASL_EXTERN _HASL_API extern

/**
 * HASL_VERSION_MIN_REQUIRED:
 *
 * A macro that should be defined by the user prior to including the `hasl.h`
 * header.
 *
 * The definition should be one of the predefined Hasl version macros:
 * %HASL_VERSION_0_2, %HASL_VERSION_0_3, ...
 *
 * This macro defines the earliest version of Hasl that the package is required
 * to be able to compile against.
 *
 * If the compiler is configured to warn about the use of deprecated functions,
 * then using functions that were deprecated in version
 * %HASL_VERSION_MIN_REQUIRED or earlier will cause warnings (but using
 * functions deprecated in later releases will not).
 *
 * Since: 0.3
 */

#if !defined(HASL_DISABLE_DEPRECATION_WARNINGS) && \
            (G_GNUC_CHECK_VERSION(4, 6) || \
             __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 4))
#define _HASL_GNUC_DO_PRAGMA(x) _Pragma(G_STRINGIFY (x))
#define HASL_DEPRECATED_MACRO _GDK_GNUC_DO_PRAGMA(GCC warning "Deprecated pre-processor symbol")
#define HASL_DEPRECATED_MACRO_FOR(f) \
  _HASL_GNUC_DO_PRAGMA(GCC warning G_STRINGIFY (Deprecated pre-processor symbol: replace with #f))
#define HASL_UNAVAILABLE_MACRO(maj,min) \
  _HASL_GNUC_DO_PRAGMA(GCC warning G_STRINGIFY (Not available before maj.min))
#else
#define HASL_DEPRECATED_MACRO
#define HASL_DEPRECATED_MACRO_FOR(f)
#define HASL_UNAVAILABLE_MACRO(maj,min)
#endif

#ifdef HASL_DISABLE_DEPRECATION_WARNINGS
#define HASL_DEPRECATED _HASL_EXTERN
#define HASL_DEPRECATED_ENUMERATOR
#define HASL_DEPRECATED_ENUMERATOR_FOR(f)
#define HASL_DEPRECATED_FOR(f) _HASL_EXTERN
#define HASL_UNAVAILABLE(maj, min) _HASL_EXTERN
#define HASL_UNAVAILABLE_ENUMERATOR(maj, min)
#define HASL_UNAVAILABLE_STATIC_INLINE(maj, min)
#define HASL_UNAVAILABLE_TYPE(maj, min)
#else
#define HASL_DEPRECATED G_DEPRECATED _HASL_EXTERN
#define HASL_DEPRECATED_ENUMERATOR G_DEPRECATED
#define HASL_DEPRECATED_ENUMERATOR_FOR(f) G_DEPRECATED_FOR(f)
#define HASL_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _HASL_EXTERN
#define HASL_UNAVAILABLE(maj, min) G_UNAVAILABLE(maj, min) _HASL_EXTERN
#define HASL_UNAVAILABLE_ENUMERATOR(maj, min) G_UNAVAILABLE(maj,min)
#define HASL_UNAVAILABLE_STATIC_INLINE(maj, min) G_UNAVAILABLE(maj, min)
#define HASL_UNAVAILABLE_TYPE(maj, min) G_UNAVAILABLE(maj, min)
#endif

/**
 * HASL_VERSION_CUR_STABLE:
 *
 * A macro that evaluates to the current stable version of hasl, in a format
 * that can be used by the C pre-processor.
 *
 * Since: 0.3
 */
#define HASL_VERSION_CUR_STABLE \
	(G_ENCODE_VERSION(HASL_MAJOR_VERSION, HASL_MINOR_VERSION))

/* If the package sets HASL_VERSION_MIN_REQUIRED to some future
 * HASL_VERSION_X_Y value that we don't know about, it will compare as 0 in
 * preprocessor tests.
 */
#ifndef HASL_VERSION_MIN_REQUIRED
#define HASL_VERSION_MIN_REQUIRED (HASL_VERSION_CUR_STABLE)
#elif HASL_VERSION_MIN_REQUIRED == 0
#undef HASL_VERSION_MIN_REQUIRED
#define HASL_VERSION_MIN_REQUIRED (HASL_VERSION_CUR_STABLE + 1)
#endif /* HASL_VERSION_MIN_REQUIRED */

#if !defined(HASL_VERSION_MAX_ALLOWED) || (HASL_VERSION_MAX_ALLOWED == 0)
#undef HASL_VERSION_MAX_ALLOWED
#define HASL_VERSION_MAX_ALLOWED (HASL_VERSION_CUR_STABLE)
#endif /* HASL_VERSION_MAX_ALLOWED */

/* sanity checks */
#if HASL_VERSION_MIN_REQUIRED > HASL_VERSION_CUR_STABLE
#error "HASL_VERSION_MIN_REQUIRED must be <= HASL_VERSION_CUR_STABLE"
#endif
#if HASL_VERSION_MAX_ALLOWED < HASL_VERSION_MIN_REQUIRED
#error "HASL_VERSION_MAX_ALLOWED must be >= HASL_VERSION_MIN_REQUIRED"
#endif
#if HASL_VERSION_MIN_REQUIRED < G_ENCODE_VERSION(0, 1)
#error "HASL_VERSION_MIN_REQUIRED must be >= HASL_VERSION_0_1"
#endif

#define HASL_VAR _HASL_EXTERN
#define HASL_AVAILABLE_IN_ALL _HASL_EXTERN

/**
 * HASL_VERSION_0_1:
 *
 * A macro that evaluates to the 0.1 version of hasl, in a format that can be
 * used by the C pre-processor.
 *
 * Since: 0.3
 */
#define HASL_VERSION_0_1 (G_ENCODE_VERSION(0, 1))

#if HASL_VERSION_MAX_ALLOWED < HASL_VERSION_0_1
#define HASL_AVAILABLE_IN_0_1 HASL_UNAVAILABLE(0, 1)
#else
#define HASL_AVAILABLE_IN_0_1 _HASL_EXTERN
#endif

/**
 * HASL_VERSION_0_2:
 *
 * A macro that evaluates to the 0.2 version of hasl, in a format that can be
 * used by the C pre-processor.
 *
 * Since: 0.3
 */
#define HASL_VERSION_0_2 (G_ENCODE_VERSION(0, 2))

#if HASL_VERSION_MAX_ALLOWED < HASL_VERSION_0_2
#define HASL_AVAILABLE_IN_0_2 HASL_UNAVAILABLE(0, 2)
#else
#define HASL_AVAILABLE_IN_0_2 _HASL_EXTERN
#endif

/**
 * HASL_VERSION_0_3:
 *
 * A macro that evaluates to the 0.3 version of hasl, in a format that can be
 * used by the C pre-processor.
 *
 * Since: 0.3
 */
#define HASL_VERSION_0_3 (G_ENCODE_VERSION(0, 3))

#if HASL_VERSION_MAX_ALLOWED < HASL_VERSION_0_3
#define HASL_AVAILABLE_IN_0_3 HASL_UNAVAILABLE(0, 3)
#else
#define HASL_AVAILABLE_IN_0_3 _HASL_EXTERN
#endif

/**
 * HASL_VERSION_0_4:
 *
 * A macro that evaluates to the 0.4 version of hasl, in a format that can be
 * used by the C pre-processor.
 *
 * Since: 0.4
 */
#define HASL_VERSION_0_4 (G_ENCODE_VERSION(0, 4))

#if HASL_VERSION_MAX_ALLOWED < HASL_VERSION_0_4
#define HASL_AVAILABLE_IN_0_4 HASL_UNAVAILABLE(0, 4)
#else
#define HASL_AVAILABLE_IN_0_4 _HASL_EXTERN
#endif

/**
 * HASL_VERSION_0_5:
 *
 * A macro that evaluates to the 0.5 version of hasl, in a format that can be
 * used by the C pre-processor.
 *
 * Since: 0.5
 */
#define HASL_VERSION_0_5 (G_ENCODE_VERSION(0, 5))

#if HASL_VERSION_MAX_ALLOWED < HASL_VERSION_0_5
#define HASL_AVAILABLE_IN_0_5 HASL_UNAVAILABLE(0, 5)
#define HASL_AVAILABLE_ENUMERATOR_IN_0_5 HASL_UNAVAILABLE_ENUMERATOR(0, 5)
#define HASL_AVAILABLE_STATIC_INLINE_IN_0_5 HASL_UNAVAILABLE_STATIC_INLINE(0, 5)
#define HASL_DEPRECATED_ENUMERATOR_IN_0_5_FOR(f) HASL_DEPRECATED_ENUMERATOR_FOR(f)
#define HASL_DEPRECATED_MACRO_IN_0_5_FOR(f) HASL_DEPRECATED_MACRO_FOR(f)
#else
#define HASL_AVAILABLE_IN_0_5 _HASL_EXTERN
#define HASL_AVAILABLE_ENUMERATOR_IN_0_5
#define HASL_AVAILABLE_STATIC_INLINE_IN_0_5
#define HASL_DEPRECATED_ENUMERATOR_IN_0_5_FOR(f)
#define HASL_DEPRECATED_MACRO_IN_0_5_FOR(f)
#endif

G_BEGIN_DECLS

/**
 * HASL_CHECK_VERSION:
 * @major: The major version to check for.
 * @minor: The minor version to check for.
 * @micro: The micro version to check for.
 *
 * Checks the version of hasl being compiled against. See [func@check_version]
 * for a runtime check.
 *
 * Returns: %TRUE if the version is the same or newer than the passed-in
 *          version.
 *
 * Since: 0.2
 */
#define HASL_CHECK_VERSION(major, minor, micro) ((major) == HASL_MAJOR_VERSION && \
                                                 ((minor) < HASL_MINOR_VERSION || \
                                                  ((minor) == HASL_MINOR_VERSION && (micro) <= HASL_MICRO_VERSION)))

/**
 * hasl_check_version:
 * @required_major: the required major version.
 * @required_minor: the required minor version.
 * @required_micro: the required micro version.
 *
 * Checks that the hasl version is compatible with the requested version.
 *
 * Returns: %NULL if the versions are compatible, or a string describing
 *          the version mismatch if not compatible.
 *
 * Since: 0.2
 */
HASL_AVAILABLE_IN_0_2
const char *hasl_check_version(guint required_major, guint required_minor, guint required_micro);

/**
 * hasl_version:
 *
 * The full version string of the running hasl.
 *
 * Since: 0.2
 */
HASL_VAR const char *hasl_version;

/**
 * hasl_major_version:
 *
 * The major version of the running hasl. Contrast with #HASL_MAJOR_VERSION,
 * which expands at compile time to the major version of hasl being compiled
 * against.
 *
 * Since: 0.2
 */
HASL_VAR const guint hasl_major_version;

/**
 * hasl_minor_version:
 *
 * The minor version of the running hasl. Contrast with #HASL_MINOR_VERSION,
 * which expands at compile time to the minor version of hasl being compiled
 * against.
 *
 * Since: 0.2
 */
HASL_VAR const guint hasl_minor_version;

/**
 * hasl_micro_version:
 *
 * The micro version of the running hasl. Contrast with #HASL_MICRO_VERSION,
 * which expands at compile time to the micro version of hasl being compiled
 * against.
 *
 * Since: 0.2
 */
HASL_VAR const guint hasl_micro_version;

G_END_DECLS

#endif /* HASL_VERSION_CONSTS_H */
