2015-09-30 03:56:11 +03:00
|
|
|
#ifndef _BASE_SHARED_HPP_
|
|
|
|
#define _BASE_SHARED_HPP_
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-21 00:25:00 +02:00
|
|
|
#include "SqBase.hpp"
|
2015-09-30 03:56:11 +03:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-10 05:57:13 +02:00
|
|
|
#include <cmath>
|
2016-03-26 18:14:47 +02:00
|
|
|
#include <limits>
|
2016-02-21 00:25:00 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <vcmp.h>
|
|
|
|
#include <sqrat.h>
|
2015-09-30 03:56:11 +03:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-21 00:25:00 +02:00
|
|
|
extern const SQChar * g_EmptyStr;
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2015-11-01 10:55:47 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-03-10 05:57:13 +02:00
|
|
|
* Proxies to communicate with the server.
|
2015-11-01 10:55:47 +02:00
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
extern PluginFuncs* _Func;
|
|
|
|
extern PluginCallbacks* _Clbk;
|
|
|
|
extern PluginInfo* _Info;
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Perform an equality comparison between two values taking into account floating point issues.
|
|
|
|
*/
|
2015-11-01 05:48:01 +02:00
|
|
|
template< typename T > inline bool EpsEq(const T a, const T b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return abs(a - b) <= 0;
|
2015-09-30 03:56:11 +03:00
|
|
|
}
|
|
|
|
|
2015-11-01 05:48:01 +02:00
|
|
|
template <> inline bool EpsEq(const Float32 a, const Float32 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
|
|
|
return fabs(a - b) <= 0.000001f;
|
|
|
|
}
|
|
|
|
|
2015-11-01 05:48:01 +02:00
|
|
|
template <> inline bool EpsEq(const Float64 a, const Float64 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
|
|
|
return fabs(a - b) <= 0.000000001d;
|
|
|
|
}
|
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Perform a less than comparison between two values taking into account floating point issues.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
template< typename T > inline bool EpsLt(const T a, const T b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) && (a < b);
|
2015-09-30 03:56:11 +03:00
|
|
|
}
|
|
|
|
|
2016-02-21 00:25:00 +02:00
|
|
|
template <> inline bool EpsLt(const Float32 a, const Float32 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) && (a - b) < 0.000001f;
|
2015-09-30 03:56:11 +03:00
|
|
|
}
|
|
|
|
|
2016-02-21 00:25:00 +02:00
|
|
|
template <> inline bool EpsLt(const Float64 a, const Float64 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) && (a - b) < 0.000000001d;
|
2015-09-30 03:56:11 +03:00
|
|
|
}
|
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Perform a greater than comparison between two values taking into account floating point issues.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
template< typename T > inline bool EpsGt(const T a, const T b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) && (a > b);
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-02-21 00:25:00 +02:00
|
|
|
template <> inline bool EpsGt(const Float32 a, const Float32 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) && (a - b) > 0.000001f;
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-02-21 00:25:00 +02:00
|
|
|
template <> inline bool EpsGt(const Float64 a, const Float64 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) && (a - b) > 0.000000001d;
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Perform a less than or equal comparison between two values taking into account
|
|
|
|
* floating point issues.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
template< typename T > inline bool EpsLtEq(const T a, const T b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) || (a < b);
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-02-21 00:25:00 +02:00
|
|
|
template <> inline bool EpsLtEq(const Float32 a, const Float32 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) || (a - b) < 0.000001f;
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-02-21 00:25:00 +02:00
|
|
|
template <> inline bool EpsLtEq(const Float64 a, const Float64 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) || (a - b) < 0.000000001d;
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Perform a greater than or equal comparison between two values taking into account
|
|
|
|
* floating point issues.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
template< typename T > inline bool EpsGtEq(const T a, const T b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) || (a > b);
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-02-21 00:25:00 +02:00
|
|
|
template <> inline bool EpsGtEq(const Float32 a, const Float32 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) || (a - b) > 0.000001f;
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-02-21 00:25:00 +02:00
|
|
|
template <> inline bool EpsGtEq(const Float64 a, const Float64 b)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
return !EpsEq(a, b) || (a - b) > 0.000000001d;
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-04-14 03:08:06 +03:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
template < typename T > struct ConvTo
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
static constexpr T Min = std::numeric_limits< T >::min();
|
|
|
|
static constexpr T Max = std::numeric_limits< T >::max();
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
template < typename U > static inline T From(U v)
|
|
|
|
{
|
|
|
|
if (v > Max)
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
else if (v < Min)
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
return static_cast< T >(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int8 ConvTo< Int8 >::From< Uint8 >(Uint8 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint8 >(Max)) ? Max : static_cast< Int8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int8 ConvTo< Int8 >::From< Uint16 >(Uint16 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint16 >(Max)) ? Max : static_cast< Int8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int8 ConvTo< Int8 >::From< Uint32 >(Uint32 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint32 >(Max)) ? Max : static_cast< Int8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int8 ConvTo< Int8 >::From< Uint64 >(Uint64 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint64 >(Max)) ? Max : static_cast< Int8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int16 ConvTo< Int16 >::From< Uint8 >(Uint8 v)
|
|
|
|
{
|
|
|
|
return static_cast< Int16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int16 ConvTo< Int16 >::From< Uint16 >(Uint16 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint16 >(Max)) ? Max : static_cast< Int16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int16 ConvTo< Int16 >::From< Uint32 >(Uint32 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint32 >(Max)) ? Max : static_cast< Int16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int16 ConvTo< Int16 >::From< Uint64 >(Uint64 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint64 >(Max)) ? Max : static_cast< Int16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int32 ConvTo< Int32 >::From< Uint8 >(Uint8 v)
|
|
|
|
{
|
|
|
|
return static_cast< Int32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int32 ConvTo< Int32 >::From< Uint16 >(Uint16 v)
|
|
|
|
{
|
|
|
|
return static_cast< Int32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int32 ConvTo< Int32 >::From< Uint32 >(Uint32 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint32 >(Max)) ? Max : static_cast< Int32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int32 ConvTo< Int32 >::From< Uint64 >(Uint64 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint64 >(Max)) ? Max : static_cast< Int32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint8 ConvTo< Uint8 >::From< Int8 >(Int8 v)
|
|
|
|
{
|
|
|
|
return (v <= 0) ? 0 : static_cast< Uint8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint8 ConvTo< Uint8 >::From< Int16 >(Int16 v)
|
|
|
|
{
|
|
|
|
if (v <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (v >= static_cast< Int16 >(Max))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint8 ConvTo< Uint8 >::From< Int32 >(Int32 v)
|
|
|
|
{
|
|
|
|
if (v <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (v >= static_cast< Int32 >(Max))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint8 ConvTo< Uint8 >::From< Int64 >(Int64 v)
|
|
|
|
{
|
|
|
|
if (v <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (v >= static_cast< Int64 >(Max))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint16 ConvTo< Uint16 >::From< Int8 >(Int8 v)
|
|
|
|
{
|
|
|
|
return (v <= 0) ? 0 : static_cast< Uint16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint16 ConvTo< Uint16 >::From< Int16 >(Int16 v)
|
|
|
|
{
|
|
|
|
return (v <= 0) ? 0 : static_cast< Uint16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint16 ConvTo< Uint16 >::From< Int32 >(Int32 v)
|
|
|
|
{
|
|
|
|
if (v <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (v >= static_cast< Int32 >(Max))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint16 ConvTo< Uint16 >::From< Int64 >(Int64 v)
|
|
|
|
{
|
|
|
|
if (v <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (v >= static_cast< Int64 >(Max))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint32 ConvTo< Uint32 >::From< Int8 >(Int8 v)
|
|
|
|
{
|
|
|
|
return (v <= 0) ? 0 : static_cast< Uint32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint32 ConvTo< Uint32 >::From< Int16 >(Int16 v)
|
|
|
|
{
|
|
|
|
return (v <= 0) ? 0 : static_cast< Uint32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint32 ConvTo< Uint32 >::From< Int32 >(Int32 v)
|
|
|
|
{
|
|
|
|
return (v <= 0) ? 0 : static_cast< Uint32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint32 ConvTo< Uint32 >::From< Int64 >(Int64 v)
|
|
|
|
{
|
|
|
|
if (v <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (v >= static_cast< Int64 >(Max))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int8 ConvTo< Int8 >::From< Float32 >(Float32 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float32 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float32 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Int8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int16 ConvTo< Int16 >::From< Float32 >(Float32 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float32 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float32 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Int16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int32 ConvTo< Int32 >::From< Float32 >(Float32 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float32 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float32 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Int32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int8 ConvTo< Int8 >::From< Float64 >(Float64 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float64 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float64 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Int8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int16 ConvTo< Int16 >::From< Float64 >(Float64 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float64 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float64 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Int16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Int32 ConvTo< Int32 >::From< Float64 >(Float64 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float64 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float64 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Int32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint8 ConvTo< Uint8 >::From< Float32 >(Float32 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float32 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float32 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint16 ConvTo< Uint16 >::From< Float32 >(Float32 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float32 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float32 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint32 ConvTo< Uint32 >::From< Float32 >(Float32 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float32 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float32 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint8 ConvTo< Uint8 >::From< Float64 >(Float64 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float64 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float64 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint8 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint16 ConvTo< Uint16 >::From< Float64 >(Float64 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float64 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float64 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint16 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> template <> inline Uint32 ConvTo< Uint32 >::From< Float64 >(Float64 v)
|
|
|
|
{
|
|
|
|
if (EpsLt(v, static_cast< Float64 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
else if (EpsGt(v, static_cast< Float64 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
return static_cast< Uint32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Convert other numeric values to signed long long integer.
|
|
|
|
*/
|
|
|
|
template <> struct ConvTo< Int64 >
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
static constexpr Int64 Min = std::numeric_limits< Int64 >::min();
|
|
|
|
static constexpr Int64 Max = std::numeric_limits< Int64 >::max();
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
template < typename T > static inline Int64 From(T v)
|
|
|
|
{
|
|
|
|
return static_cast< Int64 >(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline Int64 ConvTo< Int64 >::From< Uint64 >(Uint64 v)
|
|
|
|
{
|
|
|
|
return (v >= static_cast< Uint64 >(Max)) ? Max : static_cast< Int64 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Convert other numeric values to unsigned long long integer.
|
|
|
|
*/
|
|
|
|
template <> struct ConvTo< Uint64 >
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
static constexpr Uint64 Min = std::numeric_limits< Uint64 >::min();
|
|
|
|
static constexpr Uint64 Max = std::numeric_limits< Uint64 >::max();
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
template < typename T > static inline Uint64 From(T v)
|
|
|
|
{
|
|
|
|
return (v <= static_cast< T >(0)) ? 0 : static_cast< Uint64 >(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline Uint64 ConvTo< Uint64 >::From< Float32 >(Float32 v)
|
|
|
|
{
|
|
|
|
return From(ConvTo< Int64 >::From(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline Uint64 ConvTo< Uint64 >::From< Float64 >(Float64 v)
|
|
|
|
{
|
|
|
|
return From(ConvTo< Int64 >::From(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Convert other numeric values to a floating point value.
|
|
|
|
*/
|
|
|
|
template <> struct ConvTo< Float32 >
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
static constexpr Float32 Min = std::numeric_limits< Float32 >::min();
|
|
|
|
static constexpr Float32 Max = std::numeric_limits< Float32 >::max();
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
template < typename T > static inline Float32 From(T v)
|
|
|
|
{
|
|
|
|
return static_cast< Float32 >(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline Float32 ConvTo< Float32 >::From< Float64 >(Float64 v)
|
|
|
|
{
|
|
|
|
if (EpsGt(v, static_cast< Float64 >(Max)))
|
|
|
|
{
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
else if (EpsLt(v, static_cast< Float64 >(Min)))
|
|
|
|
{
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
return static_cast< Float32 >(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Convert other numeric values to a double floating point value.
|
|
|
|
*/
|
|
|
|
template <> struct ConvTo< Float64 >
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
static constexpr Float64 Min = std::numeric_limits< Float64 >::min();
|
|
|
|
static constexpr Float64 Max = std::numeric_limits< Float64 >::max();
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
template < typename T > static inline Float64 From(T v)
|
|
|
|
{
|
|
|
|
return static_cast< Float64 >(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Force a value to be within a certain range.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
template< typename T > inline T Clamp(T val, T min, T max)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-04-14 03:08:06 +03:00
|
|
|
// Is the specified value bellow the minimum?
|
|
|
|
if (val < min)
|
|
|
|
{
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
// Is the specified value above the maximum?
|
|
|
|
else if (val > max)
|
|
|
|
{
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
// Return the value as is
|
|
|
|
return val;
|
2016-02-21 00:25:00 +02:00
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-03-26 18:14:47 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-04-14 03:08:06 +03:00
|
|
|
* Force a value to be within a certain range.
|
|
|
|
*/
|
|
|
|
template<> inline Float32 Clamp(Float32 val, Float32 min, Float32 max)
|
|
|
|
{
|
|
|
|
// Is the specified value bellow the minimum?
|
|
|
|
if (EpsLt(val, min))
|
|
|
|
{
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
// Is the specified value above the maximum?
|
|
|
|
else if (EpsGt(val, max))
|
|
|
|
{
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
// Return the value as is
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Force a value to be within a certain range.
|
2016-03-26 18:14:47 +02:00
|
|
|
*/
|
2016-04-14 03:08:06 +03:00
|
|
|
template<> inline Float64 Clamp(Float64 val, Float64 min, Float64 max)
|
2016-03-26 18:14:47 +02:00
|
|
|
{
|
|
|
|
// Is the specified value bellow the minimum?
|
2016-04-14 03:08:06 +03:00
|
|
|
if (EpsLt(val, min))
|
2016-03-26 18:14:47 +02:00
|
|
|
{
|
2016-04-14 03:08:06 +03:00
|
|
|
return min;
|
2016-03-26 18:14:47 +02:00
|
|
|
}
|
|
|
|
// Is the specified value above the maximum?
|
2016-04-14 03:08:06 +03:00
|
|
|
else if (EpsGt(val, max))
|
2016-03-26 18:14:47 +02:00
|
|
|
{
|
2016-04-14 03:08:06 +03:00
|
|
|
return max;
|
2016-03-26 18:14:47 +02:00
|
|
|
}
|
|
|
|
// Return the value as is
|
2016-04-14 03:08:06 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Force a value to be the boundaries of the specified type.
|
|
|
|
*/
|
|
|
|
template < typename T, typename U > inline U ClampL(T v)
|
|
|
|
{
|
|
|
|
return ConvTo< U >::From(v);
|
2016-03-26 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:56:11 +03:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-02-21 00:25:00 +02:00
|
|
|
* Compute the next power of two for the specified number.
|
2015-09-30 03:56:11 +03:00
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
inline Uint32 NextPow2(Uint32 num)
|
2015-09-30 03:56:11 +03:00
|
|
|
{
|
2016-02-21 00:25:00 +02:00
|
|
|
--num;
|
|
|
|
num |= num >> 1;
|
|
|
|
num |= num >> 2;
|
|
|
|
num |= num >> 4;
|
|
|
|
num |= num >> 8;
|
|
|
|
num |= num >> 16;
|
|
|
|
return ++num;
|
|
|
|
}
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-02-27 11:57:10 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Output a message only if the _DEBUG was defined.
|
|
|
|
*/
|
|
|
|
void OutputDebug(const char * msg, ...);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Output a formatted user message to the console.
|
|
|
|
*/
|
|
|
|
void OutputMessage(const char * msg, ...);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Output a formatted error message to the console.
|
|
|
|
*/
|
|
|
|
void OutputError(const char * msg, ...);
|
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve a reference to a null script object.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
Object & NullObject();
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve a reference to a null/empty script array.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
Array & NullArray();
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve a reference to a null script function.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
Function & NullFunction();
|
2015-09-30 03:56:11 +03:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-02-21 00:25:00 +02:00
|
|
|
* Simple function to check whether the specified string can be considered as a boolean value
|
2015-09-30 03:56:11 +03:00
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
bool SToB(CSStr str);
|
2015-09-30 03:56:11 +03:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-03-10 05:57:13 +02:00
|
|
|
* Generate a formatted string and throw it as a sqrat exception.
|
|
|
|
*/
|
|
|
|
void SqThrowF(CCStr fmt, ...);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Quickly generate a formatted string on a small static buffer without any memory allocations.
|
2015-09-30 03:56:11 +03:00
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
CSStr ToStrF(CCStr fmt, ...);
|
2015-09-30 03:56:11 +03:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-03-10 05:57:13 +02:00
|
|
|
* Generate a formatted string on a temporary buffer and return the string but not the buffer.
|
2015-09-30 03:56:11 +03:00
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
CSStr ToStringF(CCStr fmt, ...);
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-03-10 05:57:13 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Obtain a randomly chosen color from a list of known colors.
|
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
const Color3 & GetRandomColor();
|
2015-09-30 03:56:11 +03:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-03-10 05:57:13 +02:00
|
|
|
* Attempt to identify the color in the specified name and return it.
|
2015-09-30 03:56:11 +03:00
|
|
|
*/
|
2016-02-21 00:25:00 +02:00
|
|
|
Color3 GetColor(CSStr name);
|
2016-03-10 05:57:13 +02:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Forward declarations of the logging functions to avoid including the logger everywhere.
|
2016-03-21 22:37:58 +02:00
|
|
|
* Primary logging functions.
|
2016-03-10 05:57:13 +02:00
|
|
|
*/
|
|
|
|
void LogDbg(CCStr fmt, ...);
|
|
|
|
void LogUsr(CCStr fmt, ...);
|
|
|
|
void LogScs(CCStr fmt, ...);
|
|
|
|
void LogInf(CCStr fmt, ...);
|
|
|
|
void LogWrn(CCStr fmt, ...);
|
|
|
|
void LogErr(CCStr fmt, ...);
|
|
|
|
void LogFtl(CCStr fmt, ...);
|
|
|
|
|
2016-03-21 22:37:58 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Forward declarations of the logging functions to avoid including the logger everywhere.
|
|
|
|
* Secondary logging functions.
|
|
|
|
*/
|
2016-03-10 05:57:13 +02:00
|
|
|
void LogSDbg(CCStr fmt, ...);
|
|
|
|
void LogSUsr(CCStr fmt, ...);
|
|
|
|
void LogSScs(CCStr fmt, ...);
|
|
|
|
void LogSInf(CCStr fmt, ...);
|
|
|
|
void LogSWrn(CCStr fmt, ...);
|
|
|
|
void LogSErr(CCStr fmt, ...);
|
|
|
|
void LogSFtl(CCStr fmt, ...);
|
|
|
|
|
2016-03-21 22:37:58 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Forward declarations of the logging functions to avoid including the logger everywhere.
|
|
|
|
* Primary conditional logging functions.
|
|
|
|
*/
|
2016-03-10 05:57:13 +02:00
|
|
|
bool cLogDbg(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogUsr(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogScs(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogInf(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogWrn(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogErr(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogFtl(bool cond, CCStr fmt, ...);
|
|
|
|
|
2016-03-21 22:37:58 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Forward declarations of the logging functions to avoid including the logger everywhere.
|
|
|
|
* Secondary conditional logging functions.
|
|
|
|
*/
|
2016-03-10 05:57:13 +02:00
|
|
|
bool cLogSDbg(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogSUsr(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogSScs(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogSInf(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogSWrn(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogSErr(bool cond, CCStr fmt, ...);
|
|
|
|
bool cLogSFtl(bool cond, CCStr fmt, ...);
|
2015-09-30 03:56:11 +03:00
|
|
|
|
2016-03-29 05:39:17 +03:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Helper class allows the use of functions with ctype style as predicate for algorithms.
|
|
|
|
*/
|
|
|
|
struct IsCType
|
|
|
|
{
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
typedef int (*CTypeFn)(int); // The signature of a ctype function.
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CTypeFn m_Fn; // Pointer to the actual function that does the comparison.
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Base constructor.
|
|
|
|
*/
|
|
|
|
IsCType(CTypeFn fn)
|
|
|
|
: m_Fn(fn)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Function call operator.
|
|
|
|
*/
|
|
|
|
template < typename T > bool operator () (T c)
|
|
|
|
{
|
|
|
|
return (m_Fn(static_cast< int >(c)) != 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Helper class allows the use of functions with ctype style as predicate for algorithms.
|
|
|
|
*/
|
|
|
|
struct IsNotCType
|
|
|
|
{
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
typedef int (*CTypeFn)(int); // The signature of a ctype function.
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CTypeFn m_Fn; // Pointer to the actual function that does the comparison.
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Base constructor.
|
|
|
|
*/
|
|
|
|
IsNotCType(CTypeFn fn)
|
|
|
|
: m_Fn(fn)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Function call operator.
|
|
|
|
*/
|
|
|
|
template < typename T > bool operator () (T c)
|
|
|
|
{
|
|
|
|
return (m_Fn(static_cast< int >(c)) == 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-30 03:56:11 +03:00
|
|
|
} // Namespace:: SqMod
|
|
|
|
|
|
|
|
#endif // _BASE_SHARED_HPP_
|