Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Web.DataVisualization / Common / Formulas / FormulaRegistry.cs
1 //-------------------------------------------------------------
2 // <copyright company=\92Microsoft Corporation\92>
3 //   Copyright © Microsoft Corporation. All Rights Reserved.
4 // </copyright>
5 //-------------------------------------------------------------
6 // @owner=alexgor, deliant
7 //=================================================================
8 //  File:               FormulaRegistry.cs
9 //
10 //  Namespace:  System.Web.UI.WebControls[Windows.Forms].Charting.Formulas
11 //
12 //      Classes:        FormulaRegistry
13 //
14 //  Purpose:    Keep track of all registered formula module types.
15 //
16 //      Reviewed:       GS - August 6, 2002
17 //                              AG - August 7, 2002
18 //
19 //===================================================================
20
21
22 #region Used namespace
23
24 using System;
25 using System.Collections;
26 using System.ComponentModel;
27 using System.ComponentModel.Design;
28
29 #endregion
30
31
32
33 #if Microsoft_CONTROL
34         namespace System.Windows.Forms.DataVisualization.Charting.Formulas
35 #else
36         namespace System.Web.UI.DataVisualization.Charting.Formulas
37 #endif
38 {
39         /// <summary>
40         /// Keep track of all registered formula modules types.
41         /// </summary>
42         internal class FormulaRegistry : IServiceProvider
43         {
44                 #region Fields
45
46                 // Storage for all registered formula modules
47                 internal        Hashtable       registeredModules = new Hashtable(StringComparer.OrdinalIgnoreCase);
48         private Hashtable _createdModules = new Hashtable(StringComparer.OrdinalIgnoreCase);
49                 private         ArrayList       _modulesNames = new ArrayList();
50
51                 #endregion
52
53                 #region Methods
54
55                 /// <summary>
56                 /// Formula Registry public constructor
57                 /// </summary>
58                 public FormulaRegistry()
59                 {
60                 }
61
62                 /// <summary>
63                 /// Adds modules into the registry.
64                 /// </summary>
65                 /// <param name="name">Module name.</param>
66                 /// <param name="moduleType">Module class type.</param>
67                 public void Register(string name, Type moduleType)
68                 {
69                         // First check if module with specified name already registered
70                         if(registeredModules.Contains(name))
71                         {
72                                 // If same type provided - ignore
73                                 if(registeredModules[name].GetType() == moduleType)
74                                 {
75                                         return;
76                                 }
77
78                                 // Error - throw exception
79                                 throw( new ArgumentException( SR.ExceptionFormulaModuleNameIsNotUnique( name ) ) );
80                         }
81
82                         // Add Module Name
83                         _modulesNames.Add(name);
84
85                         // Make sure that specified class support IFormula interface
86                         bool    found = false;
87                         Type[]  interfaces = moduleType.GetInterfaces();
88                         foreach(Type type in interfaces)
89                         {   
90                                 if(type == typeof(IFormula))
91                                 {
92                                         found = true;
93                                         break;
94                                 }
95                         }
96                         if(!found)
97                         {
98                                 throw( new ArgumentException( SR.ExceptionFormulaModuleHasNoInterface));
99                         }
100
101                         // Add formula module to the hash table
102                         registeredModules[name] = moduleType;
103                 }
104                 
105                 /// <summary>
106                 /// Returns formula module registry service object.
107                 /// </summary>
108                 /// <param name="serviceType">Service AxisName.</param>
109                 /// <returns>Service object.</returns>
110                 [EditorBrowsableAttribute(EditorBrowsableState.Never)]
111                 object IServiceProvider.GetService(Type serviceType)
112                 {
113                         if(serviceType == typeof(FormulaRegistry))
114                         {
115                                 return this;
116                         }
117                         throw (new ArgumentException( SR.ExceptionFormulaModuleRegistryUnsupportedType( serviceType.ToString())));
118                 }
119
120                 /// <summary>
121                 /// Returns formula module object by name.
122                 /// </summary>
123                 /// <param name="name">Formula Module name.</param>
124                 /// <returns>Formula module object derived from IFormula.</returns>
125                 public IFormula GetFormulaModule(string name)
126                 {
127                         // First check if formula module with specified name registered
128                         if(!registeredModules.Contains(name))
129                         {
130                                 throw( new ArgumentException( SR.ExceptionFormulaModuleNameUnknown( name ) ) );
131                         }
132
133                         // Check if the formula module object is already created
134                         if(!_createdModules.Contains(name))
135                         {       
136                                 // Create formula module object
137                                 _createdModules[name] = 
138                                         ((Type)registeredModules[name]).Assembly.
139                                         CreateInstance(((Type)registeredModules[name]).ToString());
140                         }
141
142                         return (IFormula)_createdModules[name];
143                 }
144
145                 /// <summary>
146                 /// Returns the name of the module.
147                 /// </summary>
148                 /// <param name="index">Module index.</param>
149                 /// <returns>Module Name.</returns>
150                 public string GetModuleName( int index )
151                 {
152                         return (string)_modulesNames[index];
153                 }
154
155                 #endregion
156
157                 #region Properties
158
159                 /// <summary>
160                 /// Return the number of registered modules.
161                 /// </summary>
162                 public int Count
163                 {
164                         get
165                         {
166                                 return _modulesNames.Count;
167                         }
168                 }
169                 
170                 #endregion
171         }
172
173     /// <summary>
174     /// Interface which defines the set of standard methods and
175     /// properties for each formula module
176     /// </summary>
177         internal interface IFormula
178         {
179                 #region IFormula Properties and Methods
180
181                 /// <summary>
182                 /// Formula Module name
183                 /// </summary>
184                 string Name                     { get; }
185
186         /// <summary>
187         /// The first method in the module, which converts a formula 
188         /// name to the corresponding private method.
189         /// </summary>
190         /// <param name="formulaName">String which represent a formula name</param>
191         /// <param name="inputValues">Arrays of doubles - Input values</param>
192         /// <param name="outputValues">Arrays of doubles - Output values</param>
193         /// <param name="parameterList">Array of strings - Formula parameters</param>
194         /// <param name="extraParameterList">Array of strings - Extra Formula parameters from DataManipulator object</param>
195         /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
196                 void Formula(string formulaName, double [][] inputValues, out double [][] outputValues, string [] parameterList, string [] extraParameterList, out string [][] outLabels  );
197
198                 #endregion
199         }
200 }
201