Protected Variables with Access Managment are possible - Keep it simple :D

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Protected Variables with Access Managment are possible - Keep it simple :D

Post by mrminecrafttnt »

Ive found a very simple method to add an accsees managment system for strings, this should work with other datatypes too. :)
Have a nice day!
Update: I think that i have all bug fixed :)

Code: Select all

#undef true
#undef false
const true = 1
const false = 0

Type ProtectedString
    private:
    protectetstringdata as string
    readauthorized as byte
    writeauthorized as byte
    public:
    Declare Operator Cast() As String
    Declare Operator Let ( ByVal O as String )
    
    'Auth
    Declare Sub AuthorizeRead
    Declare Sub AuthorizeWrite
    
    'DeAuth
    Declare Sub DeAuthorizeRead
    Declare Sub DeAuthorizeWrite
    
End Type

Sub ProtectedString.DeAuthorizeWrite
    writeauthorized = false
    Print "DEBUG -> WRITE ACCESS DEAUTHORIZED"
end sub

Sub ProtectedString.DeAuthorizeRead
    readauthorized = false
    Print "DEBUG -> READ ACCESS DEAUTHORIZED"
end sub

Sub ProtectedString.AuthorizeWrite
    writeauthorized = true
    Print "DEBUG -> WRITE ACCESS AUTHORIZED"
end sub

Sub ProtectedString.AuthorizeRead
    readauthorized = true
    Print "DEBUG -> READ ACCESS AUTHORIZED"
end sub

Operator ProtectedString.cast () as string
    if readauthorized = false then 
        Print "DEBUG READ -> READ ACCESS DENINED"
        return ""
    else
        return protectetstringdata 
    end if
end operator

Operator ProtectedString.let ( ByVal O as String)
    if writeauthorized = false then
        Print "DEBUG WRITE -> WRITE ACCESS DENINED -> '"+o+"'"
    else
        
        protectetstringdata = o
    end if
end operator

'Example
dim as protectedstring test
PRINT "This should not work because access is disabled by default"
test = "HI"
print test

PRINT "But when we enable access are all things possible :)"
test.authorizewrite
test.authorizeread

PRINT "But this should work"
test = "HELLO" + "MAIN"
print test
sleep
end
Post Reply