Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DBC.cs 9.5KB

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