- 1
- 2
- 3
- 4
- 5
template<typename Class>
void Raise(int Code)
{
throw Class(Code);
};
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+165
template<typename Class>
void Raise(int Code)
{
throw Class(Code);
};
Продолжение эпоса из уже всем знакомого проекта, доставшегося по наследству, в котором активно используются исключния.
+167
const StringId sidPathName = [=]() -> const char* const
{
switch (path)
{
case PATH_TOWN_HERO:
return "game/careers/town_hero";
case PATH_MAD_SCIENTIST:
return "game/careers/mad_scientist";
case PATH_SPECIAL_AGENT:
return "game/careers/special_agent";
case PATH_PSYCHIC_PAINTER:
return "game/careers/psychic_painter";
default:
RZT_ASSERT(false);
return "";
}
}();
Не удержался: давно мечтал о возможности ставить const на такие переменные, не отказываясь при этом от свитчей!
+163
/**
* Callback for gravity gradient menu option. Turn Gravity Gradient profile display on/off.
*/
void Exf2dMW::toggleGravGradientProfileCb()
{
// GV show Gravity Gradient profile here.
if (_toggleGravGradientProfileAct->isChecked())
gvWidget()->showProfiles(1);
else
gvWidget()->hideProfiles(1);
}
/**
* Callback for gravity menu option. Turn Gravity profile display on/off.
*/
void Exf2dMW::toggleGravProfileCb()
{
// GV show Gravity profile here.
if (_toggleGravProfileAct->isChecked())
gvWidget()->showProfiles(2);
else
gvWidget()->hideProfiles(2);
}
/**
* Callback for magnetic menu option. Turn Magnetic profile display on/off.
*/
void Exf2dMW::toggleMagProfileCb()
{
// GV show magnetic profile here.
if (_toggleMagProfileAct->isChecked())
gvWidget()->showProfiles(3);
else
gvWidget()->hideProfiles(3);
}
boolean logic fail.
+173
void DrawingLibrary::drawText(int x0, int y0, char string[], int strlen, int red, int green, int blue, int alpha) {
for(int i = 0; i < strlen; ++i) {
char chr = string[i];
int sX = x0 + i * 8;
// проходимся по символам, рисуя их
if(chr == '!') {
bool symbol[8][8] = {{0,0,0,0,0,1,0,0},{0,0,0,0,0,1,0,0},{0,0,0,0,0,1,0,0},{0,0,0,0,0,1,0,0},{0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0}};
this->drawSymbol(sX, y0, symbol, red, green, blue, alpha);
}
else if(chr == '"') {
bool symbol[8][8] = {{0,0,0,0,1,0,1,0},{0,0,0,0,1,0,1,0},{0,0,0,0,1,0,1,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
this->drawSymbol(sX, y0, symbol, red, green, blue, alpha);
}
else if(chr == '#') {
bool symbol[8][8] = {{0,0,0,0,1,0,1,0},{0,0,0,0,1,0,1,0},{0,0,0,1,1,1,1,1},{0,0,0,0,1,0,1,0},{0,0,0,1,1,1,1,1},{0,0,0,0,1,0,1,0},{0,0,0,0,1,0,1,0},{0,0,0,0,0,0,0,0}};
this->drawSymbol(sX, y0, symbol, red, green, blue, alpha);
}
/* еще 135 строк такого "кода" */
else if(chr == 'э') {
bool symbol[8][8] = {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,1,1,1,0},{0,0,0,0,0,0,0,1},{0,0,0,0,0,1,1,1},{0,0,0,0,0,0,0,1},{0,0,0,0,1,1,1,0},{0,0,0,0,0,0,0,0}};
this->drawSymbol(sX, y0, symbol, red, green, blue, alpha);
}
else if(chr == 'ю') {
bool symbol[8][8] = {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,1,0,0,1,0},{0,0,0,1,0,1,0,1},{0,0,0,1,1,1,0,1},{0,0,0,1,0,1,0,1},{0,0,0,1,0,0,1,0},{0,0,0,0,0,0,0,0}};
this->drawSymbol(sX, y0, symbol, red, green, blue, alpha);
}
else if(chr == 'я') {
bool symbol[8][8] = {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,1,1,1},{0,0,0,0,1,0,0,1},{0,0,0,0,0,1,1,1},{0,0,0,0,0,1,0,1},{0,0,0,0,1,0,0,1},{0,0,0,0,0,0,0,0}};
this->drawSymbol(sX, y0, symbol, red, green, blue, alpha);
}
}
}
Функция, рисующая на картинке текст.
+162
CString sLineNumber = _T("Line");
for(int lineIndex = 0; lineIndex < 255; lineIndex++)
{
sLineNumber += (_itot(lineIndex + 1, temp, 10));
...
sLineNumber = _T("Line");
}
Build the string variable in loop, instead of use CString::Format(...)
+176
__forceinline BYTE ClampShortToByte(signed short value)
{
value>255?value=255:( value<0?value=0:NULL );
return (BYTE)value;
}
+162
#ifdef WIN32
string nameOfLibToLoad("C:\opt\lib\libctest.dll");
lib_handle = LoadLibrary(TEXT(nameOfLibToLoad.c_str()));
if (!lib_handle) {
cerr << "Cannot load library: " << TEXT(nameOfDllToLoad.c_str()) << endl;
}
#else
string nameOfLibToLoad("/opt/lib/libctest.so");
lib_handle = dlopen(nameOfLibToLoad.c_str(), RTLD_LAZY);
if (!lib_handle) {
cerr << "Cannot load library: " << dlerror() << endl;
}
#endif
кроссплатформенная загрузка шаренной библиотеки... может я чего то не понимаю в этой жизни, но... совершенно здесь лишний класс строки, который делает совершенно лишний вызов конструктора и добавляет совершенно лишние n-килобайт к размеру сборки...
ссыль: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
+171
#include <iostream>
template <int n>
struct Solution
{
static const int count = Solution<n / 10>::count + 1;
static const int sum = Solution<n / 10>::sum + n % 10;
static const int last = n % 10;
static const int first = Solution<n / 10>::first;
};
#define DECLARE_ONE_DIGIT_SOLUTION(n) template <>\
struct Solution<n>\
{\
static const int count = 1;\
static const int sum = n;\
static const int last = n;\
static const int first = n;\
};
DECLARE_ONE_DIGIT_SOLUTION(0)
DECLARE_ONE_DIGIT_SOLUTION(1)
DECLARE_ONE_DIGIT_SOLUTION(2)
DECLARE_ONE_DIGIT_SOLUTION(3)
DECLARE_ONE_DIGIT_SOLUTION(4)
DECLARE_ONE_DIGIT_SOLUTION(5)
DECLARE_ONE_DIGIT_SOLUTION(6)
DECLARE_ONE_DIGIT_SOLUTION(7)
DECLARE_ONE_DIGIT_SOLUTION(8)
DECLARE_ONE_DIGIT_SOLUTION(9)
int main()
{
const int number = 1024; // <-- то самое число a
std::cout << "Number of digits: " << Solution<number>::count << std::endl;
std::cout << "Sum: " << Solution<number>::sum << std::endl;
std::cout << "Last digit: " << Solution<number>::last << std::endl;
std::cout << "First digit: " << Solution<number>::first << std::endl;
return 0;
}
Это один из ответов к слезной просьбе какого-то школьника (студента) выполнить за него д/з на С++ в разделе development форума на ЛОРе. Такую программу нарочно хрен напишешь.
Само задание: «Дано натуральное число а (a≤100). Напишите программу, определяющую количество цифр в этом числе, сумму его цифр, выводящую на экран первую и последнюю цифру через два пробела».
+161
std::auto_ptr<ItemDesc> desc(new ItemDesc());
...
m_items.insert(desc->m_item->m_name, desc.release());
Да, я тоже говнокодер. А ведь предупреждали...
+157
struct Event
{
int priority;
bool operator<(Event e) const
{
return e.priority>priority;
}
virtual void operator() () = 0;
};
priority_queue<Event*, deque<Event*> > eventQueue;
template <typename T, void (T::*g)()>
struct TEvent : Event
{
T* parent;
virtual void operator() ()
{
(parent->*g)();
}
TEvent (T* _parent) : parent(_parent){}
};
struct Entity
{
int foo;
void AFunc()
{
cout<<foo;
}
void BFunc()
{
cout<<"Ololo";
}
typedef TEvent<Entity,&Entity::AFunc> someEvent;
Entity(int _foo): foo(_foo){}
void addEvent()
{
eventQueue.push(new someEvent(this));
}
};
int main()
{
Entity foo(10);
foo.addEvent();
(*eventQueue.top())();
return 0;
}
Один из самых сложных и неочевидных способов вывести в консоль число 10.
(Этот код - проверка, будет ли работать прототип велосипедного делегата)