Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Container<T:Any>{
- var _memory:Any
- var memory:T {
- get {
- if let typed_value = self._memory as? T {
- return typed_value
- } else {
- return unsafeBitCast( self._memory, T.self )
- }
- }
- set {
- self._memory = newValue
- }
- }
- init( memory:Any ){
- self._memory = memory
- }
- }
- var Static_Containers = [String:AnyObject]()
- func static_var <T:Any>(
- value:T,
- file: StaticString = __FILE__,
- line: UWord = __LINE__,
- col: UWord = __COLUMN__,
- fun: StaticString = __FUNCTION__
- ) -> Container<T> {
- let unique_key = "FUNC_(fun)__LINE(line)_COL(col)__FILE_(file)"
- let needs_init = !contains( Static_Containers.keys, unique_key )
- if needs_init {
- Static_Containers[unique_key] = Container<T>( memory:value )
- }
- return Static_Containers[unique_key]! as! Container<T>
- }
- func test_with_nsstring( str:NSString, init_only:Bool ) -> NSString {
- var stat_str = static_var( str )
- if !init_only {
- stat_str.memory = str
- }
- return stat_str.memory
- }
- test_with_nsstring( "this should get set", true )
- test_with_nsstring( "this should be ignored", true )
- test_with_nsstring( "this should change the value", false )
- test_with_nsstring( "as should this", false )
- func test_with_int( i:Int, init_only:Bool ) -> Int {
- var stat_int = static_var( i )
- if !init_only {
- stat_int.memory = i
- }
- return stat_int.memory
- }
- test_with_int( 0, true )
- test_with_int( 1, true )
- test_with_int( 2, false )
- test_with_int( 3, false )
- func test_with_optstr( optstr:String?, init_only:Bool ) -> String? {
- var stat_optstr = static_var( optstr )
- if !init_only {
- stat_optstr.memory = optstr
- }
- return stat_optstr.memory
- }
- test_with_optstr( nil, true )
- test_with_optstr( "this should be ignored", true )
- test_with_optstr( "this should change the value", false )
- test_with_optstr( "as should this", false )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement