[*] Split auTryConstruct

This commit is contained in:
Reece Wilson 2022-03-25 15:11:50 +00:00
parent 8713d2706e
commit edaa7a295a
5 changed files with 94 additions and 77 deletions

View File

@ -7,69 +7,15 @@
***/
#pragma once
template<typename T, typename ... Args>
inline T AuTryConstruct(AuCtorCode_t &status, Args&& ... args)
struct AuCtorCode_t
{
if constexpr (AuIsConstructible<T, bool &, Args ...>::type::value)
{
status = AuCtorCode_t::Failed();
return T(status, AuForward<T>(args)...);
}
else
{
try
{
status = AuCtorCode_t::Success();
return T(AuForward<T>(args)...);
}
catch (...)
{
status = AuCtorCode_t::Failed();
return T();
}
}
}
bool value;
template<typename T, typename ... Args>
inline AuPair<AuCtorCode_t, T> AuTryConstructPair(Args&& ... args)
{
AuCtorCode_t code;
T object = AuTryConstruct<T, Args...>(AuReference(code), AuForward<Args>(args)...);
return AuMakePair(AuMove(code), AuMove(object));
}
operator bool() const
{
return value;
}
template<typename T, typename ... Args>
inline T AuTryConstructWithDefault(AuCtorCode_t &status, T &&def, Args&& ... args)
{
if constexpr (AuIsConstructible<T, bool &, Args ...>::type::value)
{
status = AuCtorCode_t::Failed();
T returnValue = T(status, AuForward<T>(args)...);
if (!status)
{
return AuMove(def);
}
return returnValue;
}
else
{
try
{
status = AuCtorCode_t::Success();
return T(AuForward<T>(args)...);
}
catch (...)
{
status = AuCtorCode_t::Failed();
return AuMove(def);
}
}
}
template<typename T, typename ... Args>
inline AuPair<AuCtorCode_t, T> AuTryConstructWithDefaultPair(T &&def, Args&& ... args)
{
AuCtorCode_t code;
T object = AuTryConstructWithDefault<T, Args...>(AuReference(code), AuFoward(def), AuForward<Args>(args)...);
return AuMakePair(AuMove(code), AuMove(object));
}
static constexpr AuCtorCode_t Failed () { return AuCtorCode_t {false}; }
static constexpr AuCtorCode_t Success() { return AuCtorCode_t {true}; }
};

View File

@ -0,0 +1,75 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: auTryConstruct.hpp
Date: 2022-3-25
Author: Reece
***/
#pragma once
template<typename T, typename ... Args>
inline T AuTryConstruct(AuCtorCode_t &status, Args&& ... args)
{
if constexpr (AuIsConstructible<T, bool &, Args ...>::type::value)
{
status = AuCtorCode_t::Failed();
return T(status, AuForward<T>(args)...);
}
else
{
try
{
status = AuCtorCode_t::Success();
return T(AuForward<T>(args)...);
}
catch (...)
{
status = AuCtorCode_t::Failed();
return T();
}
}
}
template<typename T, typename ... Args>
inline AuPair<AuCtorCode_t, T> AuTryConstructPair(Args&& ... args)
{
AuCtorCode_t code;
T object = AuTryConstruct<T, Args...>(AuReference(code), AuForward<Args>(args)...);
return AuMakePair(AuMove(code), AuMove(object));
}
template<typename T, typename ... Args>
inline T AuTryConstructWithDefault(AuCtorCode_t &status, T &&def, Args&& ... args)
{
if constexpr (AuIsConstructible<T, bool &, Args ...>::type::value)
{
status = AuCtorCode_t::Failed();
T returnValue = T(status, AuForward<T>(args)...);
if (!status)
{
return AuMove(def);
}
return returnValue;
}
else
{
try
{
status = AuCtorCode_t::Success();
return T(AuForward<T>(args)...);
}
catch (...)
{
status = AuCtorCode_t::Failed();
return AuMove(def);
}
}
}
template<typename T, typename ... Args>
inline AuPair<AuCtorCode_t, T> AuTryConstructWithDefaultPair(T &&def, Args&& ... args)
{
AuCtorCode_t code;
T object = AuTryConstructWithDefault<T, Args...>(AuReference(code), AuFoward(def), AuForward<Args>(args)...);
return AuMakePair(AuMove(code), AuMove(object));
}

View File

@ -7,6 +7,15 @@
***/
#pragma once
/**
It is not unusual for one to desire a unit-conversion constant when the developer understands
[*] FPU isn't available
[*] Can be done quickly without a framework
[*] Using a framework to normalize to a base unit, apply a translation, and return the desired reuslt is considered overkill
These functions are not to be converted into such a convoluted templated potentially-constexpr castable mess
The intention is to keep these utility functions as simple as the dumb operations they implement
*/
template<class Inch, class Precision = Inch, class Feet>
constexpr const Inch AuFeetToInch(Feet feet)
{

View File

@ -26,6 +26,7 @@
#endif
#include <auROXTL/auTypes.hpp>
#include <auROXTL/auTryConstruct.hpp>
#include <auROXTL/auTemplateMeta.hpp>
#include <auROXTL/auResult.hpp>
#include <auROXTL/auMemoryModel.hpp>

View File

@ -27,6 +27,7 @@
#include <nmmintrin.h>
#endif
#include <auROXTL/auMemory.hpp>
#include <auROXTL/auCopyMoveUtils.hpp>
#include <auROXTL/auContainerUtils.hpp>
@ -44,22 +45,7 @@
#include <auROXTL/auUnitUtils.hpp>
#include <auROXTL/auFNV1Utils.hpp>
#include <auROXTL/auHashUtils.hpp>
template<typename T, typename ... Args>
inline T AuTryConstruct(bool &status, Args&& ... args)
{
T t;
try
{
t = T(AuForward<T>(args)...);
status = true;
}
catch (...)
{
status = false;
}
return t;
}
#include <auROXTL/auTryConstructUtils.hpp>
struct IAuNullDelegate