When using opImplConv to bool and then checking an instance of the type in a ternary operator condition a compiler error is given:
Expression must be of boolean type
It does work when used in an if statement, so this seems like an oversight.
Registration code:
class Scheduler
{
public:
struct ScheduledFunction {};
struct ScheduledFunctionHandle
{
ScheduledFunction* Function{};
std::chrono::system_clock::time_point CreationTime{};
};
};
static void ConstructScheduledFunctionHandle(void* memory)
{
new (memory) Scheduler::ScheduledFunctionHandle();
}
static void DestructScheduledFunctionHandle(Scheduler::ScheduledFunctionHandle* handle)
{
handle->~ScheduledFunctionHandle();
}
static void CopyConstructScheduledFunctionHandle(void* memory, const Scheduler::ScheduledFunctionHandle& other)
{
new (memory) Scheduler::ScheduledFunctionHandle(other);
}
static bool ScheduledFunction_opImplConv(const Scheduler::ScheduledFunctionHandle* handle)
{
return handle->Function != nullptr;
}
static void RegisterScheduledFunction(asIScriptEngine& engine)
{
const char* const name = "ScheduledFunction";
engine.RegisterObjectType(name, sizeof(Scheduler::ScheduledFunctionHandle), asOBJ_VALUE | asGetTypeTraits<Scheduler::ScheduledFunctionHandle>());
engine.RegisterObjectBehaviour(name, asBEHAVE_CONSTRUCT, "void f()",
asFUNCTION(ConstructScheduledFunctionHandle), asCALL_CDECL_OBJFIRST);
engine.RegisterObjectBehaviour(name, asBEHAVE_DESTRUCT, "void f()",
asFUNCTION(DestructScheduledFunctionHandle), asCALL_CDECL_OBJFIRST);
engine.RegisterObjectBehaviour(name, asBEHAVE_CONSTRUCT, "void f(const ScheduledFunction& in other)",
asFUNCTION(CopyConstructScheduledFunctionHandle), asCALL_CDECL_OBJFIRST);
engine.RegisterObjectMethod(name, "ScheduledFunction& opAssign(const ScheduledFunction& in other)",
asMETHODPR(Scheduler::ScheduledFunctionHandle, operator=, (const Scheduler::ScheduledFunctionHandle&), Scheduler::ScheduledFunctionHandle&),
asCALL_THISCALL);
engine.RegisterObjectMethod(name, "bool opImplConv() const",
asFUNCTION(ScheduledFunction_opImplConv), asCALL_CDECL_OBJFIRST);
}
Script code:
ScheduledFunction sched;
Print("Scheduled function handle is valid: " + sched ? "yes" : "no" + "\n");