Java 替换 C 宏
最近,我将第三方哈希函数的代码从 C++ 重构为 C。这个过程相对轻松,只有一些值得注意的更改。现在我想用 Java 编写相同的函数,但遇到了一个小问题。
在 C/C++ 代码中,有一个 C 预处理器宏,它将一些整数变量名称作为参数,并对其内容和一些常量执行一系列按位运算。该宏在多个不同的地方使用,因此它的存在避免了相当多的代码重复。
然而,在 Java 中,没有与 C 预处理器相当的东西。也没有办法影响作为参数传递给方法的任何基本类型 - 即使自动装箱也会产生不可变的对象。再加上Java方法返回单个值,我似乎无法找到重写宏的简单方法。
我考虑的途径:
在任何地方手动扩展宏:它会起作用,但从长远来看,代码重复可能会让事情变得有趣。
编写一个返回数组的方法:这也可以,但它会重复生成如下代码:
long tmp[] = bitops(k, l, m, x, y, z); k = tmp[0]; l = tmp[1]; m = tmp[2]; x = tmp[3]; y = tmp[4]; z = tmp[5];
编写一个采用数组作为参数:这意味着所有变量名称都将简化为数组元素引用 - 跟踪哪个索引对应于哪个变量将相当困难。
创建一个单独的类,例如具有适当类型的公共字段的
State
,并将其用作方法的参数:这是我当前的解决方案。它允许该方法更改变量,同时仍然保留它们的名称。然而,它的缺点是,随着添加更多的宏和变量,State
类将变得越来越复杂,以避免在不同的State
之间来回复制值。 code> 对象。
您将如何用 Java 重写这样的 C 宏?是否有更合适的方法来处理这个问题,使用标准 Java 6 开发工具包提供的设施(即没有第 3 方库或单独的预处理器)?
Recently I refactored the code of a 3rd party hash function from C++ to C. The process was relatively painless, with only a few changes of note. Now I want to write the same function in Java and I came upon a slight issue.
In the C/C++ code there is a C preprocessor macro that takes a few integer variables names as arguments and performs a bunch of bitwise operations with their contents and a few constants. That macro is used in several different places, therefore its presence avoids a fair bit of code duplication.
In Java, however, there is no equivalent for the C preprocessor. There is also no way to affect any basic type passed as an argument to a method - even autoboxing produces immutable objects. Coupled with the fact that Java methods return a single value, I can't seem to find a simple way to rewrite the macro.
Avenues that I considered:
Expand the macro by hand everywhere: It would work, but the code duplication could make things interesting in the long run.
Write a method that returns an array: This would also work, but it would repeatedly result into code like this:
long tmp[] = bitops(k, l, m, x, y, z); k = tmp[0]; l = tmp[1]; m = tmp[2]; x = tmp[3]; y = tmp[4]; z = tmp[5];
Write a method that takes an array as an argument: This would mean that all variable names would be reduced to array element references - it would be rather hard to keep track of which index corresponds to which variable.
Create a separate class e.g.
State
with public fields of the appropriate type and use that as an argument to a method: This is my current solution. It allows the method to alter the variables, while still keeping their names. It has the disadvantage, however, that theState
class will get more and more complex, as more macros and variables are added, in order to avoid copying values back and forth among differentState
objects.
How would you rewrite such a C macro in Java? Is there a more appropriate way to deal with this, using the facilities provided by the standard Java 6 Development Kit (i.e. without 3rd party libraries or a separate preprocessor)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选项 3,创建您自己的 MutableInteger 包装类。
Option 3, create you own MutableInteger wrapper class.
这个,不过是作为一个中间步骤。然后继续重构 - 理想情况下 State 类应该有私有字段。将宏替换为更新此状态的方法。然后用更新状态的方法替换所有其余代码,直到最终您的程序看起来像:
最后,将
State
重命名为SHA1
或其他;-)This, but as an intermediate step. Then continue refactoring - ideally class State should have private fields. Replace the macros with methods to update this state. Then replace all the rest of your code with methods that update the state, until eventually your program looks like:
Finally, rename
State
toSHA1
or whatever ;-)