0

在 C++ 中,我有如下宏:

#ifdef DEBUG
    #define dbgAssert(condition, message)/
    if(!(condition)){ implementation.Assert(message); }
#else
    #define dbgAssert(condition, message)
#endif

这种方法是有效的,因为如果我们不处于调试模式,特别是当某些条件对 CPU 来说特别繁重时,则永远不会测试条件。

有没有办法在 Haxe 中实现这种类型的 one 班轮?

这是一个非常简单的示例,因为有一些带有十几个条件定义的宏(取决于多个参数)我无法有效地在所有地方维护冗余重新定义。

这是一个稍微有趣的系统,它允许我始终测试最简单的条件并根据级别添加更重的测试:

#if 4 == ASSERT4LEVEL
    #define lvlAssert4(conditionlvl1, conditionlvl2, conditionlvl3, conditionlvl4, message)/
    myAssert((conditionlvl1) && (conditionlvl2) && (conditionlvl3) && (conditionlvl4), message)
#elif 3 == ASSERT4LEVEL 
    #define lvlAssert4(conditionlvl1, conditionlvl2, conditionlvl3, conditionlvl4, message)/
    myAssert((conditionlvl1) && (conditionlvl2) && (conditionlvl3), message)
#elif 2 == ASSERT4LEVEL 
    #define lvlAssert4(conditionlvl1, conditionlvl2, conditionlvl3, conditionlvl4, message)/
    myAssert((conditionlvl1) && (conditionlvl2), message)
#else
    #define lvlAssert4(conditionlvl1, conditionlvl2, conditionlvl3, conditionlvl4, message)/
    myAssert((conditionlvl1), message)
#endif

如何在不执行条件的情况下复制此行为?

4

2 回答 2

3

您可以根据调试标志创建 2 个不同的函数。release 函数应该是内联的,并且应该什么都不做。

    class Debug {
        #if debug
        public static function assert (e:Bool) {
            if (!e) throw "assert";
        }
        #else
        public static inline function assert (e:Bool) {
            return;
        }
        #end

    }

您还应该看看 haxe 宏,它可以在编译期间做很多强大的事情。

于 2013-02-08T12:32:48.167 回答
2

对于这种情况,Frabbits 的答案是正确的,但这里也是宏版本。

@:macro static public function dbgAssert(condition, message) {
     #if debug
     return macro if (!($condition)) { implementation.Assert($message); }
     #else
     return macro {};
     #end
}
于 2013-02-09T00:19:55.110 回答