mdTypes

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

mdTypes

Post by MOD »

The goal of this project is to create a class structure, that is almost similar to Java.

Noteworthy are the collection classes. These allow you to add almost all FreeBASIC datatypes (except Integer - use Long or mdInteger instead) and own types into generic lists and maps.

Example code for a mdList with own class:

Code: Select all

#Include Once "md/util/mdList.bi"

Type MyClass
    Public:
    Declare Constructor ()
    Declare Constructor (ByVal value As Integer)
    Declare Operator Cast() As String

    Declare Sub setMyVar (ByVal value As Integer)
    Declare Function getMyVar () As Integer

    Private:
    As Integer myVar
End Type
'must have a default constructor or no explicit constructor at all!
Constructor MyClass ()

End Constructor
Constructor MyClass (ByVal value As Integer)
    This.setMyVar(value)
End Constructor
'must implement cast to string for string representation
Operator MyClass.Cast() As String
    Return Str(This.myVar)
End Operator

Sub MyClass.setMyVar (ByVal value As Integer)
    This.myVar = value
End Sub
Function MyClass.getMyVar () As Integer
    Return This.myVar
End Function

'operator for comparision must exist!
Operator = (ByRef lhs As MyClass, ByRef rhs As MyClass) As Integer
    Return lhs.getMyVar() = rhs.getMyVar()
End Operator

mdListDeclare(MyClass)
Dim As mdList(MyClass) list

Dim As MyClass foo
foo.setMyVar(0) : list.add(foo)
foo.setMyVar(1) : list.add(foo)
foo.setMyVar(2) : list.add(foo)
foo.setMyVar(3) : list.add(foo)
foo.setMyVar(0) : list.add(foo)

Print "Output 1:"
For i As Integer = 0 To list.size() - 1
    Print list.get(i).getMyVar()
Next
Print

Print "Output 2:"
Dim As MyClass element
ForEach(MyClass, element In list)
    Print element.getMyVar()
NextEach
Print

Print "Output 3:"
Dim As mdIterator(MyClass) iterator = list.iterator()
While iterator.hasNext()
    Print iterator.nex().getMyVar()
Wend
Print

Sleep
mdTypes also has classes for hash codes, logging and database persistence.

>>DOWNLOAD<< (Mirror at FreeBASIC-Portal)
The download contains all types, a lot of examples and a readme with installation instructions. For tutorials and more information check the mirror page.
Last edited by MOD on Oct 22, 2015 18:47, edited 3 times in total.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: mdTypes

Post by fxm »

I get the same two compiler errors with the above example:
.....\FBIDETEMP.bas(34) error 24: Invalid data types in 'mdListDeclare(MyClass)'
.....\FBIDETEMP.bas(34) error 24: Invalid data types in 'mdListDeclare(MyClass)'
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: mdTypes

Post by MOD »

I noticed, that I didn't update the code of the first post. MyClass now have a Cast() to String operator. But even without this I would expect an other error than "Invalid data types".

Did you copy the md/ folder into your inc/ folder? And do you have the latest version from the download (currently just the mirror works)?
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: mdTypes

Post by fxm »

MOD wrote:Did you copy the md/ folder into your inc/ folder? And do you have the latest version from the download (currently just the mirror works)?
Yes (and same behavior if we copy 'md' in the current folder).
MOD wrote:Did you copy the md/ folder into your inc/ folder? And do you have the latest version from the download (currently just the mirror works)?
Yes (11/24/2013).
Last edited by fxm on Dec 14, 2013 10:54, edited 1 time in total.
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: mdTypes

Post by MOD »

Could you try the updated example from the first post again or the examples, that are included in the package?
It works out of the box here (fbc 0.90.1/0.91.0).

You could also try to compile with -pp and paste the code somewhere (e.g. pastebin), so I can check the output.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: mdTypes

Post by fxm »

MOD wrote:You could also try to compile with -pp.
Error on line:
ret &=Str(currentNode->nex->value)

If I add a 'Cast' operator as:

Code: Select all

 Operator MyClass.Cast () As String
   Return Str(myVar)
 End Operator
This works.
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: mdTypes

Post by MOD »

Yes, so it was my fault, because I didn't update the example on this thread. Sorry for that.

The package contains a lot of example codes how to use mdTypes. Every mdType has a link to the Java class/interface, which it represents, at the top of the file, so one can also use the Java documentation on a certain level of usage.

It is great to bypass the problem of dynamic arrays in UDTs and general (+generic) collection of variables and objects.
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: mdTypes

Post by MOD »

Now we have a tutorial: Using mdTypes
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: mdTypes

Post by anonymous1337 »

Great job on the tutorial. Plenty of examples and descriptions. I like that.
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: mdTypes

Post by MOD »

Thx. If somebody wants additional information, just ask. I'll extend the tutorial every now and then.
swoop
Posts: 29
Joined: Mar 09, 2012 9:12

Re: mdTypes

Post by swoop »

MOD wrote:Thx. If somebody wants additional information, just ask. I'll extend the tutorial every now and then.
I have a question about this statement in the example given in the mdTypes: DELETE list.get(i)

1) Does this just delete the pointer (set it to nullptr), but does not remove the element from the list? In other words, the count/size of the list stays the same or does the element get removed from the list. I still must use list.remove(i) to remove it from the list?

2) If I want to set the pointer to null or destroy the object it points to, how do I do that with and without removing and changing the size and indexing of the list?

3) What if in my code elsewhere, the pointer is already deallocated and set to null. What happens to the list element? Is it automatically removed from the list when the object to the pointer is released?

