/*
Copyright (c) 2013-2016 Gustav Hartvigsson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stddef.h>
#include <uchar.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>
#include <math.h>

#include "config.h"

/** @file
 * @defgroup Definitions Definitions
 * @addtogroup Definitions
 * @{
 * @brief Declarations of the types and macros that are used extensively
 * throughout libssts.
 */

/**
 * @def S_BEGIN_DECLS
 * For interoperability with C++ we have to have this.
 *
 * Put at the beginning of header files.
 * @see S_END_DECLS
 */

/**
 * @def S_END_DECLS
 *
 * Put at end of header files.
 * @see S_BEGIN_DECLS
 */

#ifdef __cplusplus
  #define S_BEGIN_DECLS extern "C" {
  #define S_END_DECLS }
#else
  #define S_BEGIN_DECLS
  #define S_END_DECLS
#endif /*__cplusplus*/

S_BEGIN_DECLS

/* Make sure that we use utf-32 with the suchar string. */
#define STDC_UTF_32 1

/* ***************************** GENERIC MACROS ***************************** */

/** @def
 */

/**
 * @def S_DEPRECATED
 * Mark function as deprecated.
 */
/**
 * @def S_UNUSED
 * Supress compiler warning that variable is unused.
 */
/**
 * @def S_ALIGNED_8
 * Macro to align memory to 8 bit.
 */
/**
 * @def S_ALIGNED_16
 * Macro to align memory to 16 bit.
 */
#if defined(__GNUC__)
  #define S_DEPRECATED __attribute__((deprecated))
  #define S_UNUSED __attribute__((unused))
  #define S_ALIGNED_8 __attribute__((aligned (8)))
  #define S_ALIGNED_16 __attribute__((aligned (16)))
#elif defined(__MSC_VER)
  #define S_DEPRECATED __declspec(deprecated)
  // TODO, check if this rely works in MSVC.
  #define S_UNUSED __pragma(warning(suppress:4100))
  #define S_ALIGNED_8 __declspec(align(8))
  #define S_ALIGNED_16 __declspec(align(16))
#else
  #define S_DEPRECATED
  #define S_UNUSED
  #define S_ALIGNED_8
  #define S_ALIGNED_16
#endif

/**
 * @def __S_ATOMIC__
 * The stupid atomic value.
 */
#if __STDC_NO_ATOMICS__
  #define __S_ATOMIC__ volatile
#else
  #define __S_ATOMIC__ __Atomic
#endif

/**
 * @def S_EXPORTED
 * Mark function as exported. This makes the function available from outside a
 * library on some platforms.
 *
 * @see S_HIDDEN
 */
/**
 * @def S_HIDDEN
 * Mark function as hidden. This hides a function so it is not exposed to the
 * outside of the library.
 *
 * @see S_EXPORTED
 */

/*
 * Copied from: https://gcc.gnu.org/wiki/Visibility .. with modifications.
 */
#if defined _WIN32 || defined __CYGWIN__
  #ifdef BUILDING_DLL
    #ifdef __GNUC__
      #define S_EXPORTED __attribute__ ((dllexport))
    #else
      #define S_EXPORTED __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #else
    #ifdef __GNUC__
      #define S_EXPORTED __attribute__ ((dllimport))
    #else
      #define S_EXPORTED __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #endif
  #define S_HIDDEN
#else
  #if __GNUC__ >= 4
    #define S_EXPORTED __attribute__ ((visibility ("default")))
    #define S_HIDDEN  __attribute__ ((visibility ("hidden")))
  #else
    #define S_EXPORTED
    #define S_HIDDEN
  #endif
#endif

/**
 * FALSE has the absolute value of 0, it is used in as a way to represent a
 * false value. A "not" value.
 *
 * @see sboolean
 */
#define FALSE 0

 /**
  * TRUE represents a value that is true. A value that is "not false".
  *
  * @see sboolean
  */
#define TRUE !FALSE

/**
 * Stupid macro to create a hash of a pointer.
 */
#define S_POINTER_TO_HASH_T(p) ((hash_t)(unsigned long) p)


/* ************************************************************************** *
 * Colours
 * ************************************************************************** */

/**
 * @defgroup Colours Colours
 * @addtogroup Colours
 *   @{
 * @brief Printing colours for terminal/console printing.
 *
 * Here is a list of macros for use when printing to the console.
 * @ref S_COLOR_RESET is used to reset the colours back to their original
 *      colour.
 *
 * An example on how to use them can be found in utils.h:
 * @code{.c}
#define s_err_print(p, ...)\
  fprintf (stderr, S_COLOR_RED "[ERR] " p S_COLOR_RESET "\n", ##__VA_ARGS__)
 * @endcode
 */

#define S_COLOR_RESET   "\033[0m"       /**< Reset */
#define S_COLOR_BLACK   "\033[30m"      /**< Black */
#define S_COLOR_RED     "\033[31m"      /**< Red */
#define S_COLOR_GREEN   "\033[32m"      /**< Green */
#define S_COLOR_YELLOW  "\033[33m"      /**< Yellow */
#define S_COLOR_BLUE    "\033[34m"      /**< Blue */
#define S_COLOR_MAGENTA "\033[35m"      /**< Magenta */
#define S_COLOR_CYAN    "\033[36m"      /**< Cyan */
#define S_COLOR_WHITE   "\033[37m"      /**< White */
#define S_COLOR_BOLDBLACK   "\033[1m\033[30m"      /**< Bold Black */
#define S_COLOR_BOLDRED     "\033[1m\033[31m"      /**< Bold Red */
#define S_COLOR_BOLDGREEN   "\033[1m\033[32m"      /**< Bold Green */
#define S_COLOR_BOLDYELLOW  "\033[1m\033[33m"      /**< Bold Yellow */
#define S_COLOR_BOLDBLUE    "\033[1m\033[34m"      /**< Bold Blue */
#define S_COLOR_BOLDMAGENTA "\033[1m\033[35m"      /**< Bold Magenta */
#define S_COLOR_BOLDCYAN    "\033[1m\033[36m"      /**< Bold Cyan */
#define S_COLOR_BOLDWHITE   "\033[1m\033[37m"      /**< Bold White */

/**   @} */

/** @} */

/* We include these last so it does not create any problems.
 * We want people only to have to include defs.h and not mm.h and types.h
 * anyway.
 *
 * It should be considered a continuation of defs.h.
 */

#include "types.h"

#include "primes.h" /* This is already included in types.h... */

#include "mm.h"


S_END_DECLS
