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.6KB

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