Possible approaches/patterns to write a generic function/type

New to FreeBASIC? Post your questions here.
Post Reply
PeterHu
Posts: 152
Joined: Jul 24, 2022 4:57

Possible approaches/patterns to write a generic function/type

Post by PeterHu »

Greetings!

Just learning purpose to get an idea~

What are possible approaches(macros,and any other ways...) to write a generic function/Type in current FB?

Thanks in advance for the help.

Code: Select all

'pseudo code

function bigger(a as T,b as T) as T
	return iif(a>b,a,b)
end function

Type Simple
	public:
	declare constructor(byval idValue as integer)
	declare property ID() as integer
	declare property ID( byval idValue as integer)
	declare function toString() as string
	private:
	m_id as integer
End Type

Constructor Simple(byval idValue as integer)
	m_id=idValue
End Constructor

Property Simple.ID()as integer
	return m_id
End property

Property SImple.ID( byval idValue as integer)
	m_id=idValue
End Property

Operator >(byref lhs as Simple ,byref rhs as Simple) as boolean
	return (lhs.ID>rhs.ID)=true
End Operator

Fuction Simple.toString() as string
	return str(m_id)
End Function

'test code
	dim as integer first=1,second=2
	print bigger(first,second)
	
	dim as string str1="str1",str2="str2
	print bigger(str1,str2)
	
	
	dim as Simple simple1=Simple(100),simple2=Simple(200)
	print bigger(simple1,simple2).toString()
	
	sleep
	end
Last edited by PeterHu on Apr 10, 2023 1:32, edited 1 time in total.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Possible approaches/patterns to write a generic function/type

Post by dodicat »

Generics would be fantastic, but in the meantime only macros' are available.

Code: Select all



#macro set(o,datatype)
function bigger overload(a o as datatype,b o as datatype) as datatype
    #if #o <> "()" 
	return iif(a>b,a,b)
    #else
    return iif(ubound(a)>ubound(b),ubound(a),ubound(b))
    #endif
end function
#endmacro

set(,integer)
set(,string)
set((),double)

	dim as integer first=1,second=2
	print bigger(first,second)
	
	dim as string str1="str1",str2="str2"
	print bigger(str1,str2)
	
	
	dim as double simple1(100),simple2(200)
	print bigger(simple1(),simple2())
    
    sleep
     
adeyblue
Posts: 300
Joined: Nov 07, 2019 20:08

Re: Possible approaches/patterns to write a generic function/type

Post by adeyblue »

The other approach is to use Any Ptr and then take functions/interfaces to do all the type specific operations. Like qsort does. It can sort any type of data providing you tell it how big one piece of data is, how many you want to sort, and give it a function that can compare two pieces of data.

Code: Select all

#include "crt/stdlib.bi"

Function CompLongs cdecl (ByVal p1 as Const Any Ptr, ByVal p2 as Const Any Ptr) As Long
    dim as Long data1 = *cast(Long Ptr, p1)
    dim as Long data2 = *cast(Long Ptr, p2)
    Return data1 - data2
End Function

Function CompStrings cdecl (ByVal p1 as Const Any Ptr, ByVal p2 as Const Any Ptr) As Long
    dim ByRef as String s1 = *cast(String Ptr, p1)
    dim ByRef as String s2 = *cast(String Ptr, p2)
    If(s1 < s2) Then Return -1
    If(s1 > s2) Then Return 1
    Return 0
End Function

Randomize

dim as Long numbers(0 to 4)
dim as String texts(0 to 4)
dim i As Long

For i = 0 to 4
    dim as Long num = Rnd * 100
    texts(i) = Str(num)
    Print "Adding " & texts(i)
    numbers(i) = num
Next

qsort(@numbers(0), 5, SizeOf(Long), @CompLongs)
qsort(@texts(0), 5, SizeOf(String), @CompStrings)

Print "Sorted results:"
For i = 0 to 4
    Print numbers(i), texts(i)
