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

Adjust stream constructor.

This commit is contained in:
Sandu Liviu Catalin 2021-03-09 00:40:16 +02:00
parent 6935d4bfb1
commit 4c08879b5c
2 changed files with 43 additions and 6 deletions

View File

@ -176,7 +176,8 @@ template < class T > struct RegisterStream
mCls
// Constructors
.template Ctor()
.template Ctor< SQInteger >()
.template Ctor< StackStrF & >()
.template Ctor< SQInteger, StackStrF & >()
// Properties
.Prop(_SC("Str"), &T::GetStr, &T::SetStr)
;

View File

@ -210,6 +210,42 @@ protected:
}
};
/* ------------------------------------------------------------------------------------------------
* Helper used to extract the string from the constructor to the proper type.
*/
template < class T > struct StreamConstructorStr
{
static auto Get(StackStrF & str)
{
return str.ToStr();
}
};
/* ------------------------------------------------------------------------------------------------
* Specializations of `StreamConstructorStr` that return the raw string pointer instead,
*/
template < > struct StreamConstructorStr< std::fstream >
{
static const SQChar * Get(StackStrF & str)
{
return str.mPtr;
}
};
template < > struct StreamConstructorStr< std::ofstream >
{
static const SQChar * Get(StackStrF & str)
{
return str.mPtr;
}
};
template < > struct StreamConstructorStr< std::ifstream >
{
static const SQChar * Get(StackStrF & str)
{
return str.mPtr;
}
};
/* ------------------------------------------------------------------------------------------------
* Stream-based input/output class.
*/
@ -217,7 +253,7 @@ template < class T > struct SqStream : public SqStreamStorage< T >
{
using SqStreamStorage< T >::Stream;
// --------------------------------------------------------------------------------------------
using Type = SqStreamStorage< T >::Type;
using Type = typename SqStreamStorage< T >::Type;
/* --------------------------------------------------------------------------------------------
* Default string constructor.
*/
@ -237,16 +273,16 @@ template < class T > struct SqStream : public SqStreamStorage< T >
/* --------------------------------------------------------------------------------------------
* Base file constructor.
*/
explicit SqStream(StackStrF & name)
: SqStreamStorage< T >(name.mPtr), m_Buffer()
explicit SqStream(StackStrF & str)
: SqStreamStorage< T >(StreamConstructorStr< Type >::Get(str)), m_Buffer()
{
}
/* --------------------------------------------------------------------------------------------
* Explicit file constructor.
*/
SqStream(SQInteger m, StackStrF & name)
: SqStreamStorage< T >(name.mPtr, static_cast< std::ios_base::openmode >(m)), m_Buffer()
SqStream(SQInteger m, StackStrF & str)
: SqStreamStorage< T >(StreamConstructorStr< Type >::Get(str), static_cast< std::ios_base::openmode >(m)), m_Buffer()
{
}