1. C++ / Говнокод #19606

    +6

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    //		implicit_cast< >
    // I believe this was originally going to be in the C++ standard but 
    // was left out by accident. It's even milder than static_cast.
    // I use it instead of static_cast<> to emphasize that I'm not doing
    // anything nasty. 
    // Usage is identical to static_cast<>
    template <class OutputClass, class InputClass>
    inline OutputClass implicit_cast(InputClass input){
    	return input;
    }
    
    //		horrible_cast< >
    // This is truly evil. It completely subverts C++'s type system, allowing you 
    // to cast from any class to any other class. Technically, using a union 
    // to perform the cast is undefined behaviour (even in C). But we can see if
    // it is OK by checking that the union is the same size as each of its members.
    // horrible_cast<> should only be used for compiler-specific workarounds. 
    // Usage is identical to reinterpret_cast<>.
    
    // This union is declared outside the horrible_cast because BCC 5.5.1
    // can't inline a function with a nested class, and gives a warning.
    template <class OutputClass, class InputClass>
    union horrible_union{
    	OutputClass out;
    	InputClass in;
    };
    
    template <class OutputClass, class InputClass>
    inline OutputClass horrible_cast(const InputClass input){
    	horrible_union<OutputClass, InputClass> u;
    	// Cause a compile-time error if in, out and u are not the same size.
    	// If the compile fails here, it means the compiler has peculiar
    	// unions which would prevent the cast from working.
    	typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass)==sizeof(u) 
    		&& sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1];
    	u.in = input;
    	return u.out;
    }

    Боль и страдание шаблонного программирования на С++98. Комменты и названия доставляют.

    gorthauer87, 11 Марта 2016

    Комментарии (17)
  2. C++ / Говнокод #19604

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    void checklock(globalMemManager *c)
    	{
    		if (!c->isThread) return;
    		_voidint idl=c->isThread();
    #ifdef THREADDEBUG
    		assert(idl!=0);
    #endif
    		while (idl==0 && c->lock());
    		if (idl!=0) while (true) {
    			while (c->lockId()!=idl) {
    				c->canLeave()=idl;
    			}
    			if (c->lock()) break;
    		}
    		c->lock()=true;
    	}
    
    	void unlock(globalMemManager *c)
    	{
    		if (!c->isThread) return;
    		c->lock()=false;
    #ifdef THREADDEBUG
    		assert(c->lockId()==c->isThread());
    #endif
    		c->canLeave()=0;
    		c->lockId()=0;
    	}
    
    
    	void globalMemManager::manage()
    	{
    		if (!lock()) if (lockId()==0 && canLeave()!=0) {
    			if (!lock()) lock()=true;
    			lockId()=canLeave();
    		}
    	}

    Лучшее

    foxes, 10 Марта 2016

    Комментарии (3)
  3. C++ / Говнокод #19603

    −2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    namespace bt {
    #define MEMNULL \
    	_FORCEINLINE void* operator new(size_t) { return 0; } \
    	_FORCEINLINE void operator delete(void* ) { }
    
    #define MEMDEFAULT(classname) \
    	_FORCEINLINE void* operator new(size_t size) { return extFunctions.CreateMem((unsigned int)size, 0); } \
    	_FORCEINLINE void operator delete(void* val) { extFunctions.DeleteMem(val, 0); }
    
    #define MEMMANAGER(classname) \
    	_FORCEINLINE void* operator new(size_t size) { return bt::internalmemmanager.getNew((unsigned int)size); } \
    	_FORCEINLINE void operator delete(void* val) {bt::internalmemmanager.freeThis(val,sizeof(classname));}
    
    #define MEMMANAGERCLEAN(classname) \
    	_FORCEINLINE void* operator new(size_t size) { return bt::internalmemmanager.getNewClean((unsigned int)size); } \
    	_FORCEINLINE void operator delete(void* val) { bt::internalmemmanager.freeThis(val,sizeof(classname)); }
    
    	class memManagerExport {
    	public:
    		MEMDEFAULT(memManagerExport)
    
    		BT_API memManagerExport(unsigned int size);
    
    		BT_API virtual ~memManagerExport();
    
    		/// destroy all memory segments and free list of free pointers
    		BT_API void _free();
    		/// return pointer to new object and create new segment of objects if need.
    		BT_API void *_addAlloc();
    		/// return pointer to free object
    		BT_API void *_getFree(unsigned int size);
    		/// add pointer to list of free
    		BT_API void _appendToFree(_voidint idat);
    		/// mark pointer to free ???
    		BT_API void _markFree(void* val);
    		/// return number object in segment
    		BT_API unsigned int _valid(unsigned int id);
    		/// return segment number
    		BT_API unsigned int _segid(unsigned int id);
    		/// prepare calculation for object size
    		BT_API void _calcsize(unsigned int size);
    
    	private:
    		enum States {
    			VALIDED = 0x011F1C01,
    			FREE = 0
    		};
    
    		unsigned int fisVal;
    		struct p_smemManager *fargs;
    	};
    
    	class memManager: public memManagerExport {
    	public:
    		MEMDEFAULT(memManager)
    
    		_FORCEINLINE memManager(unsigned int size):memManagerExport(size) {}
    
    		_FORCEINLINE ~memManager() {}
    
    		/// create memory for object
    		_FORCEINLINE void *getNew(unsigned int size) {return (void*)_getFree(size);}
    		/// delete memory for object
    		_FORCEINLINE void freeThis(void * val) {_appendToFree((_voidint)val);}
    		/// destroy all memory segments and free list of free
    		_FORCEINLINE void free() {_free();};
    	};
    
    	class globalMemManager {
    	public:
    		MEMDEFAULT(globalMemManager)
    
    }

    Давайте ржать

    foxes, 10 Марта 2016

    Комментарии (18)
  4. C++ / Говнокод #19601

    −1

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    #ifndef H112
    #define H112 201004L
    
    #include<iostream>
    #include<fstream>
    #include<sstream>
    #include<cmath>
    #include<cstdlib>
    #include<string>
    #include<list>
    #include<vector>
    #include<algorithm>
    #include<stdexcept>
    
    //------------------------------------------------------------------------------
    
    #ifdef _MSC_VER
    #include <hash_map>
    using stdext::hash_map;
    #else
    #include <ext/hash_map>
    using __gnu_cxx::hash_map;
    
    namespace __gnu_cxx {
    
        template<> struct hash<std::string>
        {
            size_t operator()(const std::string& s) const
            {
                return hash<char*>()(s.c_str());
            }
        };
    
    } // of namespace __gnu_cxx
    #endif
    
    //------------------------------------------------------------------------------
    
    #define unordered_map hash_map
    
    //------------------------------------------------------------------------------
    
    typedef long Unicode;
    
    //------------------------------------------------------------------------------
    
    using namespace std;
    
    template<class T> string to_string(const T& t)
    {
      ostringstream os;
      os << t;
      return os.str();
    }
    
    struct Range_error : out_of_range {  // enhanced vector range error reporting
      int index;
      Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
    };
    
    
    // trivially range-checked vector (no iterator checking):
    template< class T> struct Vector : public std::vector<T> {
      typedef typename std::vector<T>::size_type size_type;
    
      Vector() { }
      explicit Vector(size_type n) :std::vector<T>(n) {}
      Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
      template <class I>
      Vector(I first, I last) :std::vector<T>(first,last) {}
    
      T& operator[](unsigned int i) // rather than return at(i);
      {
        if (i<0||this->size()<=i) throw Range_error(i);
        return std::vector<T>::operator[](i);
      }
      const T& operator[](unsigned int i) const
      {
        if (i<0||this->size()<=i) throw Range_error(i);
        return std::vector<T>::operator[](i);
      }
    };
    
    // disgusting macro hack to get a range checked vector:
    #define vector Vector
    
    // trivially range-checked string (no iterator checking):
    struct String : std::string {
      
      String() { }
      String(const char* p) :std::string(p) {}
      String(const string& s) :std::string(s) {}
      template<class S> String(S s) :std::string(s) {}
      String(int sz, char val) :std::string(sz,val) {}
      template<class Iter> String(Iter p1, Iter p2) : std::string(p1,p2) { }
    
      char& operator[](unsigned int i) // rather than return at(i);
      {
        if (i<0||size()<=i) throw Range_error(i);
        return std::string::operator[](i);

    LispGovno, 10 Марта 2016

    Комментарии (14)
  5. C++ / Говнокод #19596

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    class SMemManager {
    public:
     SMemManager() { std::cout<<"Singleton new: "<<this<<std::endl; init(); }
     
     void* operator new(size_t) { return (void*)p_body; }
     void operator delete(void* ) { }
     
    private:
     void init() {
      if (p_init!=0xF3149A51) {
      	  p_countMemData=0;
      	  p_memData=0;
    	  std::cout<<"Singleton init: "<<this<<std::endl;
      }
      p_init=0xF3149A51;
     }
     
     unsigned int p_init;
     unsigned int p_countMemData;
     void ** p_memData;
     static char p_body[];
     
    };
    char SMemManager::p_body[sizeof(SMemManager)];

    Один из эпичных синглтонов от foxes.
    Посмотреть эволюцию этого шедевра или окунуться в его обсуждение можно в теме по ссылке:
    http://www.gamedev.ru/code/forum/?id=211629&page=6#m81

    -Eugene-, 09 Марта 2016

    Комментарии (264)
  6. C++ / Говнокод #19591

    +2

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    const int MOD = 1000000007;
    
    int pow(int a, int b) {
    	if (!b) return 1;
    	if (b & 1) return (pow(a, b - 1) * 1LL * a) % MOD;
    	return pow((a * 1LL * a) % MOD, b / 2);
    }

    http://ideone.com/JlfNxZ

    Там ещё куча всякого говна есть
    http://acm.math.spbu.ru/~kunyavskiy/cpp/

    3_dar, 08 Марта 2016

    Комментарии (38)
  7. C++ / Говнокод #19588

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    switch (a) {
    	case 12345:
    		return 0;
    	case 14523:
    		return 1;
    	case 102543:
    		return 2;
    	case 104325:
    		return 3;
    	case 243051:
    		return 4;
    	case 245130:
    		return 5;
    	case 350214:
    		return 6;
    	case 351402:
    		return 7;
    	case 423150:
    		return 8;
    	case 425031:
    		return 9;
    	case 530412:
    		return 10;
    	case 531204:
    		return 11;
    	}
    	return -1;

    Ekke, 07 Марта 2016

    Комментарии (51)
  8. C++ / Говнокод #19579

    −3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    if (c & Expression::RefValue) {
          scontext += "|RefValue";
        }
        if (c & Expression::RefParameter) {
          scontext += "|RefParameter";
        }
        if (c & Expression::DeepReference) {
          scontext += "|DeepReference";
        }
        if (c & Expression::ObjectContext) {
          scontext += "|ObjectContext";
        }
        if (c & Expression::InParameterExpression) {
          scontext += "|InParameterExpression";
        }
        if (c & Expression::ExistContext) {
          scontext += "|ExistContext";
        }
        if (c & Expression::UnsetContext) {
          scontext += "|UnsetContext";
        }
        if (c & Expression::AssignmentLHS) {
          scontext += "|AssignmentLHS";
        }
        if (c & Expression::RefAssignmentLHS) {
          scontext += "|RefAssignmentLHS";
        }
        if (c & Expression::DeepAssignmentLHS) {
          scontext += "|DeepAssignmentLHS";
        }
        if (c & Expression::AssignmentRHS) {
          scontext += "|AssignmentRHS";
        }
        if (c & Expression::InvokeArgument) {
          scontext += "|InvokeArgument";
        }
        if (c & Expression::OprLValue) {
          scontext += "|OprLValue";
        }
        if (c & Expression::DeepOprLValue) {
          scontext += "|DeepOprLValue";
        }
        if (c & Expression::AccessContext) {
          scontext += "|AccessContext";
        }
        if (c & Expression::ReturnContext) {
          scontext += "|ReturnContext";
        }

    Нашел тут:
    https://github.com/facebook/hhvm/blob/master/hphp/compiler/construct.cpp#L226

    kschingiz, 05 Марта 2016

    Комментарии (35)
  9. C++ / Говнокод #19558

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    #if !defined LOG_PRINT_FNAME && !defined LOG_PRINT_LVL && \
        !defined LOG_PRINT_DATE  && !defined LOG_PRINT_TIME && \
        !defined LOG_TIME_FMT   &&  !defined LOG_DATETIME_SEP && \
        !defined LOG_PRINT_TIMER && !defined LOG_VERB
    #warning No logging options specified, default config will be used
    #endif
     
    // Initialize unitialized values
    #ifndef LOG_PRINT_FNAME
        #define LOG_PRINT_FNAME true
    #endif
    #ifndef LOG_PRINT_LVL
        #define LOG_PRINT_LVL false
    #endif
    #ifndef LOG_PRINT_DATE
        #define LOG_PRINT_DATE false
    #endif
    #ifndef LOG_PRINT_TIME
        #define LOG_PRINT_TIME false
    #endif
    #ifndef LOG_DATE_FMT
        #define LOG_DATE_FMT "%D"
    #endif
    #ifndef LOG_TIME_FMT
        #define LOG_TIME_FMT "%T"
    #endif
    #ifndef LOG_DATETIME_SEP
        #define LOG_DATETIME_SEP ""
    #endif
    #ifndef LOG_PRINT_TIMER
        #define LOG_PRINT_TIMER true
    #endif
    #ifndef LOG_VERB
        #define LOG_VERB 1
    #endif
     
     
    #if LOG_PRINT_TIMER == true && (LOG_PRINT_DATE == true || LOG_PRINT_TIME == true)
    #warning Printing timer and datetime both is not possible, datetime will be discarded
    #define LOG_PRINT_DATE false
    #define LOG_PRINT_TIME false
    #endif
     
    enum LogLevel_t {
        INFO     = 3,
        WARNING  = 2,
        ERROR    = 0,
        TRACE    = 7
    };
     
    #define TIMER
    #define DATE
    #define TIME
    #define SEPARATOR
    #define FNAME
    #define LVL(lvl)
     
    #define STREAM(lvl) \
        ((lvl) == TRACE || (lvl) == ERROR) ? std::cerr : std::cout
     
    #if LOG_PRINT_TIMER == true
    #define TIMER << "[" << std::fixed << Log::getTimer() << "] "
    #endif
     
    #if LOG_PRINT_DATE == true
    #define DATE << Log::getTimeFmt(LOG_DATE_FMT) << " "
    #endif
     
    #if LOG_PRINT_TIME == true
    #define TIME << Log::getTimeFmt(LOG_TIME_FMT) << " "
    #endif
     
    #if LOG_PRINT_DATE == true && LOG_PRINT_TIME == true
    #define SEPARATOR << LOG_DATETIME_SEP
    #endif
     
    #if LOG_PRINT_FNAME == true
    #define FNAME << __FUNCTION__ << ": "
    #endif
     
    #if LOG_PRINT_LVL == true
    #define LVL(lvl) << #lvl << ": "
    #endif
     
    #define LOG(lvl) \
        std::cout.precision(5); \
        STREAM(lvl) TIMER DATE SEPARATOR TIME LVL(lvl) FNAME

    xxx: Бля, я написал какое-то говно
    yyy: Я видел некоторое дерьмо
    xxx: Пиздец просто
    zzz: Действительно говно.
    xxx: Я не знаю даже нахуя я это делал
    xxx: Почему-то, когда начинал, мне не пришло в голову, что логгирование на макросах, конфигурируемое в компилтайме — это пиздец

    CYB3R, 02 Марта 2016

    Комментарии (26)
  10. C++ / Говнокод #19555

    +3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    std::string sql = "INSERT INTO digest_test_record (set_id, ref_digest, cand_digest, vdt_cfg_warn, digest_cfg_warn, "
        "ref_duration, ref_cardinality, ref_dispersion, "
        "cand_duration, cand_cardinality, cand_dispersion, "
        "cardinality, difference, red_difference, ext_difference, "
        "inv_cardinality, inv_difference, inv_red_difference, inv_ext_difference,
        "timing, lib_version) SELECT 0, "
        "ROW($1, $2, $3, $4, $5, $6, $7, $8)::digest_info, "
        "ROW($9, $10, $11, $12, $13, $14, $15, $16)::digest_info,"
        "$17, $18, ",
        "$19, $20, $21, "
        "$22, $23, $24, "
        "$25, $26, $27, $28, "
        "$29, $30, $31, $32, "
        "$33, ROW($34, $35, $36, make_date($37, $38, $39), $40)::lib_version_info";
    
        cn.prepare("insert", sql);
    
        xact.prepared("insert")
        (ref_digest_info.src_width)(ref_digest_info.src_height)(ref_digest_info.src_fps)(ref_digest_info.src_duration)
            (ref_digest_info.vdt_duration)(ref_digest_info.cardinality)(ref_digest_info.has_flags)(ref_digest_info.src_filename)
        (cand_digest_info.src_width)(cand_digest_info.src_height)(cand_digest_info.src_fps)(cand_digest_info.src_duration)
            (cand_digest_info.vdt_duration)(cand_digest_info.cardinality)(cand_digest_info.has_flags)(cand_digest_info.src_filename)
        (results.vdt_cfg_warn)(results.digest_cfg_warn)
        (results.ref_duration)(results.ref_cardinality)(results.ref_dispersion)
        (results.cand_duration)(results.cand_cardinality)(results.cand_dispersion)
        (results.cardinality)(results.difference)(results.red_difference)(results.ext_difference)
        (results.inv_cardinality)(results.inv_difference)(results.inv_red_difference)(results.inv_ext_difference)
        ((double)timing / CLOCKS_PER_SEC)(li.main_ver)(li.sub_ver)(li.revision)(li.year)(li.month)(li.day)(li.platform).exec();

    Мои глаза.... Яркий пример использования нативного pqxx

    leon_mz, 01 Марта 2016

    Комментарии (44)