- sswitch for C++
- Single Value Multiple Compare for C++
- nuget.org : cppsswitch
- github.com : Clone or download > download ZIP
- clang++ 3.6.0 (c++, c++98, c++0x, c++11, c++14, c++1z)
- g++ 4.9.2 (c++, c++98, c++0x, c++11, c++14)
- Visual Studio 2010(v100), 2015(v140)
- Clang 3.7 with Microsoft CodeGen (v140_clang_3_7)
Description
- sv: SwitchValue (string, wstring, int, long long, float, double)
- v : Variable (string, wstring, int, long long, float, double)
- i : Interger (int, long long)
- n : Number (int, long long, float, double)
- b : Boolean (bool)
- expr: Expression
#include <iostream>
#include <stdio.h>
#include "sswitch.h"
#include "svmcmp.h" // Single Value Multiple Compare
#include "slvb.h" // Statements Like Visual Basic(useless)
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || _MSC_VER >= 1600
#include "sops.h" // Simple Operators(more useless)
#endif
using namespace std;
void testcase1()
{
#ifdef _SOPS_H_
1 between(1, 3);
#endif // _SOPS_H_
Switch(std::string("test").c_str())
{
Case(std::string("test").c_str()) :
Case("test") :
cout << "Error!" << endl;
break;
Default:
cout << "Important: For the performance, "
<< "Switch statement is not support string pointer of the rvalue instance."
<< endl;
break;
} End_Switch
std::string str1 = "test";
Switch(str1.c_str())
{
Case(std::wstring(L"test").c_str()) :
Default:
cout << "Error!" << endl;
break;
Case(std::string("test").c_str()) :
cout << "True!" << endl;
break;
} End_Switch
Switch("123")
{
Default:
cout << "Not matched" << endl;
break;
Case_InNCmp(3, 123, 234, 456) :
cerr << "Wrong matched" << endl;
break;
Case_InNCmp(3, 123.0, 234.0, 456.0) :
cerr << "Wrong matched" << endl;
break;
#if defined(__cpp_variadic_templates)
Case_In("1123", 1234, "123", 123.1f, 123.2) :
cout << "Matched with varadic template" << endl;
break;
#endif // !__cpp_variadic_templates
Case_InNCmp(3, "1123", "1223", "1234", "123") :
cerr << "Wrong matched" << endl;
break;
Case_InNCmp(4, "1123", "1223", "1234", "123") :
cout << "Matched" << endl;
break;
} End_Switch;
int a = 0;
string s1("Age");
wstring sw1(L"Age");
for (int i = 0; i < 2; i++)
{
Switch(i)
{
#ifdef _MSC_VER
Case(wstring(L"Age").c_str()) : Case(L"Age") :
a = 50;
break;
#endif // _MSC_VER
Case_Between(1, 3) :
a += 1200;
break;
Case("Name") :
a = 100;
break;
Case("Age") :
a += 200;
Case("Age1") :
Default:
a += 300;
continue;
Case_Not_Between(1.1, 3.5) :
a += 800;
Switch(s1.c_str())
{
#ifdef _MSC_VER
Case(wstring(L"Age").c_str()) :
Case(L"Age") :
a = 50;
break;
#endif // _MSC_VER
Case_Between(1, 3.0) :
a += 1200;
break;
Case("Name") :
a = 100;
break;
Case("Age") :
a += 200;
Case("Age1") :
Default:
a += 300;
continue;
Case_Not_Between(1.1, 3.5) :
a += 800;
break;
Case(string("Age").c_str()) :
break;
} End_SwitchC // support "continue"
break;
Case(string("Age").c_str()) :
break;
} End_SwitchC // support "continue"
// Like Visual Basic
Select_Case(s1.c_str())
#ifdef _MSC_VER
Case_(wstring(L"Age").c_str())
Case_(L"Age")
a = 50;
#endif // _MSC_VER
Case_Between_(1, 3.0)
a += 1200;
Case_("Name")
a = 100;
Case_("Age")
a += 200;
Case_("Age1")
Default_
a += 300;
continue; // Next
Case_Not_Between_(1.1, 3.5)
a += 800;
Case_(string("Age").c_str())
End_SelectC // support "continue"
}
cout << "Result: " << a << endl;
}
전에 테스트 좀 하고 공개하기로 했는데 워낙 게을러서 좀 많이 늦었네요.
단일값 다중 비교 연산자 기능도 써보세요. (svmcmp.h)
깃헙 : https://github.com/GuruWand/sswitch
누겟 : https://www.nuget.org/packages/cppsswitch
오픈소스는 개추야
존나복잡하네 걍if쓰고맘
ㄴ 언제까지 if문 떡칠만 하실겁니까!!!
음 신기하군 개추
언더바와 대소문자 혼용 뭐잉... 일단 누구말처럼 복잡한게, 자주 쓰고 유용한 것 위주로 정리하는게 좋을것 같아.
아예 성능 개선이 힘든 것들은 빼버리구.
언더바와 대소문자 혼용은 일부러 그렇게 한거구요. Case 문 연산자 또는 연산자끼리의 명확하게 구분하자는 의미 입니다. (개병신 같지만) 일단 자주 쓰지 않더라도 꼭 있어야 하는것만 있구요. 꼭 필요할 때 뭐하나 없으면 개 빡치거든요. 몇개 줄인다고 해서 속도가 더 빨라지는것도 아니고요.
몇개 더 넣는다고 해서 다른것들이 느려 지는것도 아니고요.
일부러 그렇게 한 것 때문에 camel 과 snake 양대 스타일의 비호감을 살 수 있을듯.
저도 그럴거 같아요 ㅠ.ㅠ