FormulaRegistry.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // Purpose: Keep track of all registered formula module types.
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. namespace FastReport.DataVisualization.Charting.Formulas
  11. {
  12. /// <summary>
  13. /// Keep track of all registered formula modules types.
  14. /// </summary>
  15. internal class FormulaRegistry : IServiceProvider
  16. {
  17. #region Fields
  18. // Storage for all registered formula modules
  19. internal Hashtable registeredModules = new Hashtable(StringComparer.OrdinalIgnoreCase);
  20. private Hashtable _createdModules = new Hashtable(StringComparer.OrdinalIgnoreCase);
  21. private ArrayList _modulesNames = new ArrayList();
  22. #endregion
  23. #region Methods
  24. /// <summary>
  25. /// Formula Registry public constructor
  26. /// </summary>
  27. public FormulaRegistry()
  28. {
  29. }
  30. /// <summary>
  31. /// Adds modules into the registry.
  32. /// </summary>
  33. /// <param name="name">Module name.</param>
  34. /// <param name="moduleType">Module class type.</param>
  35. public void Register(string name, Type moduleType)
  36. {
  37. // First check if module with specified name already registered
  38. if(registeredModules.Contains(name))
  39. {
  40. // If same type provided - ignore
  41. if(registeredModules[name].GetType() == moduleType)
  42. {
  43. return;
  44. }
  45. // Error - throw exception
  46. throw( new ArgumentException( SR.ExceptionFormulaModuleNameIsNotUnique( name ) ) );
  47. }
  48. // Add Module Name
  49. _modulesNames.Add(name);
  50. // Make sure that specified class support IFormula interface
  51. bool found = false;
  52. Type[] interfaces = moduleType.GetInterfaces();
  53. foreach(Type type in interfaces)
  54. {
  55. if(type == typeof(IFormula))
  56. {
  57. found = true;
  58. break;
  59. }
  60. }
  61. if(!found)
  62. {
  63. throw( new ArgumentException( SR.ExceptionFormulaModuleHasNoInterface));
  64. }
  65. // Add formula module to the hash table
  66. registeredModules[name] = moduleType;
  67. }
  68. /// <summary>
  69. /// Returns formula module registry service object.
  70. /// </summary>
  71. /// <param name="serviceType">Service AxisName.</param>
  72. /// <returns>Service object.</returns>
  73. [EditorBrowsableAttribute(EditorBrowsableState.Never)]
  74. object IServiceProvider.GetService(Type serviceType)
  75. {
  76. if(serviceType == typeof(FormulaRegistry))
  77. {
  78. return this;
  79. }
  80. throw (new ArgumentException( SR.ExceptionFormulaModuleRegistryUnsupportedType( serviceType.ToString())));
  81. }
  82. /// <summary>
  83. /// Returns formula module object by name.
  84. /// </summary>
  85. /// <param name="name">Formula Module name.</param>
  86. /// <returns>Formula module object derived from IFormula.</returns>
  87. public IFormula GetFormulaModule(string name)
  88. {
  89. // First check if formula module with specified name registered
  90. if(!registeredModules.Contains(name))
  91. {
  92. throw( new ArgumentException( SR.ExceptionFormulaModuleNameUnknown( name ) ) );
  93. }
  94. // Check if the formula module object is already created
  95. if(!_createdModules.Contains(name))
  96. {
  97. // Create formula module object
  98. _createdModules[name] =
  99. ((Type)registeredModules[name]).Assembly.
  100. CreateInstance(((Type)registeredModules[name]).ToString());
  101. }
  102. return (IFormula)_createdModules[name];
  103. }
  104. /// <summary>
  105. /// Returns the name of the module.
  106. /// </summary>
  107. /// <param name="index">Module index.</param>
  108. /// <returns>Module Name.</returns>
  109. public string GetModuleName( int index )
  110. {
  111. return (string)_modulesNames[index];
  112. }
  113. #endregion
  114. #region Properties
  115. /// <summary>
  116. /// Return the number of registered modules.
  117. /// </summary>
  118. public int Count
  119. {
  120. get
  121. {
  122. return _modulesNames.Count;
  123. }
  124. }
  125. #endregion
  126. }
  127. /// <summary>
  128. /// Interface which defines the set of standard methods and
  129. /// properties for each formula module
  130. /// </summary>
  131. internal interface IFormula
  132. {
  133. #region IFormula Properties and Methods
  134. /// <summary>
  135. /// Formula Module name
  136. /// </summary>
  137. string Name { get; }
  138. /// <summary>
  139. /// The first method in the module, which converts a formula
  140. /// name to the corresponding private method.
  141. /// </summary>
  142. /// <param name="formulaName">String which represent a formula name</param>
  143. /// <param name="inputValues">Arrays of doubles - Input values</param>
  144. /// <param name="outputValues">Arrays of doubles - Output values</param>
  145. /// <param name="parameterList">Array of strings - Formula parameters</param>
  146. /// <param name="extraParameterList">Array of strings - Extra Formula parameters from DataManipulator object</param>
  147. /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
  148. void Formula(string formulaName, double [][] inputValues, out double [][] outputValues, string [] parameterList, string [] extraParameterList, out string [][] outLabels );
  149. #endregion
  150. }
  151. }