Function.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef __DAHUA_INFRA_TFUNCTION_H__
  2. #define __DAHUA_INFRA_TFUNCTION_H__
  3. #if defined(_MSC_VER)
  4. #pragma warning (push)
  5. #pragma warning (disable : 4786)
  6. #endif
  7. #include "Defs.h"
  8. #include "Memory/SharedPtr.h"
  9. #include "Memory/WeakPtr.h"
  10. namespace Dahua {
  11. namespace Infra {
  12. namespace Detail {
  13. template< typename T > struct is_void
  14. {
  15. enum { value = false };
  16. };
  17. template<> struct is_void< void >
  18. {
  19. enum { value = true };
  20. };
  21. template<class R> struct function_return_type { typedef R type; };
  22. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  23. struct unusable
  24. {
  25. unusable() {}
  26. template<class T> unusable(T const&) {}
  27. };
  28. template<> struct function_return_type<void> { typedef unusable type; };
  29. #define DAHUA_FUNCTION_RETURN(X) X; return Detail::unusable ()
  30. #define DAHUA_FUNCTION_TYPENAME
  31. #define DAHUA_FUNCTION_TEMPLATE
  32. #else
  33. #define DAHUA_FUNCTION_RETURN(X) X
  34. #define DAHUA_FUNCTION_TYPENAME typename
  35. #define DAHUA_FUNCTION_TEMPLATE template
  36. #endif
  37. // from http://www.codeproject.com/cpp/FastDelegate.asp by Don Clugston
  38. // horrible_cast< >
  39. // This is truly evil. It completely subverts C++'s type system, allowing you
  40. // to cast from any class to any other class. Technically, using a union
  41. // to perform the cast is undefined behaviour (even in C). But we can see if
  42. // it is OK by checking that the union is the same size as each of its members.
  43. // horrible_cast<> should only be used for compiler-specific workarounds.
  44. // Usage is identical to reinterpret_cast<>.
  45. // This union is declared outside the horrible_cast because BCC 5.5.1
  46. // can't inline a function with a nested class, and gives a warning.
  47. template <class OutputClass, class InputClass>
  48. union horrible_union{
  49. OutputClass out;
  50. InputClass in;
  51. };
  52. template <class OutputClass, class InputClass>
  53. inline OutputClass horrible_cast(const InputClass input){
  54. horrible_union<OutputClass, InputClass> u;
  55. // Cause a compile-time error if in, out and u are not the same size.
  56. // If the compile fails here, it means the compiler has peculiar
  57. // unions which would prevent the cast from working.
  58. typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass) == sizeof(u)
  59. /*&& sizeof(InputClass)==sizeof(OutputClass)*/ ? 1 : -1];
  60. u.in = input;
  61. return u.out;
  62. }
  63. } // namespace Detail
  64. //TFuction0
  65. #define FUNCTION_NUMBER 0
  66. #define FUNCTION_CLASS_TYPES typename R
  67. #define FUNCTION_TYPES
  68. #define FUNCTION_TYPE_ARGS
  69. #define FUNCTION_ARGS
  70. #include "FunctionTemplate.h"
  71. #undef FUNCTION_NUMBER
  72. #undef FUNCTION_CLASS_TYPES
  73. #undef FUNCTION_TYPES
  74. #undef FUNCTION_TYPE_ARGS
  75. #undef FUNCTION_ARGS
  76. //TFuction1
  77. #define FUNCTION_NUMBER 1
  78. #define FUNCTION_CLASS_TYPES typename R, typename T1
  79. #define FUNCTION_TYPES T1
  80. #define FUNCTION_TYPE_ARGS T1 a1
  81. #define FUNCTION_ARGS a1
  82. #include "FunctionTemplate.h"
  83. #undef FUNCTION_NUMBER
  84. #undef FUNCTION_CLASS_TYPES
  85. #undef FUNCTION_TYPES
  86. #undef FUNCTION_TYPE_ARGS
  87. #undef FUNCTION_ARGS
  88. //TFuction2
  89. #define FUNCTION_NUMBER 2
  90. #define FUNCTION_CLASS_TYPES typename R, typename T1, typename T2
  91. #define FUNCTION_TYPES T1, T2
  92. #define FUNCTION_TYPE_ARGS T1 a1, T2 a2
  93. #define FUNCTION_ARGS a1, a2
  94. #include "FunctionTemplate.h"
  95. #undef FUNCTION_NUMBER
  96. #undef FUNCTION_CLASS_TYPES
  97. #undef FUNCTION_TYPES
  98. #undef FUNCTION_TYPE_ARGS
  99. #undef FUNCTION_ARGS
  100. //TFuction3
  101. #define FUNCTION_NUMBER 3
  102. #define FUNCTION_CLASS_TYPES typename R, typename T1, typename T2, typename T3
  103. #define FUNCTION_TYPES T1, T2, T3
  104. #define FUNCTION_TYPE_ARGS T1 a1, T2 a2, T3 a3
  105. #define FUNCTION_ARGS a1, a2, a3
  106. #include "FunctionTemplate.h"
  107. #undef FUNCTION_NUMBER
  108. #undef FUNCTION_CLASS_TYPES
  109. #undef FUNCTION_TYPES
  110. #undef FUNCTION_TYPE_ARGS
  111. #undef FUNCTION_ARGS
  112. //TFuction4
  113. #define FUNCTION_NUMBER 4
  114. #define FUNCTION_CLASS_TYPES typename R, typename T1, typename T2, typename T3, typename T4
  115. #define FUNCTION_TYPES T1, T2, T3, T4
  116. #define FUNCTION_TYPE_ARGS T1 a1, T2 a2, T3 a3, T4 a4
  117. #define FUNCTION_ARGS a1, a2, a3, a4
  118. #include "FunctionTemplate.h"
  119. #undef FUNCTION_NUMBER
  120. #undef FUNCTION_CLASS_TYPES
  121. #undef FUNCTION_TYPES
  122. #undef FUNCTION_TYPE_ARGS
  123. #undef FUNCTION_ARGS
  124. //TFuction5
  125. #define FUNCTION_NUMBER 5
  126. #define FUNCTION_CLASS_TYPES typename R, typename T1, typename T2, typename T3, typename T4, typename T5
  127. #define FUNCTION_TYPES T1, T2, T3, T4, T5
  128. #define FUNCTION_TYPE_ARGS T1 a1, T2 a2, T3 a3, T4 a4, T5 a5
  129. #define FUNCTION_ARGS a1, a2, a3, a4, a5
  130. #include "FunctionTemplate.h"
  131. #undef FUNCTION_NUMBER
  132. #undef FUNCTION_CLASS_TYPES
  133. #undef FUNCTION_TYPES
  134. #undef FUNCTION_TYPE_ARGS
  135. #undef FUNCTION_ARGS
  136. //TFuction6
  137. #define FUNCTION_NUMBER 6
  138. #define FUNCTION_CLASS_TYPES typename R, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6
  139. #define FUNCTION_TYPES T1, T2, T3, T4, T5, T6
  140. #define FUNCTION_TYPE_ARGS T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6
  141. #define FUNCTION_ARGS a1, a2, a3, a4, a5, a6
  142. #include "FunctionTemplate.h"
  143. #undef FUNCTION_NUMBER
  144. #undef FUNCTION_CLASS_TYPES
  145. #undef FUNCTION_TYPES
  146. #undef FUNCTION_TYPE_ARGS
  147. #undef FUNCTION_ARGS
  148. } // namespace Infra
  149. } // namespace Dahua
  150. #if defined(_MSC_VER)
  151. #pragma warning (pop)
  152. #endif
  153. #endif //__DAHUA_INFRA_TFUNCTION_H__