Mirror of Svelto.ECS because we're a fan of it
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
9.4KB

  1. #if DISABLE_DBC || !DEBUG || PROFILE_SVELTO
  2. #define DISABLE_CHECKS
  3. using System.Diagnostics;
  4. #endif
  5. using System;
  6. namespace DBC.ECS
  7. {
  8. /// <summary>
  9. /// Design By Contract Checks.
  10. ///
  11. /// Each method generates an exception or
  12. /// a trace assertion statement if the contract is broken.
  13. /// </summary>
  14. /// <remarks>
  15. /// This example shows how to call the Require method.
  16. /// Assume DBC_CHECK_PRECONDITION is defined.
  17. /// <code>
  18. /// public void Test(int x)
  19. /// {
  20. /// try
  21. /// {
  22. /// Check.Require(x > 1, "x must be > 1");
  23. /// }
  24. /// catch (System.Exception ex)
  25. /// {
  26. /// Console.WriteLine(ex.ToString());
  27. /// }
  28. /// }
  29. /// </code>
  30. /// If you wish to use trace assertion statements, intended for Debug scenarios,
  31. /// rather than exception handling then set
  32. ///
  33. /// <code>Check.UseAssertions = true</code>
  34. ///
  35. /// You can specify this in your application entry point and maybe make it
  36. /// dependent on conditional compilation flags or configuration file settings, e.g.,
  37. /// <code>
  38. /// #if DBC_USE_ASSERTIONS
  39. /// Check.UseAssertions = true;
  40. /// #endif
  41. /// </code>
  42. /// You can direct output to a Trace listener. For example, you could insert
  43. /// <code>
  44. /// Trace.Listeners.Clear();
  45. /// Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
  46. /// </code>
  47. ///
  48. /// or direct output to a file or the Event Log.
  49. ///
  50. /// (Note: For ASP.NET clients use the Listeners collection
  51. /// of the Debug, not the Trace, object and, for a Release build, only exception-handling
  52. /// is possible.)
  53. /// </remarks>
  54. ///
  55. static class Check
  56. {
  57. #region Interface
  58. /// <summary>
  59. /// Precondition check.
  60. /// </summary>
  61. #if DISABLE_CHECKS
  62. [Conditional("__NEVER_DEFINED__")]
  63. #endif
  64. public static void Require(bool assertion, string message)
  65. {
  66. if (!assertion)
  67. throw new PreconditionException(message);
  68. }
  69. /// <summary>
  70. /// Precondition check.
  71. /// </summary>
  72. ///
  73. #if DISABLE_CHECKS
  74. [Conditional("__NEVER_DEFINED__")]
  75. #endif
  76. public static void Require(bool assertion, string message, Exception inner)
  77. {
  78. if (!assertion)
  79. throw new PreconditionException(message, inner);
  80. }
  81. /// <summary>
  82. /// Precondition check.
  83. /// </summary>
  84. ///
  85. #if DISABLE_CHECKS
  86. [Conditional("__NEVER_DEFINED__")]
  87. #endif
  88. public static void Require(bool assertion)
  89. {
  90. if (!assertion)
  91. throw new PreconditionException("Precondition failed.");
  92. }
  93. /// <summary>
  94. /// Postcondition check.
  95. /// </summary>
  96. ///
  97. #if DISABLE_CHECKS
  98. [Conditional("__NEVER_DEFINED__")]
  99. #endif
  100. public static void Ensure(bool assertion, string message)
  101. {
  102. if (!assertion)
  103. throw new PostconditionException(message);
  104. }
  105. /// <summary>
  106. /// Postcondition check.
  107. /// </summary>
  108. ///
  109. #if DISABLE_CHECKS
  110. [Conditional("__NEVER_DEFINED__")]
  111. #endif
  112. public static void Ensure(bool assertion, string message, Exception inner)
  113. {
  114. if (!assertion)
  115. throw new PostconditionException(message, inner);
  116. }
  117. /// <summary>
  118. /// Postcondition check.
  119. /// </summary>
  120. ///
  121. #if DISABLE_CHECKS
  122. [Conditional("__NEVER_DEFINED__")]
  123. #endif
  124. public static void Ensure(bool assertion)
  125. {
  126. if (!assertion)
  127. throw new PostconditionException("Postcondition failed.");
  128. }
  129. /// <summary>
  130. /// Invariant check.
  131. /// </summary>
  132. ///
  133. #if DISABLE_CHECKS
  134. [Conditional("__NEVER_DEFINED__")]
  135. #endif
  136. public static void Invariant(bool assertion, string message)
  137. {
  138. if (!assertion)
  139. throw new InvariantException(message);
  140. }
  141. /// <summary>
  142. /// Invariant check.
  143. /// </summary>
  144. ///
  145. #if DISABLE_CHECKS
  146. [Conditional("__NEVER_DEFINED__")]
  147. #endif
  148. public static void Invariant(bool assertion, string message, Exception inner)
  149. {
  150. if (!assertion)
  151. throw new InvariantException(message, inner);
  152. }
  153. /// <summary>
  154. /// Invariant check.
  155. /// </summary>
  156. ///
  157. #if DISABLE_CHECKS
  158. [Conditional("__NEVER_DEFINED__")]
  159. #endif
  160. public static void Invariant(bool assertion)
  161. {
  162. if (!assertion)
  163. throw new InvariantException("Invariant failed.");
  164. }
  165. /// <summary>
  166. /// Assertion check.
  167. /// </summary>
  168. #if DISABLE_CHECKS
  169. [Conditional("__NEVER_DEFINED__")]
  170. #endif
  171. public static void Assert(bool assertion, string message)
  172. {
  173. if (!assertion)
  174. throw new AssertionException(message);
  175. }
  176. /// <summary>
  177. /// Assertion check.
  178. /// </summary>
  179. ///
  180. #if DISABLE_CHECKS
  181. [Conditional("__NEVER_DEFINED__")]
  182. #endif
  183. public static void Assert(bool assertion, string message, Exception inner)
  184. {
  185. if (!assertion)
  186. throw new AssertionException(message, inner);
  187. }
  188. /// <summary>
  189. /// Assertion check.
  190. /// </summary>
  191. ///
  192. #if DISABLE_CHECKS
  193. [Conditional("__NEVER_DEFINED__")]
  194. #endif
  195. public static void Assert(bool assertion)
  196. {
  197. if (!assertion)
  198. throw new AssertionException("Assertion failed.");
  199. }
  200. #endregion // Interface
  201. #region Implementation
  202. // No creation
  203. /// <summary>
  204. /// Is exception handling being used?
  205. /// </summary>
  206. #endregion // Implementation
  207. } // End Check
  208. internal class Trace
  209. {
  210. internal static void Assert(bool assertion, string v)
  211. {
  212. #if NETFX_CORE
  213. System.Diagnostics.Contracts.Contract.Assert(assertion, v);
  214. #else
  215. System.Diagnostics.Trace.Assert(assertion, v);
  216. #endif
  217. }
  218. }
  219. #region Exceptions
  220. /// <summary>
  221. /// Exception raised when a contract is broken.
  222. /// Catch this exception type if you wish to differentiate between
  223. /// any DesignByContract exception and other runtime exceptions.
  224. ///
  225. /// </summary>
  226. public class DesignByContractException : Exception
  227. {
  228. protected DesignByContractException()
  229. {
  230. }
  231. protected DesignByContractException(string message) : base(message)
  232. {
  233. }
  234. protected DesignByContractException(string message, Exception inner) : base(message, inner)
  235. {
  236. }
  237. }
  238. /// <summary>
  239. /// Exception raised when a precondition fails.
  240. /// </summary>
  241. public class PreconditionException : DesignByContractException
  242. {
  243. /// <summary>
  244. /// Precondition Exception.
  245. /// </summary>
  246. public PreconditionException()
  247. {
  248. }
  249. /// <summary>
  250. /// Precondition Exception.
  251. /// </summary>
  252. public PreconditionException(string message) : base(message)
  253. {
  254. }
  255. /// <summary>
  256. /// Precondition Exception.
  257. /// </summary>
  258. public PreconditionException(string message, Exception inner) : base(message, inner)
  259. {
  260. }
  261. }
  262. /// <summary>
  263. /// Exception raised when a postcondition fails.
  264. /// </summary>
  265. public class PostconditionException : DesignByContractException
  266. {
  267. /// <summary>
  268. /// Postcondition Exception.
  269. /// </summary>
  270. public PostconditionException()
  271. {
  272. }
  273. /// <summary>
  274. /// Postcondition Exception.
  275. /// </summary>
  276. public PostconditionException(string message) : base(message)
  277. {
  278. }
  279. /// <summary>
  280. /// Postcondition Exception.
  281. /// </summary>
  282. public PostconditionException(string message, Exception inner) : base(message, inner)
  283. {
  284. }
  285. }
  286. /// <summary>
  287. /// Exception raised when an invariant fails.
  288. /// </summary>
  289. public class InvariantException : DesignByContractException
  290. {
  291. /// <summary>
  292. /// Invariant Exception.
  293. /// </summary>
  294. public InvariantException()
  295. {
  296. }
  297. /// <summary>
  298. /// Invariant Exception.
  299. /// </summary>
  300. public InvariantException(string message) : base(message)
  301. {
  302. }
  303. /// <summary>
  304. /// Invariant Exception.
  305. /// </summary>
  306. public InvariantException(string message, Exception inner) : base(message, inner)
  307. {
  308. }
  309. }
  310. /// <summary>
  311. /// Exception raised when an assertion fails.
  312. /// </summary>
  313. public class AssertionException : DesignByContractException
  314. {
  315. /// <summary>
  316. /// Assertion Exception.
  317. /// </summary>
  318. public AssertionException()
  319. {
  320. }
  321. /// <summary>
  322. /// Assertion Exception.
  323. /// </summary>
  324. public AssertionException(string message) : base(message)
  325. {
  326. }
  327. /// <summary>
  328. /// Assertion Exception.
  329. /// </summary>
  330. public AssertionException(string message, Exception inner) : base(message, inner)
  331. {
  332. }
  333. }
  334. #endregion // Exception classes
  335. } // End Design By Contract