我有以下几个用于创建测试功能的C预处理器宏:
// Defines a test function in the active suite
#define test(name)\
void test_##name();\
SuiteAppender test_##name##_appender(TestSuite::active(), test_##name);\
void test_##name()
Run Code Online (Sandbox Code Playgroud)
这是这样使用的:
test(TestName) {
// Test code here
}
Run Code Online (Sandbox Code Playgroud)
和
// Defines a test function in the specified suite
#define testInSuite(name, suite)\
void test_##name();\
SuiteAppender test_##name##_appender(suite, test_##name);\
void test_##name()
Run Code Online (Sandbox Code Playgroud)
这是这样使用的:
test(TestName, TestSuiteName) {
// Test code here
}
Run Code Online (Sandbox Code Playgroud)
如何删除两个宏之间的重复?
我有一个带有两个不同应用程序(前端,后端)的symfony应用程序,但有一个共同的操作.现在我在两个应用程序中都重复了它的代码,但我完全不喜欢它.
有没有办法在多个symfony应用程序中重用一个动作?
我想在我的构造函数中抛出异常,这样我就不必处理僵尸对象了.但是,我还想提前提供验证方法,以便人们可以避免在没有原因的情况下"处理异常".在GUI中,期望无效数据并不例外.但是我也想避免代码重复和开销.GCC/Microsoft Visual C++编译器是否足够聪明,可以消除两次验证输入的低效率,如果没有,是否会有一个微妙的变化可以取悦?
我的观点的示例代码块如下:
#include <string>
#include <exception>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
using std::exception;
// a validation function
bool InputIsValid(const string& input) {
return (input == "hello");
}
// a class that uses the validation code in a constructor
class MyObject {
public:
MyObject(string input) {
if (!InputIsValid(input)) //since all instances of input
throw exception(); //has already been validated
//does this code incur an overhead
//or get optimised out?
cout << input << endl; …Run Code Online (Sandbox Code Playgroud) Git新手问题。我设置了两个组织,并且希望将存储库从一个组织复制/复制到另一个组织。需要采取哪些步骤?
class Vec3{
private:
float x, y, z;
public:
Vec3() = default;
Vec3(const float c) {x = c; y = c; z = c;}
static Vec3& normalize(Vec3& v) {/* normalize */ return v;}
};
Vec3 aaa = Vec3( 1.0f);
Vec3 bbb = Vec3::normalize( Vec3( 1.0f));
Vec3 ccc = Vec3::normalize( aaa);
Run Code Online (Sandbox Code Playgroud)
我想编写将向量作为参数的函数,对它们进行一些处理并将它们作为引用返回.
在上面的代码中bbb不会编译,因为它是对rvalue的非const引用.我不能使它成为const因为normalize需要修改对象.如果我使函数接受rvalue references(Vec3&& v)然后ccc不会编译因为aaa是左值.我可以在不写两个版本的情况下完成这项工作normalize吗?
(我对rvalue vs左值引用感到困惑,我不明白为什么a someFunc(const Vec3& v)会接受rvalues和lvalues,而非const版本不会.)
我想找到第一个myfile????.txt尚不存在的文件名(???? 是一个数字).这有效:
import os
i = 0
f = 'myfile%04i.txt' % i
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
Run Code Online (Sandbox Code Playgroud)
但我不喜欢代码重复f = ....
是否有一种pythonic方法来避免此while循环中的代码重复?
注意:我已经发布了一个半满意的解决方案,使用do/while成语,如在Python中模拟一个do-while循环的主要答案中所提到的那样?,但我仍然想知道这种特殊情况是否有更好的方法(因此,这不是这个问题的愚蠢).
这是之前的 MainActivity.kt
var spannable = SpannableStringBuilder("$noColorText$coloredText")
spannable.setSpan(
ForegroundColorSpan(ContextCompat.getColor(textView.context, R.color.mainGreen)),
noColorText.length, spannable.length,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
spannable.setSpan(
StyleSpan(BOLD),
noColorText.length, spannable.length,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
textView.text = spannable
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的方法。
扩展名.kt
// TODO: e.g: "string".putSpans(start, end, flags) { ForgroundColorSpan(color), StyleSpan(BOLD) }
fun String.putSpans(vararg flags: Int.() -> Unit, spanBuilder: SpannableStringBuilder.() -> Unit):
SpannableStringBuilder = SpannableStringBuilder(this).apply(spanBuilder)
Run Code Online (Sandbox Code Playgroud)
主活动.kt
// TODO: Change SpannableBuilder to be modular (without, reinput duplicate args)
val resultSpan = "$noColorText$coloredText ".putSpans {
setSpan(ForegroundColorSpan(ContextCompat.getColor(textView.context, R.color.mainGreen)),
noColorText.length, this.length, // this is duplicate
Spannable.SPAN_EXCLUSIVE_INCLUSIVE) // this is duplicate
setSpan(StyleSpan(BOLD),
noColorText.length, …Run Code Online (Sandbox Code Playgroud) code-duplication kotlin kotlin-extension spannablestringbuilder
对于每个实体,我正在制作控制器,服务和DAO.我现在有大约8个实体与这些类.让我们来看看,例如,我的课程Categorie和Product.
类CategorieDaoImpl实现方法CategorieDao
@Override
public boolean insertCategorie(Categorie categorie) {
Session session = null;
try {
session = super.getConnection();
session.getTransaction().begin();
session.save(categorie);
session.getTransaction().commit();
return true;
} catch (HibernateException e) {
e.printStackTrace();
return false;
} finally {
closeConnection(session);
}
}
Run Code Online (Sandbox Code Playgroud)
类ProductDaoImpl实现方法ProductDao
@Override
public boolean insertProduct(Product product) {
Session session = null;
try {
session = super.getConnection();
session.getTransaction().begin();
session.save(product);
session.getTransaction().commit();
return true;
} catch (HibernateException e) {
e.printStackTrace();
return false;
}
finally {
closeConnection(session);
} …Run Code Online (Sandbox Code Playgroud) 我有一个基于锯齿状数组的循环,我需要在不同的地方使用多次.
如何防止自己一次又一次地重写这个循环,以便我将它复制?
foreach (int[] columns in rowsAndColumns)
{
foreach (int element in columns)
{
}
}
Run Code Online (Sandbox Code Playgroud) 假设我有2个按钮,它们应该执行相同的操作但是在不同的对象上.
目前我正在将所有需要的引用传递给这样的方法:
private void sub1_add_to_db_btn_Click(object sender, EventArgs e)
{
Add_Substance_To_Database(
substanse1, sub1_add_to_db_btn, sub2_add_to_db_btn, sub1_found_in_db_list,
sub2_found_in_db_list, false, sub1_listBox, sub2_listBox);
}
private void sub2_add_to_db_btn_Click(object sender, EventArgs e)
{
Add_Substance_To_Database(
substanse2, sub2_add_to_db_btn, sub1_add_to_db_btn, sub2_found_in_db_list,
sub1_found_in_db_list, false, sub2_listBox, sub1_listBox);
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有其他更有效的方法来做到这一点.谢谢.
编辑:
这是我的一些代码看起来像,它让我疯狂!
private void sub1_add_to_db_btn_Click(object sender, EventArgs e)
{
Add_Substance_To_Database(substanse1, sub1_add_to_db_btn, sub2_add_to_db_btn,
sub1_found_in_db_list, sub2_found_in_db_list, false, sub1_listBox, sub2_listBox);
}
private void sub2_add_to_db_btn_Click(object sender, EventArgs e)
{
Add_Substance_To_Database(substanse2, sub2_add_to_db_btn, sub1_add_to_db_btn,
sub2_found_in_db_list, sub1_found_in_db_list, false, sub2_listBox, sub1_listBox);
}
private void sub1_edit_name_btn_Click(object sender, EventArgs e)
{
Add_Substance_To_Database(substanse1, sub1_add_to_db_btn, …Run Code Online (Sandbox Code Playgroud) code-duplication ×10
c# ×2
c++ ×2
.net ×1
c ×1
duplicates ×1
git ×1
java ×1
kotlin ×1
loops ×1
lvalue ×1
macros ×1
methods ×1
optimization ×1
python ×1
repository ×1
rvalue ×1
symfony1 ×1
validation ×1
while-loop ×1
winforms ×1