Code: Select all
/' SafeString interface
::
'/
'' A wrapper for STRING allowing correct byval/byref behavior and
'' providing NEW/NEW[]/DELETE/DELETE[] support.
type SafeString
public:
'' Constructs from another string (if possible).
declare constructor (byval zs as zstring ptr = 0)
'' (default copy ctor, copy assignment and destructor are fine.)
'' Gets a reference (pointer) to the underlying string.
declare function Reference () as string ptr
'' Gets the value of the underlying string.
declare function Value () as string
'' Gets the address of the character data.
declare function GetData () as zstring ptr
' Gets the length of the string in characters.
declare function GetLength () as integer
'' Implicitly casts to a string value, making usage with most runtime
'' library procedures transparent.
declare operator cast () as string
private:
m_impl as string
end type
'' Mirrors SafeString.Reference, provided only to make referencing the
'' underlying string easier.
declare operator * (byref as SafeString) as string ptr
/' SafeString implementation
::
'/
'' :::::
constructor SafeString (byval zs as zstring ptr)
m_impl = *iif(zs, zs, @"")
end constructor
'' :::::
function SafeString.Reference () as string ptr
return @m_impl
end function
'' :::::
function SafeString.Value () as string
return m_impl
end function
'' :::::
function SafeString.GetData () as zstring ptr
return strptr(m_impl)
end function
'' :::::
function SafeString.GetLength () as integer
return len(m_impl)
end function
'' :::::
operator * (byref self as SafeString) as string ptr
return self.Reference()
end operator
'' :::::
operator SafeString.cast () as string
return m_impl
end operator
/' SafeString demo
::
'/
'' Passing convention testors.
sub f (byval ss as SafeString)
ss = "modified"
end sub
sub g (byref ss as SafeString)
ss = "modified"
end sub
'' ::::: (main)
' easy initialization with NEW/NEW[] support..
dim s1 as SafeString = "Hello, "
var s2 = new SafeString("world!")
print s1, *s2
' indexing..
(**s1)[0] = asc("h")
(***s2)[0] = asc("W")
print s1, *s2
' concatenation..
s1 &= *s2
print s1, *s2
' safe to pass by value..
f(s1) : g(*s2)
print s1, *s2
' DELETE/DELETE[] support..
delete s2
edit: sorry, meant to put this in tips and tricks, must have pushed the wrong button.
edit2: added SafeString.GetData/.GetLength