Lean  $LEAN_TAG$
RegisteredSecurityDataTypesProvider.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using System.Collections.Generic;
19 
21 {
22  /// <summary>
23  /// Provides an implementation of <see cref="IRegisteredSecurityDataTypesProvider"/> that permits the
24  /// consumer to modify the expected types
25  /// </summary>
27  {
28  /// <summary>
29  /// Provides a reference to an instance of <see cref="IRegisteredSecurityDataTypesProvider"/> that contains no registered types
30  /// </summary>
32 
33  private readonly Dictionary<string, Type> _types = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
34 
35  /// <summary>
36  /// Registers the specified type w/ the provider
37  /// </summary>
38  /// <returns>True if the type was previously not registered</returns>
39  public bool RegisterType(Type type)
40  {
41  lock (_types)
42  {
43  Type existingType;
44  if (_types.TryGetValue(type.Name, out existingType))
45  {
46  if (existingType != type)
47  {
48  // shouldn't happen but we want to know if it does
49  throw new InvalidOperationException(
50  Messages.RegisteredSecurityDataTypesProvider.TwoDifferentTypesDetectedForTheSameTypeName(type, existingType));
51  }
52  return true;
53  }
54 
55  _types[type.Name] = type;
56  return false;
57  }
58  }
59 
60  /// <summary>
61  /// Removes the registration for the specified type
62  /// </summary>
63  /// <returns>True if the type was previously registered</returns>
64  public bool UnregisterType(Type type)
65  {
66  lock (_types)
67  {
68  return _types.Remove(type.Name);
69  }
70  }
71 
72  /// <summary>
73  /// Gets an enumerable of data types expected to be contained in a <see cref="DynamicSecurityData"/> instance
74  /// </summary>
75  public bool TryGetType(string name, out Type type)
76  {
77  lock (_types)
78  {
79  return _types.TryGetValue(name, out type);
80  }
81  }
82  }
83 }