@@ -14,35 +14,35 @@ namespace Simpleflow
1414 /// </summary>
1515 public partial class FunctionRegister : IFunctionRegister // ,IActivityInvoker
1616 {
17- // Key is name, and value is index, an index represents block (type store/method store)
18- // and index of store. 30th bit represents block, and rest of them as index (2**29) in block
19- // block - 1 represents _typeStore and 0 represents _methodStore
17+ // Key is name, and value is index, an index represents block (type store/method store/function provider Store)
18+ // and index of store. 29th and 30th bit represent block/store, and rest of them as index (2**28) in block
19+ // block - 0 represents _methodStore
20+ // block - 1 represents _providerStore
21+ // block - 2 represents ..unused..
22+ // block - 3 represents ..unused..
23+
24+ private const int MethodStoreIndex = 0 ;
25+ private const int ProviderStoreIndex = 1 ;
2026
2127 private readonly Dictionary < string , int > _bitmapIndex =
2228 new Dictionary < string , int > ( StringComparer . InvariantCultureIgnoreCase ) ;
2329
24- private readonly List < Type > _typeStore = new List < Type > ( ) ;
25- private readonly List < Delegate > _methodStore = new List < Delegate > ( ) ;
30+ private readonly List < Delegate > _methodStore = new List < Delegate > ( ) ;
31+ private readonly List < IFunctionProvider > _providerStore = new List < IFunctionProvider > ( ) ;
2632
2733 private readonly object _sync = new object ( ) ;
2834
29- /// <summary>
30- ///
31- /// </summary>
32- /// <param name="name"></param>
33- /// <param name="activity"></param>
34- /// <returns></returns>
35- ///
36- // Need to enable this later once this feature is implemented
37- private IFunctionRegister Add ( string name , Type activity )
35+
36+ public IFunctionRegister Add ( string name , Delegate @delegate )
3837 {
39- // TODO : able to invoke methods of the Type
40- // find all methods and add it
41- // let customer = $customer.new()
42- // $MethodName (p1: value, ...) on customer
43-
4438 ValidateFunctionName ( name ) ;
4539
40+ //Allow only static methods
41+ if ( @delegate . Target != null )
42+ {
43+ throw new SimpleflowException ( Resources . Message . RegisterNonStaticMethodError ) ;
44+ }
45+
4646 if ( _bitmapIndex . ContainsKey ( name ) )
4747 {
4848 throw new DuplicateFunctionException ( name ) ;
@@ -51,33 +51,26 @@ private IFunctionRegister Add(string name, Type activity)
5151 int index ;
5252 lock ( _sync )
5353 {
54- Debug . Assert ( _bitmapIndex . Count == _typeStore . Count + _methodStore . Count ) ;
54+ Debug . Assert ( _bitmapIndex . Count == _methodStore . Count + _providerStore . Count ) ;
5555
56- _typeStore . Add ( activity ) ;
57- index = _typeStore . Count - 1 ;
56+ _methodStore . Add ( @delegate ) ;
57+ index = _methodStore . Count - 1 ;
5858 }
59-
60- _bitmapIndex . Add ( name , 1 << 29 | index ) ;
59+ _bitmapIndex . Add ( name , CreateBitmapIndex ( storeIndex : MethodStoreIndex , valueIndex : index ) ) ;
6160
6261 return this ;
6362 }
6463
65- /// <summary>
66- ///
67- /// </summary>
68- /// <param name="name"></param>
69- /// <param name="delegate"></param>
70- /// <returns></returns>
71- public IFunctionRegister Add ( string name , Delegate @delegate )
64+
65+ public IFunctionRegister Add ( string name , IFunctionProvider functionProvider )
7266 {
73- //Allow only static methods
74- if ( @delegate . Target != null )
67+ ValidateFunctionName ( name ) ;
68+
69+ if ( functionProvider == null )
7570 {
76- throw new SimpleflowException ( Resources . Message . RegisterNonStaticMethodError ) ;
71+ throw new ArgumentNullException ( nameof ( functionProvider ) ) ;
7772 }
7873
79- ValidateFunctionName ( name ) ;
80-
8174 if ( _bitmapIndex . ContainsKey ( name ) )
8275 {
8376 throw new DuplicateFunctionException ( name ) ;
@@ -86,61 +79,61 @@ public IFunctionRegister Add(string name, Delegate @delegate)
8679 int index ;
8780 lock ( _sync )
8881 {
89- Debug . Assert ( _bitmapIndex . Count == _typeStore . Count + _methodStore . Count ) ;
82+ Debug . Assert ( _bitmapIndex . Count == _methodStore . Count + _providerStore . Count ) ;
9083
91- _methodStore . Add ( @delegate ) ;
92- index = _methodStore . Count - 1 ;
84+ _providerStore . Add ( functionProvider ) ;
85+ index = _providerStore . Count - 1 ;
9386 }
94- // 0 << 29 | index , since 0 << 29 is 0,
95- // so here we don't need to use bitwise operation to add up together
96- _bitmapIndex . Add ( name , index ) ;
87+ _bitmapIndex . Add ( name , CreateBitmapIndex ( storeIndex : ProviderStoreIndex , valueIndex : index ) ) ;
9788
9889 return this ;
9990 }
10091
92+
10193 /// <inheritdoc />
102- public Delegate GetFunction ( string name )
94+ public FunctionPointer GetFunction ( string name , ArgumentInfo [ ] argumentInfo ) // ArgumentInfo
10395 {
10496 if ( _bitmapIndex . ContainsKey ( name ) )
10597 {
10698 var bitmapIndex = _bitmapIndex [ name ] ;
107-
108- var firstBit = bitmapIndex >> 29 ;
109- var index = ( firstBit << 29 ) ^ bitmapIndex ;
110-
111- // ReSharper disable once InconsistentlySynchronizedField
112- return firstBit == 1 ? null : _methodStore [ index ] ;
99+ ( int storeIndex , int valueIndex ) = GetStoreAndValueIndex ( bitmapIndex ) ;
100+
101+ return storeIndex switch
102+ {
103+ MethodStoreIndex => new FunctionPointer { Reference = _methodStore [ valueIndex ] } ,
104+ ProviderStoreIndex => _providerStore [ valueIndex ] . GetFunction ( name , argumentInfo ) ,
105+ _ => null
106+ } ;
113107 }
114108 return null ;
115109 }
116110
117- /// <summary>
118- ///
119- /// </summary>
120- /// <param name="name"></param>
121- /// <returns></returns>
122-
123- private bool ? IsFunctionAvailableInClass ( string name )
111+ private ( int storeIndex , int valueIndex ) GetStoreAndValueIndex ( int bitmapIndex )
124112 {
125- if ( _bitmapIndex . ContainsKey ( name ) )
126- {
127- var bitmapIndex = _bitmapIndex [ name ] ;
128- var firstBit = bitmapIndex >> 29 ;
113+ var storeIndex = bitmapIndex >> 28 ;
114+ var valueIndex = ( storeIndex << 28 ) ^ bitmapIndex ;
129115
130- return firstBit == 1 ;
131- }
132- return false ;
116+ return ( storeIndex , valueIndex ) ;
117+ }
118+
119+ private int CreateBitmapIndex ( int storeIndex , int valueIndex )
120+ {
121+ return storeIndex << 28 | valueIndex ;
133122 }
134123
135124 private void ValidateFunctionName ( string name )
136125 {
137- var pattern = "^[_]*[a-zA-Z][_a-zA-Z0-9]*([.][_]*[a-zA-Z][_a-zA-Z0-9]*)*$" ;
126+ if ( string . IsNullOrWhiteSpace ( name ) )
127+ {
128+ throw new ArgumentNullException ( nameof ( name ) ) ;
129+ }
138130
139- if ( ! System . Text . RegularExpressions . Regex . Match ( name , pattern ) . Success )
131+ var pattern = "^[_]*[a-zA-Z][_a-zA-Z0-9]*([.][_]*[a-zA-Z][_a-zA-Z0-9]*)*$" ;
132+
133+ if ( ! System . Text . RegularExpressions . Regex . Match ( name , pattern ) . Success )
140134 {
141135 throw new InvalidFunctionNameException ( name ) ;
142136 }
143137 }
144-
145138 }
146139}
0 commit comments