1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Separate worker and parameter to their own sources.

This commit is contained in:
Sandu Liviu Catalin 2020-09-06 21:45:32 +03:00
parent 31af9efe94
commit 38b874f444
8 changed files with 532 additions and 511 deletions

View File

@ -42,7 +42,6 @@ add_library(SqModule MODULE
Library/IO.cpp Library/IO.hpp
Library/IO/File.cpp Library/IO/File.hpp
Library/IO/INI.cpp Library/IO/INI.hpp
Library/Job.cpp Library/Job.hpp
Library/MMDB.cpp Library/MMDB.hpp
Library/Numeric.cpp Library/Numeric.hpp
Library/Numeric/LongInt.cpp Library/Numeric/LongInt.hpp
@ -59,6 +58,8 @@ add_library(SqModule MODULE
Library/Utils/Buffer.cpp Library/Utils/Buffer.hpp
Library/Web.cpp Library/Web.hpp
Library/Worker.cpp Library/Worker.hpp
Library/Worker/Job.cpp Library/Worker/Job.hpp
Library/Worker/Parameter.cpp Library/Worker/Parameter.hpp
Library/XML.cpp Library/XML.hpp
Misc/Broadcast.cpp
Misc/Constants.cpp

View File

@ -1,11 +0,0 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
} // Namespace:: SqMod

View File

@ -10,207 +10,6 @@ SQMODE_DECL_TYPENAME(SqWorker, _SC("SqWorker"))
// ------------------------------------------------------------------------------------------------
Worker::Container Worker::sm_Workers{};
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(CCStr str)
: Parameter()
{
// Make sure there's a string
if (str)
{
mSize = static_cast< uint32_t >(std::strlen(str));
mString = new SQChar[mSize+1];
std::strcpy(mString, str);
}
// Even an empty string should still be marked as a string
mType = T_STRING;
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(CCStr str, size_t len)
: Parameter()
{
// Make sure there's a string
if (str && len)
{
mSize = ConvTo< uint32_t >::From(len);
mString = new SQChar[mSize+1];
std::strncpy(mString, str, mSize);
mString[mSize] = '\0'; // Null terminator
}
// Even an empty string should still be marked as a string
mType = T_STRING;
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(ArrayType && v)
: mType(T_ARRAY), mSize(static_cast< uint32_t >(v.size()))
, mArray(new ArrayType(std::forward< ArrayType >(v)))
{
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(const ArrayType & v)
: mType(T_ARRAY), mSize(static_cast< uint32_t >(v.size()))
, mArray(new ArrayType(v))
{
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(TableType && v)
: mType(T_ARRAY), mSize(static_cast< uint32_t >(v.size()))
, mTable(new TableType(std::forward< TableType >(v)))
{
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(const TableType & v)
: mType(T_ARRAY), mSize(static_cast< uint32_t >(v.size()))
, mTable(new TableType(v))
{
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(const Parameter & o)
: mType(o.mType), mSize(o.mSize), mData(o.mData)
{
// Identify the type to be copied
switch (mType)
{
// Fundamental types can be copied bit-wise (which we did)
case T_NULL:
case T_INT:
case T_BOOL:
case T_FLOAT: break;
case T_STRING:
if (mSize)
{
mString = new SQChar[mSize];
std::strncpy(mString, o.mString, mSize);
mString[mSize] = '\0'; // Null terminator
}
else
{
mString = nullptr; // Empty string?
}
break;
case T_ARRAY:
mArray = o.mArray ? new ArrayType(*o.mArray) : nullptr;
break;
case T_TABLE:
mTable = o.mTable ? new TableType(*o.mTable) : nullptr;
break;
// How did we get here?
default: break;
}
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(Parameter && o)
: mType(o.mType), mSize(o.mSize), mData(o.mData)
{
o.Discard(); // Take ownership
}
// ------------------------------------------------------------------------------------------------
bool Parameter::operator == (const Parameter & o) const noexcept
{
// If they're not the same type then there's no point in comparing
if (mType != o.mType)
{
return false;
}
// Identify which type to compare
switch (mType)
{
// Null is same regardless
case T_NULL: return true;
// Boolean is stored as integer
case T_INT:
case T_BOOL: return (mInt == o.mInt);
// Take into account precision errors
case T_FLOAT: return EpsEq(mFloat, o.mFloat);
case T_STRING:
// Only perform a comparison if there's actually a string to compare
if (mSize && mSize == o.mSize)
{
return std::strncmp(mString, o.mString, mSize) == 0;
}
else
{
return false; // If they're not the same size then they can't be the same
}
// For table or arrays we only test if they're the same rather then each value individually
case T_ARRAY: return (mArray == o.mArray);
case T_TABLE: return (mTable == o.mTable);
// How did we get here?
default: return false;
}
}
// ------------------------------------------------------------------------------------------------
void Parameter::Assign(const Parameter & o)
{
// Avoid self assignment
if (this == &o) return;
/* We could probably optimize this by reusing current container memory.
* But chances are we would complicate code for the simpler case.
* And the simpler case is likely to be the more common scenario.
*/
// Discard current information
Clear();
// The size and type are copied bit-wise
mType = o.mType;
mSize = o.mSize;
// Identify the type to be copied
switch (mType)
{
// Fundamental types can be copied bit-wise
case T_NULL:
case T_INT:
case T_BOOL:
case T_FLOAT:
mData = o.mData;
break;
// Strings require memory to be allocated
case T_STRING:
if (mSize)
{
mString = new SQChar[mSize];
std::strncpy(mString, o.mString, mSize);
mString[mSize] = '\0'; // Null terminator
}
else
{
mString = nullptr; // Empty string?
}
break;
case T_ARRAY:
mArray = o.mArray ? new ArrayType(*o.mArray) : nullptr;
break;
case T_TABLE:
mTable = o.mTable ? new TableType(*o.mTable) : nullptr;
break;
// How did we get here?
default: break;
}
}
// ------------------------------------------------------------------------------------------------
void Parameter::Assign(Parameter && o)
{
// Avoid self assignment
if (this == &o) return;
// Discard current information
Clear();
// We don't care about the type since we take ownership
mType = o.mType;
mSize = o.mSize;
mData = o.mData;
// Take ownership
o.Discard();
}
// ------------------------------------------------------------------------------------------------
void Parameter::Clear()
{
switch (mType)
{
case T_STRING: delete[] mString; break;
case T_ARRAY: delete mArray; break;
case T_TABLE: delete mTable; break;
default: break;
}
}
// ------------------------------------------------------------------------------------------------
Thread::Thread( Worker * worker)
: mWorker(worker)

View File

@ -3,6 +3,7 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
#include "Base/VecMap.hpp"
#include "Library/Worker/Job.hpp"
// ------------------------------------------------------------------------------------------------
#include <thread>
@ -23,303 +24,6 @@ using namespace moodycamel;
// ------------------------------------------------------------------------------------------------
struct Worker;
/* ------------------------------------------------------------------------------------------------
* Used to transmit values between workers in a language agnostic way.
*/
struct Parameter
{
enum
{
T_NULL=0, // Null/undefined type.
T_INT, // Integer type.
T_BOOL, // Boolean type.
T_FLOAT, // Floating point type.
T_STRING, // String type.
T_ARRAY, // Array type.
T_TABLE // Table type.
};
// --------------------------------------------------------------------------------------------
using ArrayType = std::vector< Parameter >; // Parameter array.
using TableType = VecMap< Parameter, Parameter >; // Parameter table.
// --------------------------------------------------------------------------------------------
uint32_t mType; // Type of value stored in the parameter.
uint32_t mSize; // Container size. Mostly used for the string because there's space from padding.
/* --------------------------------------------------------------------------------------------
*
*/
union {
int64_t mInt; // Parameter value represented as integer.
uint64_t mData; // Parameter value represented as raw bits.
double mFloat; // Parameter value represented as floating point.
CStr mString; // Parameter value represented as string pointer.
ArrayType * mArray; // Parameter value represented as array pointer.
TableType * mTable; // Parameter value represented as table pointer.
};
/* --------------------------------------------------------------------------------------------
* Default constructor (null).
*/
Parameter() noexcept
: Parameter(nullptr)
{
}
/* --------------------------------------------------------------------------------------------
* Null constructor.
*/
explicit Parameter(std::nullptr_t)
: mType(T_NULL), mSize(0), mData(0ull)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(int8_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(uint8_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(int16_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(uint16_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(int32_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(uint32_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(int64_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(uint64_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(static_cast< int64_t >(v))
{
}
/* --------------------------------------------------------------------------------------------
* Boolean constructor.
*/
explicit Parameter(bool SQ_UNUSED_ARG(v)) //static analyzer shat the bed
: mType(T_BOOL), mSize(1), mInt(v ? 1 : 0)
{
}
/* --------------------------------------------------------------------------------------------
* Floating point constructor.
*/
explicit Parameter(float v)
: mType(T_FLOAT), mSize(sizeof(v)), mFloat(v)
{
}
/* --------------------------------------------------------------------------------------------
* Floating point constructor.
*/
explicit Parameter(double v)
: mType(T_FLOAT), mSize(sizeof(v)), mFloat(v)
{
}
/* --------------------------------------------------------------------------------------------
* String constructor.
*/
explicit Parameter(CCStr str);
/* --------------------------------------------------------------------------------------------
* String constructor.
*/
explicit Parameter(CCStr str, size_t len);
/* --------------------------------------------------------------------------------------------
* Array constructor.
*/
explicit Parameter(ArrayType && v);
/* --------------------------------------------------------------------------------------------
* Array constructor.
*/
explicit Parameter(const ArrayType & v);
/* --------------------------------------------------------------------------------------------
* Table constructor.
*/
explicit Parameter(TableType && v);
/* --------------------------------------------------------------------------------------------
* Table constructor.
*/
explicit Parameter(const TableType & v);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Parameter(const Parameter & o);
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Parameter(Parameter && o);
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Parameter()
{
Reset();
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Parameter & operator = (const Parameter & o)
{
Assign(o);
return *this;
}
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Parameter & operator = (Parameter && o)
{
Assign(std::forward< Parameter >(o));
return *this;
}
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Parameter & o) const noexcept;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Parameter & o) const noexcept
{
return !(*this == o);
}
/* --------------------------------------------------------------------------------------------
* Discard any current information and set to null.
*/
void Reset()
{
Clear(nullptr);
}
/* --------------------------------------------------------------------------------------------
* Swap parameter contents.
*/
void Swap(Parameter & o) noexcept
{
std::swap(mType, o.mType);
std::swap(mSize, o.mSize);
std::swap(mData, o.mData);
}
/* --------------------------------------------------------------------------------------------
* Assign a copy of another parameter.
*/
void Assign(const Parameter & o);
/* --------------------------------------------------------------------------------------------
* Assign a ownership of another parameter.
*/
void Assign(Parameter && o);
private:
/* --------------------------------------------------------------------------------------------
* Discard any and all information without performing any release of memory.
*/
void Discard() noexcept
{
mType = T_NULL;
mSize = 0;
mData = 0ull;
}
/* --------------------------------------------------------------------------------------------
* Clear/release any stored value and reset to default. Does not set to null.
*/
void Clear();
/* --------------------------------------------------------------------------------------------
* Clear/release any stored value and reset to default. Specialization which sets to null.
*/
void Clear(std::nullptr_t)
{
Clear(); // Do a regular clear first
Discard(); // Now we can forget about it
}
};
/* ------------------------------------------------------------------------------------------------
* Used to represent a job that a worker must do, as well as a reply from the worker with the result.
*/
struct Job
{
using Parameters = Parameter::ArrayType;
/* --------------------------------------------------------------------------------------------
* Describe the typeof job a worker must do.
*/
enum class Type : uint8_t
{
Null=0, // Null/non existent job.
Stop, // Inform the worker to stop.
Eval, // Inform the worker to evaluate some code.
Exec // Inform the worker to execute a file.
};
// --------------------------------------------------------------------------------------------
Type mType; // Job type.
String mTarget; // Where to perform the job.
String mPayload; // Where to perform the job.
Function mCallback; // Function to call once completed.
Parameter mResponse; // The value given by the worker.
Parameters mParameters; // Job parameters.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Job()
: mType(Type::Null), mTarget(), mPayload(), mCallback(), mResponse(), mParameters()
{
}
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Job(Function && callback)
: mType(Type::Null), mTarget(), mPayload()
, mCallback(std::forward< Function >(callback)), mResponse(), mParameters()
{
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Job(const Job & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Job(Job && o) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Job() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment.
*/
Job & operator = (const Job & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment.
*/
Job & operator = (Job && o) = default;
};
/* ------------------------------------------------------------------------------------------------
* Thread.
*/

View File

@ -1,5 +1,5 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Job.hpp"
#include "Library/Worker/Job.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {

View File

@ -0,0 +1,73 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
#include "Library/Worker/Parameter.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
struct Worker;
/* ------------------------------------------------------------------------------------------------
* Used to represent a job that a worker must do, as well as a reply from the worker with the result.
*/
struct Job
{
using Parameters = Parameter::ArrayType;
/* --------------------------------------------------------------------------------------------
* Describe the typeof job a worker must do.
*/
enum class Type : uint8_t
{
Null=0, // Null/non existent job.
Stop, // Inform the worker to stop.
Eval, // Inform the worker to evaluate some code.
Exec // Inform the worker to execute a file.
};
// --------------------------------------------------------------------------------------------
Type mType; // Job type.
String mTarget; // Where to perform the job.
String mPayload; // Where to perform the job.
Function mCallback; // Function to call once completed.
Parameter mResponse; // The value given by the worker.
Parameters mParameters; // Job parameters.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Job()
: mType(Type::Null), mTarget(), mPayload(), mCallback(), mResponse(), mParameters()
{
}
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Job(Function && callback)
: mType(Type::Null), mTarget(), mPayload()
, mCallback(std::forward< Function >(callback)), mResponse(), mParameters()
{
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Job(const Job & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Job(Job && o) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Job() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment.
*/
Job & operator = (const Job & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment.
*/
Job & operator = (Job && o) = default;
};
} // Namespace:: SqMod

View File

@ -0,0 +1,208 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Worker/Parameter.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(CCStr str)
: Parameter()
{
// Make sure there's a string
if (str)
{
mSize = static_cast< uint32_t >(std::strlen(str));
mString = new SQChar[mSize+1];
std::strcpy(mString, str);
}
// Even an empty string should still be marked as a string
mType = T_STRING;
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(CCStr str, size_t len)
: Parameter()
{
// Make sure there's a string
if (str && len)
{
mSize = ConvTo< uint32_t >::From(len);
mString = new SQChar[mSize+1];
std::strncpy(mString, str, mSize);
mString[mSize] = '\0'; // Null terminator
}
// Even an empty string should still be marked as a string
mType = T_STRING;
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(ArrayType && v)
: mType(T_ARRAY), mSize(static_cast< uint32_t >(v.size()))
, mArray(new ArrayType(std::forward< ArrayType >(v)))
{
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(const ArrayType & v)
: mType(T_ARRAY), mSize(static_cast< uint32_t >(v.size()))
, mArray(new ArrayType(v))
{
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(TableType && v)
: mType(T_ARRAY), mSize(static_cast< uint32_t >(v.size()))
, mTable(new TableType(std::forward< TableType >(v)))
{
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(const TableType & v)
: mType(T_ARRAY), mSize(static_cast< uint32_t >(v.size()))
, mTable(new TableType(v))
{
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(const Parameter & o)
: mType(o.mType), mSize(o.mSize), mData(o.mData)
{
// Identify the type to be copied
switch (mType)
{
// Fundamental types can be copied bit-wise (which we did)
case T_NULL:
case T_INT:
case T_BOOL:
case T_FLOAT: break;
case T_STRING:
if (mSize)
{
mString = new SQChar[mSize];
std::strncpy(mString, o.mString, mSize);
mString[mSize] = '\0'; // Null terminator
}
else
{
mString = nullptr; // Empty string?
}
break;
case T_ARRAY:
mArray = o.mArray ? new ArrayType(*o.mArray) : nullptr;
break;
case T_TABLE:
mTable = o.mTable ? new TableType(*o.mTable) : nullptr;
break;
// How did we get here?
default: break;
}
}
// ------------------------------------------------------------------------------------------------
Parameter::Parameter(Parameter && o)
: mType(o.mType), mSize(o.mSize), mData(o.mData)
{
o.Discard(); // Take ownership
}
// ------------------------------------------------------------------------------------------------
bool Parameter::operator == (const Parameter & o) const noexcept
{
// If they're not the same type then there's no point in comparing
if (mType != o.mType)
{
return false;
}
// Identify which type to compare
switch (mType)
{
// Null is same regardless
case T_NULL: return true;
// Boolean is stored as integer
case T_INT:
case T_BOOL: return (mInt == o.mInt);
// Take into account precision errors
case T_FLOAT: return EpsEq(mFloat, o.mFloat);
case T_STRING:
// Only perform a comparison if there's actually a string to compare
if (mSize && mSize == o.mSize)
{
return std::strncmp(mString, o.mString, mSize) == 0;
}
else
{
return false; // If they're not the same size then they can't be the same
}
// For table or arrays we only test if they're the same rather then each value individually
case T_ARRAY: return (mArray == o.mArray);
case T_TABLE: return (mTable == o.mTable);
// How did we get here?
default: return false;
}
}
// ------------------------------------------------------------------------------------------------
void Parameter::Assign(const Parameter & o)
{
// Avoid self assignment
if (this == &o) return;
/* We could probably optimize this by reusing current container memory.
* But chances are we would complicate code for the simpler case.
* And the simpler case is likely to be the more common scenario.
*/
// Discard current information
Clear();
// The size and type are copied bit-wise
mType = o.mType;
mSize = o.mSize;
// Identify the type to be copied
switch (mType)
{
// Fundamental types can be copied bit-wise
case T_NULL:
case T_INT:
case T_BOOL:
case T_FLOAT:
mData = o.mData;
break;
// Strings require memory to be allocated
case T_STRING:
if (mSize)
{
mString = new SQChar[mSize];
std::strncpy(mString, o.mString, mSize);
mString[mSize] = '\0'; // Null terminator
}
else
{
mString = nullptr; // Empty string?
}
break;
case T_ARRAY:
mArray = o.mArray ? new ArrayType(*o.mArray) : nullptr;
break;
case T_TABLE:
mTable = o.mTable ? new TableType(*o.mTable) : nullptr;
break;
// How did we get here?
default: break;
}
}
// ------------------------------------------------------------------------------------------------
void Parameter::Assign(Parameter && o)
{
// Avoid self assignment
if (this == &o) return;
// Discard current information
Clear();
// We don't care about the type since we take ownership
mType = o.mType;
mSize = o.mSize;
mData = o.mData;
// Take ownership
o.Discard();
}
// ------------------------------------------------------------------------------------------------
void Parameter::Clear()
{
switch (mType)
{
case T_STRING: delete[] mString; break;
case T_ARRAY: delete mArray; break;
case T_TABLE: delete mTable; break;
default: break;
}
}
} // Namespace:: SqMod

View File

@ -0,0 +1,247 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
#include "Base/VecMap.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Used to transmit values between workers in a language agnostic way.
*/
struct Parameter
{
enum
{
T_NULL=0, // Null/undefined type.
T_INT, // Integer type.
T_BOOL, // Boolean type.
T_FLOAT, // Floating point type.
T_STRING, // String type.
T_ARRAY, // Array type.
T_TABLE // Table type.
};
// --------------------------------------------------------------------------------------------
using ArrayType = std::vector< Parameter >; // Parameter array.
using TableType = VecMap< Parameter, Parameter >; // Parameter table.
// --------------------------------------------------------------------------------------------
uint32_t mType; // Type of value stored in the parameter.
uint32_t mSize; // Container size. Mostly used for the string because there's space from padding.
/* --------------------------------------------------------------------------------------------
*
*/
union {
int64_t mInt; // Parameter value represented as integer.
uint64_t mData; // Parameter value represented as raw bits.
double mFloat; // Parameter value represented as floating point.
CStr mString; // Parameter value represented as string pointer.
ArrayType * mArray; // Parameter value represented as array pointer.
TableType * mTable; // Parameter value represented as table pointer.
};
/* --------------------------------------------------------------------------------------------
* Default constructor (null).
*/
Parameter() noexcept
: Parameter(nullptr)
{
}
/* --------------------------------------------------------------------------------------------
* Null constructor.
*/
explicit Parameter(std::nullptr_t)
: mType(T_NULL), mSize(0), mData(0ull)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(int8_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(uint8_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(int16_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(uint16_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(int32_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(uint32_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(int64_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(v)
{
}
/* --------------------------------------------------------------------------------------------
* Integer constructor.
*/
explicit Parameter(uint64_t v)
: mType(T_INT), mSize(sizeof(v)), mInt(static_cast< int64_t >(v))
{
}
/* --------------------------------------------------------------------------------------------
* Boolean constructor.
*/
explicit Parameter(bool SQ_UNUSED_ARG(v)) //static analyzer shat the bed
: mType(T_BOOL), mSize(1), mInt(v ? 1 : 0)
{
}
/* --------------------------------------------------------------------------------------------
* Floating point constructor.
*/
explicit Parameter(float v)
: mType(T_FLOAT), mSize(sizeof(v)), mFloat(v)
{
}
/* --------------------------------------------------------------------------------------------
* Floating point constructor.
*/
explicit Parameter(double v)
: mType(T_FLOAT), mSize(sizeof(v)), mFloat(v)
{
}
/* --------------------------------------------------------------------------------------------
* String constructor.
*/
explicit Parameter(CCStr str);
/* --------------------------------------------------------------------------------------------
* String constructor.
*/
explicit Parameter(CCStr str, size_t len);
/* --------------------------------------------------------------------------------------------
* Array constructor.
*/
explicit Parameter(ArrayType && v);
/* --------------------------------------------------------------------------------------------
* Array constructor.
*/
explicit Parameter(const ArrayType & v);
/* --------------------------------------------------------------------------------------------
* Table constructor.
*/
explicit Parameter(TableType && v);
/* --------------------------------------------------------------------------------------------
* Table constructor.
*/
explicit Parameter(const TableType & v);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Parameter(const Parameter & o);
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Parameter(Parameter && o);
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Parameter()
{
Reset();
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Parameter & operator = (const Parameter & o)
{
Assign(o);
return *this;
}
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Parameter & operator = (Parameter && o)
{
Assign(std::forward< Parameter >(o));
return *this;
}
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Parameter & o) const noexcept;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Parameter & o) const noexcept
{
return !(*this == o);
}
/* --------------------------------------------------------------------------------------------
* Discard any current information and set to null.
*/
void Reset()
{
Clear(nullptr);
}
/* --------------------------------------------------------------------------------------------
* Swap parameter contents.
*/
void Swap(Parameter & o) noexcept
{
std::swap(mType, o.mType);
std::swap(mSize, o.mSize);
std::swap(mData, o.mData);
}
/* --------------------------------------------------------------------------------------------
* Assign a copy of another parameter.
*/
void Assign(const Parameter & o);
/* --------------------------------------------------------------------------------------------
* Assign a ownership of another parameter.
*/
void Assign(Parameter && o);
private:
/* --------------------------------------------------------------------------------------------
* Discard any and all information without performing any release of memory.
*/
void Discard() noexcept
{
mType = T_NULL;
mSize = 0;
mData = 0ull;
}
/* --------------------------------------------------------------------------------------------
* Clear/release any stored value and reset to default. Does not set to null.
*/
void Clear();
/* --------------------------------------------------------------------------------------------
* Clear/release any stored value and reset to default. Specialization which sets to null.
*/
void Clear(std::nullptr_t)
{
Clear(); // Do a regular clear first
Discard(); // Now we can forget about it
}
};
} // Namespace:: SqMod