#ifndef PLATFORM_H
#define PLATFORM_H
#pragma once
#include "wchartypes.h"
#include "tier0/memdbgoff.h"
#include "tier0/valve_off.h"
#include "minbase/minbase_identify.h"
#include "minbase/minbase_securezeromemory_impl.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <new>
#include <utility>
#include <string.h>
#include <time.h>
#include "minbase/minbase_types.h"
#include "minbase/minbase_decls.h"
#include "minbase/minbase_macros.h"
#include "minbase/minbase_endian.h"
#if IsPosix()
typedef int SOCKET;
#define INVALID_SOCKET (-1)
#else
typedef uintp SOCKET;
#define INVALID_SOCKET (SOCKET)(~0)
#endif
#define UNREACHABLE() { Assert(0); HINT(0); }
#define NO_DEFAULT default: UNREACHABLE();
#include "minbase/minbase_warnings.h"
#include "minbase/minbase_annotations.h"
#include "platformtime.h"
#if IsPosix()
#define _snprintf snprintf
#if !defined( stricmp )
#define stricmp strcasecmp
#endif
#if !defined( _stricmp )
#define _stricmp strcasecmp
#endif
#define _strcmpi strcasecmp
#include <errno.h>
inline int GetLastError() { return errno; }
#endif
#define PLATFORM_INTERFACE extern "C"
PLATFORM_INTERFACE bool Plat_IsInDebugSession();
template <class T>
inline void Construct( T* pMemory )
{
HINT( pMemory != 0 );
::new( pMemory ) T;
}
template <class T>
inline T* ValueInitializeConstruct( T* pMemory )
{
HINT( pMemory != 0 );
return ::new( pMemory ) T{};
}
template <class T, typename... ConstructorArgs>
inline T* Construct( T* pMemory, ConstructorArgs&&... args )
{
HINT( pMemory != 0 );
return ::new( pMemory ) T( std::forward<ConstructorArgs>(args)... );
}
template <class T>
inline void Destruct( T* pMemory )
{
pMemory->~T();
#ifdef _DEBUG
memset( (void*)pMemory, 0xDD, sizeof(T) );
#endif
}
template <typename LambdaType>
class CScopeGuardLambdaImpl
{
public:
explicit CScopeGuardLambdaImpl( LambdaType &&lambda ) : m_lambda( std::move( lambda ) ) {}
~CScopeGuardLambdaImpl()
{
if ( !m_bDismissed )
m_lambda();
}
void Dismiss() { m_bDismissed = true; }
private:
LambdaType m_lambda;
bool m_bDismissed = false;
};
template <typename LambdaType>
CScopeGuardLambdaImpl< LambdaType > MakeScopeGuardLambda( LambdaType &&lambda )
{
return CScopeGuardLambdaImpl< LambdaType >( std::move( lambda ) );
}
#define RunLambdaAtScopeExit2( VarName, ... ) const auto VarName( MakeScopeGuardLambda( __VA_ARGS__ ) ); (void)VarName
#define RunLambdaAtScopeExit( ... ) RunLambdaAtScopeExit2( UNIQUE_ID, __VA_ARGS__ )
#define RunCodeAtScopeExit( ... ) RunLambdaAtScopeExit( [&]() { __VA_ARGS__ ; } )
#endif