OSDN Git Service

be5f431b869542eacab7ae9250a700e8afb8e969
[pettanr/clientJs.git] / 0.6.x / js / core / 04_XClass.js
1 /**\r
2  * Class を定義し システムの管理下に置く.\r
3  * 全てのクラスと pool が有効の場合インスタンスへの参照が保持される.\r
4  *  1. X.Class.create( def, opt_final, opt_pool, opt_abstract ) でクラスを登録.\r
5  *  2. コンストラクタ となるメソッドは、Constructor : function( arg ){ ... }, に書く.\r
6  *  3. 通常通り new で インスタンス生成\r
7  *  4. kill() でオブジェクトをクリーンして削除、pool が有効の場合は pool される.\r
8  *  5. pool が有効の場合、new で pool されたインスタンスが返される.\r
9  *  6. \r
10  * \r
11  */\r
12 X.Class = ( function(){\r
13         var CLASS_LIST         = [],\r
14                 DEF_LIST           = [],\r
15                 PRIVATE_CLASS_LIST = [],\r
16                 PRIVATE_DEF_LIST   = [],\r
17                 CONSTRUCTOR        = 'Constructor',\r
18                 killPrivateFlag    = false,\r
19                 traits             = null,\r
20                 _slice             = Array.prototype.slice;\r
21         \r
22         /* サブクラスを作るメソッド  \r
23          * var subClass = superClass.inherits( ... ) \r
24          * http://d.hatena.ne.jp/m-hiyama/20051018/1129605002\r
25          */\r
26         function inherits( /* displayName, classSetting, opt_PrivateClass, props */ ){\r
27                 var args        = X.copyArray( arguments ),\r
28                         params      = [],\r
29                         Super       = this,\r
30                         superDef    = X.Class._getClassDef( Super ),\r
31                         displayName = args[ 0 ],\r
32                         classSetting,\r
33                         opt_super,\r
34                         klass, def;\r
35                 if( superDef.Final ) X.Notification.critical( 'X.Class inherits, Class is final!' );\r
36                 \r
37                 // サブクラス名\r
38                 if( X.Type.isString( displayName ) ){\r
39                         args.shift();\r
40                 } else {\r
41                         displayName = 'SubClass of ' + superDef.displayName;\r
42                 };\r
43                 params.push( displayName );\r
44                 \r
45                 // サブクラス設定\r
46                 classSetting = args[ 0 ];\r
47                 if( X.Type.isNumber( classSetting ) ){\r
48                         args.shift();\r
49                 } else {\r
50                         classSetting = superDef.setting;\r
51                 };\r
52                 if( superDef.isPrivate ) classSetting = classSetting | X.Class.PRIVATE_DATA;\r
53                 opt_super = !!( classSetting & X.Class.SUPER_ACCESS );\r
54                 params.push( classSetting );\r
55 \r
56                 // サブクラスのシャドウ\r
57                 if( X.Class._getClass( args[ 0 ] ) ){\r
58                         params.push( args.shift() );\r
59                 } else\r
60                 if( superDef.privateClass ){\r
61                         params.push( superDef.privateClass );\r
62                 };\r
63                 params.push( args[ 0 ] ); /* props サブクラスでは未定義でも可 */\r
64                 \r
65                 // 継承クラスの作成\r
66                 Super.__new = null;\r
67                 traits = new Super();\r
68                 Super.__new = C;\r
69                 klass  = X.Class.create.apply( X.Class, params );\r
70                 traits = null;\r
71                 \r
72                 // 継承用プロパティを控える\r
73                 if( opt_super === true ){\r
74                         def = X.Class._getClassDef( klass );\r
75                         def.Super = Super;\r
76                         def.SuperProto = Super.prototype;\r
77                         def.SuperConstructor = superDef[ CONSTRUCTOR ] || superDef.SuperConstructor;\r
78                 };\r
79                 \r
80                 return klass;\r
81         };\r
82         \r
83         /* X.Class.create で作られたクラスのインスタンスが共通で備えるメソッド */\r
84         var CommonProps = {\r
85                 kill : function(){\r
86                         var instance = this,\r
87                                 klass    = X.Class._getClass( instance ),\r
88                                 def      = X.Class._getClassDef( klass ),\r
89                                 data, p, i;\r
90                         if( def.isPrivate === true && killPrivateFlag === false ){\r
91                                 X.Notification.critical( 'PrivateInstance.kill() work in PrivateUser.kill().' );\r
92                                 return;\r
93                         };\r
94                         killPrivateFlag = false; // onKill 内で PrivateInstance.kill() を防ぐため\r
95                         // onKill() === false の場合、kill のキャンセル\r
96                         // private      は false での キャンセル は無視される\r
97                         if( X.Type.isFunction( instance.onKill ) && instance.onKill() === false && !def.isPrivate ){\r
98                                 return;\r
99                         };\r
100                         for( p in instance ){\r
101                                 if( instance.hasOwnProperty && !instance.hasOwnProperty( p ) ) continue;\r
102                                 delete instance[ p ];\r
103                         };\r
104                         if( def.pool ){\r
105                                 def.live && def.live.splice( def.live.indexOf( instance ), 1 );\r
106                                 def.pool[ def.pool.length ] = instance;\r
107                         };\r
108                         if( def.privateClass ){\r
109                                 i = def.userList.indexOf( instance );\r
110                                 if( i !== -1 ){\r
111                                         data            = X.Class._getPrivate( instance );\r
112                                         killPrivateFlag = true;\r
113                                         data.kill();\r
114                                         def.dataList.splice( i, 1 );\r
115                                         def.userList.splice( i, 1 );\r
116                                 };\r
117                         };\r
118                 },\r
119                 instanceOf : function( klass ){\r
120                         var Super = this;\r
121                         if( this.constructor === klass ) return true;\r
122                         while( Super = X.Class._getClassDef( Super ).Super ){\r
123                                 if( Super === klass ) return true;\r
124                         };\r
125                         return false;\r
126                 }\r
127         };\r
128         \r
129         /*\r
130          * new の実体.コンストラクタの機能は instance.Constructor に書く.\r
131          * これにより pool された オブジェクト(破棄されたインスタンス) を再利用できる\r
132          */\r
133         /* Constructor Real for GeneralClass */\r
134         function C( args ){\r
135                 var klass    = this,\r
136                         def      = X.Class._getClassDef( klass ),\r
137                         dataUser = def._tempUser,\r
138                         instance,\r
139                         userDef;\r
140                 if( def.Abstract === true ){\r
141                         X.Notification.critical( 'AbstractClass!' );\r
142                         return;\r
143                 };\r
144                 if( def.isPrivate === true && dataUser === null ){\r
145                         X.Notification.critical( 'use myClass.newPrivate( instance, ...args )!' );\r
146                         return;\r
147                 };\r
148                 klass.__new = null;\r
149                 instance = def.pool && def.pool.length > 0 ? def.pool.pop() : new klass();\r
150                 klass.__new = C;\r
151                 if( def.isPrivate === true ){\r
152                         userDef = X.Class._getClassDef( dataUser );\r
153                         userDef.dataList.push( instance );\r
154                         userDef.userList.push( dataUser );\r
155                         instance.User = dataUser;\r
156                         def._tempUser = null;\r
157                 } else {\r
158                         def.live && def.live.push( instance );\r
159                 };\r
160                 if( def.Super ){\r
161                         // klass.prototype に移動\r
162                         instance.Super = def.SuperProto;\r
163                         instance.SuperConstructor = superConstructor;\r
164                 };\r
165                 def[ CONSTRUCTOR ] ?\r
166                         def[ CONSTRUCTOR ].apply( instance, args ) :\r
167                 def.SuperConstructor &&\r
168                         def.SuperConstructor.apply( instance, args );\r
169                 return instance;\r
170         };\r
171         \r
172         function superConstructor(){\r
173                 var s = X.Class._getClassDef( this ).SuperConstructor;\r
174                 s && s.apply( this, arguments );\r
175         };\r
176         \r
177         return {\r
178                 POOL_OBJECT  : 1,\r
179                 ABSTRACT     : 2,\r
180                 FINAL        : 4,\r
181                 SUPER_ACCESS : 8,\r
182                 PRIVATE_DATA : 16,\r
183                 create : function( /* displayName, classSetting, opt_PrivateClass, props */ ){\r
184                         var args        = X.copyArray( arguments ),\r
185                                 displayName = args[ 0 ],\r
186                                 classSetting,\r
187                                 opt_pool, opt_abstract, opt_final, opt_private,\r
188                                 privateDef,\r
189                                 props,\r
190                                 klass,\r
191                                 classDef = {};\r
192                         if( X.Type.isString( displayName ) === true ){\r
193                                 classDef.displayName = displayName;\r
194                                 args.shift();\r
195                         };\r
196                         \r
197                         // クラス設定\r
198                         classDef.setting = classSetting = args[ 0 ];\r
199                         if( X.Type.isNumber( classSetting ) ){\r
200                                 opt_pool     = !!( classSetting & X.Class.POOL_OBJECT  );\r
201                                 opt_abstract = !!( classSetting & X.Class.ABSTRACT     );\r
202                                 opt_final    = !!( classSetting & X.Class.FINAL        );\r
203                                 opt_private  = !!( classSetting & X.Class.PRIVATE_DATA );\r
204                                 if( opt_final && opt_abstract ){\r
205                                         X.Notification.critical( 'final & Abstract!' );\r
206                                         return;\r
207                                 };      \r
208                                 args.shift();\r
209                         } else {\r
210                                 classDef.setting = 0;\r
211                         };\r
212                         \r
213                         // シャドウクラス\r
214                         if( PRIVATE_CLASS_LIST.indexOf( args[ 0 ] ) !== -1 ){\r
215                                 privateDef = X.Class._getClassDef( args[ 0 ] );\r
216                                 if( privateDef.isPrivate !== true ){\r
217                                         X.Notification.critical( 'PrivateClass not found! please, X.Class.create( X.Class.PRIVATE, {...} ).' );\r
218                                         return;\r
219                                 } else\r
220                                 if( privateDef.Abstract === true ){\r
221                                         X.Notification.critical( 'PrivateClass is Abstract!' );\r
222                                         return;\r
223                                 };\r
224                                 classDef.privateClass = args.shift();\r
225                         };\r
226                         \r
227                         // インスタンスのメンバー\r
228                         props = args[ 0 ];\r
229                         if( props === null || !X.Type.isObject( props ) ){\r
230                                 // サブクラスの場合、クラス定義の上書きがなくても作成可能\r
231                                 if( !traits ){\r
232                                         X.Notification.critical( 'No Class Def!' );\r
233                                         return;\r
234                                 };\r
235                                 props = {};\r
236                         } else\r
237                         if( props[ CONSTRUCTOR ] && X.Type.isFunction( props[ CONSTRUCTOR ] ) ){\r
238                                 classDef[ CONSTRUCTOR ] = props[ CONSTRUCTOR ];\r
239                         };\r
240 \r
241                         klass = new Function( 'var a=arguments,f=a.callee;if(f.__new)return f.__new(a)' );\r
242                         klass.__new = C;\r
243                         if( X.UA.Safari && X.UA.Safari < 2 ){\r
244                                 traits && X.Class._override( klass.prototype, traits, false );\r
245                                 X.Class._override( X.Class._override( klass.prototype, CommonProps, false ), props, true );\r
246                         } else {\r
247                                 klass.prototype = X.Class._override( X.Class._override( traits || klass.prototype, props, true ), CommonProps, false );\r
248                                 klass.prototype.constructor = klass;\r
249                         };\r
250                         klass.name = displayName;\r
251                         \r
252                         if( opt_abstract === true ){\r
253                                 classDef.Abstract = true;\r
254                         } else\r
255                         if( opt_pool === true ){\r
256                                 classDef.pool = [];\r
257                                 if( opt_private === false ) classDef.live = [];\r
258                         };                      \r
259                         if( opt_final === true ){\r
260                                 classDef.Final = true;\r
261                         } else {\r
262                                 klass.inherits = inherits;\r
263                         };                      \r
264                         if( opt_private === true ){\r
265                                 if( classDef.privateClass ){\r
266                                         X.Notification.critical( 'Private Data Class has no PrivateClass!' );\r
267                                         return;\r
268                                 };\r
269                                 classDef.isPrivate = true;\r
270                                 PRIVATE_CLASS_LIST.push( klass );\r
271                                 PRIVATE_DEF_LIST.push( classDef );\r
272                         } else {\r
273                                 CLASS_LIST.push( klass );\r
274                                 DEF_LIST.push( classDef );                              \r
275                         };\r
276                         return klass;\r
277                 },\r
278                 sys_shutdown : function(){\r
279                         \r
280                 },\r
281                 _getClass : function( instance ){\r
282                         var cList    = CLASS_LIST,\r
283                                 i        = cList.length,\r
284                                 klass;\r
285                         for( ; i; ){\r
286                                 klass = cList[ --i ];\r
287                                 if( instance.constructor === klass ) return klass;\r
288                         };\r
289                         cList = PRIVATE_CLASS_LIST;\r
290                         i     = cList.length;\r
291                         for( ; i; ){\r
292                                 klass = cList[ --i ];\r
293                                 if( instance.constructor === klass ) return klass;\r
294                         };\r
295                         \r
296                         if( cList.indexOf( instance ) !== -1 ) return instance;\r
297                         if( CLASS_LIST.indexOf( instance ) !== -1 ) return instance;\r
298                 },\r
299                 _getClassDef : function( KlassOrInstance ){\r
300                         var i = CLASS_LIST.indexOf( KlassOrInstance );\r
301                         if( i === -1 ) i = CLASS_LIST.indexOf( X.Class._getClass( KlassOrInstance ) );\r
302                         if( i !== -1 ) return DEF_LIST[ i ];\r
303                         \r
304                         i = PRIVATE_CLASS_LIST.indexOf( KlassOrInstance );\r
305                         if( i === -1 ) i = PRIVATE_CLASS_LIST.indexOf( X.Class._getClass( KlassOrInstance ) );\r
306                         if( i !== -1 ) return PRIVATE_DEF_LIST[ i ];\r
307                         \r
308                         if( DEF_LIST.indexOf( KlassOrInstance ) !== -1 ) return KlassOrInstance;\r
309                         if( PRIVATE_DEF_LIST.indexOf( KlassOrInstance ) !== -1 ) return KlassOrInstance;\r
310                 },\r
311                 _newPrivate : function( /* instance, args */ ){\r
312                         var args         = X.copyArray( arguments ),\r
313                                 user         = args.shift(),\r
314                                 def          = X.Class._getClassDef( user ),\r
315                                 privateClass = def.privateClass,\r
316                                 privateDef   = X.Class._getClassDef( privateClass ),\r
317                                 i            = -1;\r
318                         if( def.userList ){\r
319                                 i = def.userList.indexOf( user );\r
320                         } else {\r
321                                 def.userList = [];\r
322                                 def.dataList = [];\r
323                         };\r
324                         if( i !== -1 ){\r
325                                 X.Notification.critical( 'PrivateData already exist!' );\r
326                                 return;\r
327                         };\r
328                         if( privateDef._tempUser ){\r
329                                 X.Notification.critical( 'newPrivate を連続呼び出しされたところ破綻' );\r
330                                 return;\r
331                         };\r
332                         privateDef._tempUser = user;\r
333                         return privateClass.__new( args );\r
334                 },\r
335                 _getPrivate : function( instance ){\r
336                         var def = X.Class._getClassDef( instance ),\r
337                                 i   = def.userList.indexOf( instance );\r
338                         if( i !== -1 ) return def.dataList[ i ];\r
339                 },\r
340                 /* over のプロパティを target にコピーする.ただし target の プロパティが優先, force で解除 */\r
341                 _override : function ( target, src, force ){\r
342                         var p;\r
343                         for( p in src ){\r
344                                 if( p === 'Super' || p === 'SuperConstructor' ){\r
345                                         X.Notification.critical( 'Super & SuperConstructor is reserved!' );\r
346                                         return;\r
347                                 };\r
348                                 if( force || target[ p ] === void 0 ){\r
349                                         target[ p ] = src[ p ];\r
350                                 };\r
351                         };\r
352                         return target;\r
353                 }\r
354         };\r
355 })();