For example with Wstring:
Dim As Wstring Ptr pw = Callocate(length+1, Sizeof(Wstring))
(with Sizeof(Wstring) = 2 in that case)
[edit]
Better syntax without warning.
Well, to me adding a (mul 1) is simply a waste of resources ...fxm wrote:Because that is more generic for any fixed string.
Code: Select all
' ========================================================================================
' One * returns the address of the CWSTR buffer.
' Two ** deferences the string data.
' Needed because LEFT and RIGHT (cws) fail with an ambiguous call error.
' We have to use **cws (notice the double indirection) with these functions.
' ========================================================================================
PRIVATE OPERATOR * (BYREF cws AS CWSTR) AS WSTRING PTR
OPERATOR = cast(WSTRING PTR, cws.m_pBuffer)
END OPERATOR
' ========================================================================================
Code: Select all
DIM cws AS CWSTR
cws = "1234567890"
MID(**cws, 3, 1) = "x"
print cws
Code: Select all
Type UDTzstring
Declare Constructor (Byval length As Integer)
Declare Destructor ()
Declare Operator Cast () Byref As Zstring
Dim As Zstring Ptr pz
End Type
Constructor UDTzstring (Byval length As Integer)
This.pz = Callocate(length+1 * Sizeof(Zstring))
End Constructor
Destructor UDTzstring ()
Deallocate(This.pz)
End Destructor
Operator UDTzstring.Cast () Byref As Zstring
Print "called: UDTzstring.Cast() Byref As Zstring"
Return *This.pz
End Operator
Dim As UDTzstring u = UDTzstring(16)
*u.pz = "Free 1.06.0"
Print *u.pz
'Mid(Cast(Zstring, u), 5, 6) = "Basic" '' Cast(Zstring, u) not alloced
Mid(u, 5, 6) = "Basic"
Print *u.pz
Sleep
Code: Select all
Free 1.06.0
called: UDTzstring.Cast() Byref As Zstring
FreeBasic 1.06.0
Code: Select all
Type UDTwstring
Declare Constructor (Byval length As Integer)
Declare Destructor ()
Declare Operator Cast () Byref As Wstring
Dim As Wstring Ptr pw
End Type
Constructor UDTwstring (Byval length As Integer)
This.pw = Callocate(length+1, Sizeof(Wstring))
End Constructor
Destructor UDTwstring ()
Deallocate(This.pw)
End Destructor
Operator UDTwstring.Cast () Byref As Wstring
Print "called: UDTwstring.Cast() Byref As Wstring"
Return *This.pw
End Operator
Dim As UDTWstring u = UDTwstring(16)
*u.pw = "Free 1.06.0"
Print *u.pw
'Mid(Cast(Wstring, u), 5, 6) = "Basic" '' Cast(Wstring, u) not allowed
Mid(u, 5, 6) = "Basic"
Print *u.pw
Sleep
Code: Select all
Free 1.06.0
called: UDTwstring.Cast() Byref As Wstring
Free 1.06.0
So, that works presently as said dkl ('Byref As Zstring function result can now be used with Print or string assignments'), but in the particular case of implicit casting by modifying the referred variable, that works only for Zstring and not for Wstring.dkl wrote:Looks like allowing cast(zstring, ...) was a bug introduced during the 64bit port. I've fixed it for now, though ultimately we may end up allowing zstring to be used as data type on its own (not just with Byref) anyways, for example in order to fix #650. And it makes sense to allow it when dealing with Byref things anyways.
.....
This works since 00d8aa37 (found via git bisect), and while it surely wasn't directly intended, it's an indirect side-effect and makes sense to me. There will be an implicit dereference done for the Byref As Zstring function result, and thanks to the overload resolution fixes related to that, the Byref As Zstring function result can now be used with Print or string assignments.
FB-DOC wrote: In procedure definitions, Private specifies that a procedure has internal linkage, meaning its name is not visible to external modules.
(gives a syntax error in: -lang "fb") MUST BE WRITTEN HERE! (I thought, it was a workaround for -lang "fb")
The Option Private statement allows procedures to be defined with internal linkage by default. (Page: Option Private: states clearly, NOT allowed in -lang "fb")
Code: Select all
Declare Private Sub DoNothing() '' (1) error 17: Syntax error, found 'Private' in 'Declare Private Sub DoNothing()'
Declare Private Function Ret_One() As UInteger '' (2) error 17: Syntax error, found 'Private' in 'Declare Private Function Ret_One() As UInteger'
' ----- Start Prog. ----- '
Sleep 5000, 1 ' <--- just wait 5 Sec. before 'quitting'
' ----- END Prog. ----- '
' implementation(s)
Private Sub DoNothing()
' a lot of nothing
End Sub
Private Function Ret_One() As UInteger
Return 1
End Function
' ----- EOF ----- '
Yes, this I've read, somewhere in DOC. That's the reason, I've been puzzled ...fxm wrote:Per default, all procedures are public.
This should be added to DOC on page: Private (IMO). Also, that it works on -lang "fb" (as opposed to: Option Private).fxm wrote:'Private / Public' must only be put in: the procedure implementation (NEVER: in the declaration)
Whether calling the Cast operator by static binding (code above) or by dynamic binding (code below with virtual Cast operator), the result is always bad as if the compiler always provided a wstring copy from the Cast operator return:fxm wrote:- For Wstring, implicit byref casting does not work for modifying the referred variable:Code: Select all
Type UDTwstring Declare Constructor (Byval length As Integer) Declare Destructor () Declare Operator Cast () Byref As Wstring Dim As Wstring Ptr pw End Type Constructor UDTwstring (Byval length As Integer) This.pw = Callocate(length+1, Sizeof(Wstring)) End Constructor Destructor UDTwstring () Deallocate(This.pw) End Destructor Operator UDTwstring.Cast () Byref As Wstring Print "called: UDTwstring.Cast() Byref As Wstring" Return *This.pw End Operator Dim As UDTWstring u = UDTwstring(16) *u.pw = "Free 1.06.0" Print *u.pw 'Mid(Cast(Wstring, u), 5, 6) = "Basic" '' Cast(Wstring, u) not allowed Mid(u, 5, 6) = "Basic" Print *u.pw Sleep
Code: Select all
Free 1.06.0 called: UDTwstring.Cast() Byref As Wstring Free 1.06.0
Code: Select all
Type UDTwstring Extends Object
Declare Constructor (Byval length As Integer)
Declare Destructor ()
Declare Virtual Operator Cast () Byref As Wstring
Dim As Wstring Ptr pw
End Type
Constructor UDTwstring (Byval length As Integer)
This.pw = Callocate(length+1, Sizeof(Wstring))
End Constructor
Destructor UDTwstring ()
Deallocate(This.pw)
End Destructor
Virtual Operator UDTwstring.Cast () Byref As Wstring
Print "called: UDTwstring.Cast() Byref As Wstring"
Return *This.pw
End Operator
Dim As UDTWstring u = UDTwstring(16)
*u.pw = "Free 1.06.0"
Print *u.pw
'Mid(Cast(Wstring, u), 5, 6) = "Basic" '' Cast(Wstring, u) not allowed
Mid(u, 5, 6) = "Basic"
Print *u.pw
Sleep
Code: Select all
Type UDTwstring Extends Object
Declare Constructor (Byval length As Integer)
Declare Destructor ()
Declare Virtual Operator Cast () Byref As Wstring
Dim As Wstring Ptr pw
End Type
Constructor UDTwstring (Byval length As Integer)
This.pw = Callocate(length+1, Sizeof(Wstring))
End Constructor
Destructor UDTwstring ()
Deallocate(This.pw)
End Destructor
Operator UDTwstring.Cast () Byref As Wstring
Print "called: UDTwstring.Cast() Byref As Wstring"
Return *This.pw
End Operator
Dim As UDTWstring u = UDTwstring(16)
*u.pw = "Free 1.06.0"
Print *u.pw
'Mid(Cast(Wstring, u), 5, 6) = "Basic" '' Cast(Wstring, u) not allowed
Dim As Function(Byref As UDTwstring) Byref As Wstring cast_operator = Cast(Any Ptr Ptr Ptr, @u)[0][0]
Mid(cast_operator(u), 5, 6) = "Basic"
Print *u.pw
Sleep
Code: Select all
Free 1.06.0
called: UDTwstring.Cast() Byref As Wstring
FreeBasic 1.06.0
Code: Select all
open cons as #1
print #1, "hello"
close #1
'(eof)
I'm still exploring screenres flags and there is some conflict with OPEN CONS.fxm wrote: KeyPgOpenCons → fxm [Added a warning about the real behavior presently different from that described]
Code: Select all
#include "fbgfx.bi"
using fb
screenres 640, 400, 8, 1, GFX_OPENGL 'or GFX_FULLSCREEN
open cons for output as #1
print #1, "hello"
close #1
sleep
Code: Select all
print #1,, "hello" 'add comma
Finally it seems to work , it sends the data to the console. But the opengl windows is hanging, so I guess this is due to sleep. What is the opengl command to replace sleep then?sancho2 wrote:It should be (shouldn't it?):Code: Select all
print #1,, "hello" 'add comma