Code: Select all

'Imports
#INCLUDE ONCE "md/util/mdList.bi"

'Tell FB, that you want to use mdList with String variables - this will expand the intern macro
mdListDeclare(BYTE, PTR)

'Declare a list
DIM AS mdList(BYTE, PTR) list

'Create and add some data
DIM AS BYTE PTR a = NEW BYTE
DIM AS BYTE PTR b = NEW BYTE

*a = 5
*b = 7

list.add(a)
list.add(b)

'Iterate with simple for loop and print out data
FOR i AS INTEGER = 0 TO list.size() - 1
    PRINT *list.get(i),
    DELETE list.get(i)
NEXT
MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: mdTypes

Post by MOD »

The thing about the pointer feature of mdTypes' collections api is more flexibility. Usually you would use real objects, not object pointers.
Anyway, to answer your questions:

1. The "Delete" in this line is FreeBASICs Delete Statement. It does only delete the pointers data but doesn't set the pointer to null or change the list in any way. After this action you should not use the pointers data anymore, like with every other deleted pointer. As the example ends after this statement, I've added the line to free the memory, as it should be done by the programmer. If it wouldn't end here, one would need a line to set the pointer to NULL or remove the element from the list (but not while iterating through the list, use a temp list for that!).

2. Well, it's what happens in the example. The pointer ist destroyed but not set to NULL and the list stays unchanged.

3. The list could check if the pointer is NULL but cannot check if it was already destroyed anywhere else (simply not possible, we don't have any kind of garbage collector). However, what should I do when the pointer is NULL? As I said in my first sentence, this feature is about flexibility, so it's up to the programmer how to manipulate the list. If you want automatic freeing of data after leaving the scope, use objects not object pointers.
swoop
Posts: 29
Joined: Mar 09, 2012 9:12

Re: mdTypes

Post by swoop »

MOD wrote:The thing about the pointer feature of mdTypes' collections api is more flexibility. Usually you would use real objects, not object pointers.
I am getting many errors trying to compile code that uses the mdTypes, and I must be doing something very wrong.

The errors are like these:
tm_mysql.bas(40) error 41: Variable not declared, tmG_dbList
tm_mysql.bas(43) error 247: Symbol not a CLASS, ENUM, TYPE or UNION type, before '.'
tm_mysql.bas(43) error 3: Expected End-of-Line, found '('
tm_mysql.bas(46) error 41: Variable not declared, tmG_dbListCount
tm_mysql.bas(58) error 41: Variable not declared, tmG_resultList
tm_mysql.bas(61) error 247: Symbol not a CLASS, ENUM, TYPE or UNION type, before
A snippet of the code I'm trying to compile:

Code: Select all

'Imports
#Include Once "mysql\mysql.bi"
#Include Once "windows.bi"
#Include Once "md\util\mdList.bi"

'Tell FB, that you want to use mdList with MYSQL and MYSQL_RES variables - this will expand the intern macro
mdListDeclare(MYSQL, PTR)			' MySQL DB Pointer Type
mdListDeclare(MYSQL_RES, PTR)		' MySQL Results Pointer Type 

'Declare a global list
DIM AS mdList(MYSQL, PTR) tmG_dbList
DIM AS mdList(MYSQL_RES, PTR) tmG_resultList

function tm_addDBList (ByVal tp_db as MYSQL ptr) as integer

	dim as integer tv_dbID

	' Add a MYSQL DB pointer to list
	tmG_dbList.Add (tp_db)
	
	' Get the new current size of list - 1; that will be the reference ID returned
	tv_dbID = tmG_dbList.Size() - 1
	
	return (tv_dbID)

end function ' tm_addDBList


MOD
Posts: 557
Joined: Jun 11, 2009 20:15

Re: mdTypes

Post by MOD »

At least for your snippet the problem is, that you're declaring the lists in the main scope but using them in a sub. Try this:

Code: Select all

DIM SHARED AS mdList(MYSQL, PTR) tmG_dbList
DIM SHARED AS mdList(MYSQL_RES, PTR) tmG_resultList
swoop
Posts: 29
Joined: Mar 09, 2012 9:12

Re: mdTypes

Post by swoop »

Thank you again for answering my questions. It is really helpful.

I'd like to ask if I understand these codes correctly with the pointer example code:

Code: Select all

*list.get(i) = 1    ' Does this set the value to 1 contained at the pointer?
list.set(i) = 2      ' Does this set the value to 2 contained at the pointer?
*list.set(i) = 3      ' Does this set the value to 3 contained at the pointer?
list.get(i) = b      ' Does this refer the element to b pointer?
list.get(i) = NULL    ' Does this refer the element to a null pointer?
PRINT list.contains(NULL)    ' If NULL is contained in any list element, then this prints TRUE
PRINT list.contains(NOT NULL)   'If LIST does not contain all null pointers, then this prints TRUE
Pointer Example Code:

Code: Select all

'Imports
#INCLUDE ONCE "md/util/mdList.bi"

'Tell FB, that you want to use mdList with String variables - this will expand the intern macro
mdListDeclare(BYTE, PTR)

'Declare a list
DIM AS mdList(BYTE, PTR) list

'Create and add some data
DIM AS BYTE PTR a = NEW BYTE
DIM AS BYTE PTR b = NEW BYTE

*a = 5
*b = 7

list.add(a)
list.add(b)

'Iterate with simple for loop and print out data
FOR i AS INTEGER = 0 TO list.size() - 1
    PRINT *list.get(i),
    DELETE list.get(i)
NEXT

SLEEP
Post Reply