Next
There are tradeoffs for each approach. Macro based things will be type safe (with dodicat's code above, the compiler would complain if you tried to compare an Integer and a String) but since you have to define them for each type used (this bit of dodicats code
set(,integer)
set(,string)
set((),double)
they're harder to pickup and use. In the case of macro 'generic' types too, you have to ensure that happens only once per file since you can't have two types with the same name.

Any ptr based stuff is easier to pickup and use (none of that required defining stuff) but also easier to use wrongly -Switch the sizeof(Long) & Sizeof(String) in the qsorts in my example and it will compile fine, but probably crash the program when run.
PeterHu
Posts: 152
Joined: Jul 24, 2022 4:57

Re: Possible approaches/patterns to write a generic function/type

Post by PeterHu »

Thank you both for the guideline!I think both approaches are great and beautiful!【C|FB can always solve problems by making use of very simple tools it has.】

So I found this framework from github

Code: Select all

https://github.com/denise-amiga/fb-framework
Its template implementation is kind of both the two.

A lot of useful libs,a lot to learn~
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Possible approaches/patterns to write a generic function/type

Post by paul doe »

PeterHu wrote: Apr 10, 2023 23:41 ...
So I found this framework from github

Code: Select all

https://github.com/denise-amiga/fb-framework
Its template implementation is kind of both the two.
...
That's an old fork of my framework, interestingly. It's probably outdated and doesn't have the latest bug fixes. The framework isn't available anymore (for now), but if all you need are the templatable collections, they can be found here:

https://github.com/glasyalabolas/fb-collections

They come with examples, but you can ask in the thread linked to in the description for further assistance if you need it.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: Possible approaches/patterns to write a generic function/type

Post by dafhi »

did u mean a different thread

cool
Last edited by dafhi on Apr 11, 2023 8:59, edited 1 time in total.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Possible approaches/patterns to write a generic function/type

Post by paul doe »

Uff, sorry. A different link got pasted. Corrected now :oops:
PeterHu
Posts: 152
Joined: Jul 24, 2022 4:57

Re: Possible approaches/patterns to write a generic function/type

Post by PeterHu »

paul doe wrote: Apr 11, 2023 2:48
PeterHu wrote: Apr 10, 2023 23:41 ...
So I found this framework from github

Code: Select all

https://github.com/denise-amiga/fb-framework
Its template implementation is kind of both the two.
...
That's an old fork of my framework, interestingly. It's probably outdated and doesn't have the latest bug fixes. The framework isn't available anymore (for now), but if all you need are the templatable collections, they can be found here:

https://github.com/glasyalabolas/fb-collections

They come with examples, but you can ask in the thread linked to in the description for further assistance if you need it.
fb-collections is part of the framework,am I right?The whole framework is great which helps me a lot.

All examples in either fb-collections or fb-framework compile and run perfect with fb1.09 both 32bit and 64bit under Win10.That's amazing!

Really hope the framework is under maintenance and,evern better,more features like basic utils(eg. utf16 string issues asian people met
under windows),unittest,database,sounds,compression,encrpytion,net/web etc..... could be added into the framework benefitting by its integration.Sorry,I wanted too much.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Possible approaches/patterns to write a generic function/type

Post by paul doe »

PeterHu wrote: Apr 11, 2023 5:11 fb-collections is part of the framework,am I right?The whole framework is great which helps me a lot.
...
The collections are standalone, for the most part. The framework was built using them (such as the event or the threading frameworks).
...
All examples in either fb-collections or fb-framework compile and run perfect with fb1.09 both 32bit and 64bit under Win10.That's amazing!
...
Thanks. It will run in any FB version from 1.08 onwards.
...
Really hope the framework is under maintenance and,evern better,more features like basic utils(eg. utf16 string issues asian people met
under windows),unittest,database,sounds,compression,encrpytion,net/web etc..... could be added into the framework benefitting by its integration.Sorry,I wanted too much.
I removed the public version a while back because I was reworking it into a more useable form. For example, I already have a working implementation of shared_ptrs in FB, which will make the collections and several parts of the framework much easier to work with, but I haven't updated the code yet. I originally designed it to be able to port certain VB projects more easily to FB.

Thank you for the interest, mate, really. But it is highly unlikely that I will continue developing this. Bear that in mind if you want to use it on big projects, ok?
PeterHu
Posts: 152
Joined: Jul 24, 2022 4:57

Re: Possible approaches/patterns to write a generic function/type

Post by PeterHu »

paul doe wrote: Apr 11, 2023 13:41
PeterHu wrote: Apr 11, 2023 5:11 fb-collections is part of the framework,am I right?The whole framework is great which helps me a lot.
...
The collections are standalone, for the most part. The framework was built using them (such as the event or the threading frameworks).
...
All examples in either fb-collections or fb-framework compile and run perfect with fb1.09 both 32bit and 64bit under Win10.That's amazing!
...
Thanks. It will run in any FB version from 1.08 onwards.
...
Really hope the framework is under maintenance and,evern better,more features like basic utils(eg. utf16 string issues asian people met
under windows),unittest,database,sounds,compression,encrpytion,net/web etc..... could be added into the framework benefitting by its integration.Sorry,I wanted too much.
I removed the public version a while back because I was reworking it into a more useable form. For example, I already have a working implementation of shared_ptrs in FB, which will make the collections and several parts of the framework much easier to work with, but I haven't updated the code yet. I originally designed it to be able to port certain VB projects more easily to FB.

Thank you for the interest, mate, really. But it is highly unlikely that I will continue developing this. Bear that in mind if you want to use it on big projects, ok?
Well noted with many thanks.
Post Reply