It is currently Thu May 23, 2013 7:25 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Allow to change a structure of a pointer
PostPosted: Fri Jan 20, 2012 2:27 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1441
Location: Germany
Hi,

it would be nice if something like this is possible:
Code:
If Type = 1
  *ptr.Type1Str = *Buffer
else
  *ptr.Type2Str = *Buffer
endif
Bernd


Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Fri Jan 20, 2012 4:22 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 4715
Location: Berlin - Germany
This is possible :wink:

//edit
not to create a structure at runtime, but to use different structures

_________________
PureBasic 5.11 | Windows 7 SP1 (x64) | Linux Mint 14 (x64) | RealSource

The use of EnableExplicit is free of charge and avoids errors.


Last edited by ts-soft on Fri Jan 20, 2012 5:32 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Fri Jan 20, 2012 4:29 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 8876
Location: France
Well, if you can change the structure at runtime, how will you access the fields if the structure have different fiels ? It needs to known at compile time, as PB isn't an interpreted language.


Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Fri Jan 20, 2012 4:42 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 711
Location: Germany, Glienicke
use structure union:
Code:
Structure BothTypes
   StructureUnion
      Type1.Type1Str
      Type2.Type2Str
   EndStructureUnion
EndStructure

*ptr.BothTypes = *Buffer
If Type = 1
  *ptr\Type1\...
Else
  *ptr\Type2\...
EndIf

_________________
Image


Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Fri Jan 20, 2012 7:05 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1441
Location: Germany
@Stargate

That's clear, but not what I need.
In your case I have to use the whole time a different name to access the structure. :cry:

At the moment I solve it this way:
Code:
If ChannelRecordSize = 100
  ReadChannel.Prototype_ReadChannel = @ReadChannel100()
ElseIf ChannelRecordSize = 476
  ReadChannel.Prototype_ReadChannel = @ReadChannel476()
EndIf
Than I can call the 'same' procedure: ReadChannel(*Buffer + Ptr) for the rest of the program :wink:

But I need 2 procedures which are completely identical up to the structure which I use for the pointer:
Code:
Procedure ReadChannel100(*Channel.Channel100Str)
Code:
Procedure ReadChannel476(*Channel.Channel476Str)
Bernd


Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Fri Jan 20, 2012 9:26 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Jun 28, 2003 12:01 am
Posts: 349
Hi,

I do not see any way to do this in PureBasic, if both structures are completely different.

If both structures are extended from a base structure and the procedure only uses the base elements, you could do the following:

Code:
Structure ChannelBase
   ; base elements
EndStructure

Structure Channel100Str Extends ChannelBase
   ; 100Str specific elements
EndStructure

Structure Channel476Str Extends ChannelBase
   ; 476Str specific elements
EndStructure

Procedure ReadChannel( *Channel.ChannelBase )
   ; ....
EndProcedure

; ....

ReadChannel( *ptr100Str )

; ....

ReadChannel( *ptr476Str )

; ....



After all there maybe is a way ... hmmm ....
You could create a standardized structure and COPY all elements from the original structure to the standardized one, which is handled by the Procedure ReadChannel(*Channel.ChannelStandardized).

cu,
guido

_________________
Windows 8 / Windows 7 / Windows XP (als VM)
PB Last Final / Last Beta Testing


Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Sat Jan 21, 2012 8:25 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Mar 09, 2006 9:24 am
Posts: 195
Location: S. Kor
I think anonymous structure union will be more useful.

Something like this.
Code:
Structure BothTypes
  StructureUnion
    AnonymousStructure Type1Str
    AnonymousStructure Type2Str
  EndStructureUnion
EndStructure

*ptr.BothTypes = *Buffer


or
Code:
Structure BothTypes
  StructureUnion
    AnonymousStructure   ;Type1Str
      ......
    EndAnonymousStructure
    AnonymousStructure   ;Type2Str
      ......
    EndAnonymousStructure
  EndStructureUnion
EndStructure


Edit:
Never mind.
This way has a problem.


Last edited by breeze4me on Sat Jan 21, 2012 4:58 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Sat Jan 21, 2012 9:14 am 
Offline
Always Here
Always Here
User avatar

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7304
Location: Norway
infratec wrote:
But I need 2 procedures which are completely identical up to the structure which I use for the pointer:
Code:
Procedure ReadChannel100(*Channel.Channel100Str)
Code:
Procedure ReadChannel476(*Channel.Channel476Str)
Bernd
If what you say is true (the procedures are actually, truly, identical) then, by definition, one structure must be a subset of the other, else field lookup wouldn't work. In this case, just pass in the address of the variable + the offset in the structure where the subset starts, and it will work regardless of declared structure.

It would help if you showed us the structures.

If you want runtime duck typing then make your structure contain a single map and shove all your members in there.

_________________
Woa, I set up a web server.


Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Sat Jan 21, 2012 4:16 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1441
Location: Germany
Hi Trond,

Here is what you want:
Code:
Structure Channel100Str
  Name.s{32}
  Dummy0.a[12]
  Transponder.a
  Dummy1.a[15]
  ServiceID.u
EndStructure

Structure Channel476Str
  Name.s{32}
  Dummy0.a[12]
  Transponder.a
  Dummy1.a[391]
  ServiceID.u
EndStructure

Prototype Prototype_ReadChannel(*Channel)
Global ReadChannel.Prototype_ReadChannel

Procedure ReadChannel100(*Channel.Channel100Str)
  Channel$ = *Channel\Name + #LF$
  Channel$ + Str(*Channel\Transponder) + #LF$
  Channel$ + Str(*Channel\ServiceID)
  AddGadgetItem(0, -1, Channel$)
EndProcedure


Procedure ReadChannel476(*Channel.Channel476Str)
  Channel$ = *Channel\Name + #LF$
  Channel$ + Str(*Channel\Transponder) + #LF$
  Channel$ + Str(*Channel\ServiceID)
  AddGadgetItem(0, -1, Channel$)
EndProcedure
Depending on the 'version' of the file which I process, I use one or the other:
Code:
:
:
:
If ChannelRecordSize = 100
  ReadChannel.Prototype_ReadChannel = @ReadChannel100()
ElseIf ChannelRecordSize = 476
  ReadChannel.Prototype_ReadChannel = @ReadChannel476()
EndIf

ClearGadgetItems(0)
For i = 1 To Channels
  ReadChannel(*Buffer + Ptr)
  Ptr + ChannelRecordSize
Next i
:
:
:
My solution works, but it's 'ugly'.

Bernd


Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Sat Jan 21, 2012 5:47 pm 
Offline
Always Here
Always Here
User avatar

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7304
Location: Norway
If it's coming from somewhere, the format is fixed and you can't do much about it. :(

But If you can swap the two last fields in both structures, or make both structures extend from one, with the field Dummy1 as the only field in each used structure, this can be solved elegantly:
Code:
Structure ChannelStr
  Name.s{32}
  Dummy0.a[12]
  Transponder.a
  ServiceID.u
  Dummy1_.a[0]
EndStructure

Structure Channel100Str Extends ChannelStr
  Dummy1.a[15]
EndStructure

Structure Channel476Str Extends ChannelStr
  Dummy1.a[391]
EndStructure

Procedure ReadChannel(*Channel.ChannelStr)
  Channel$ = *Channel\Name + #LF$
  Channel$ + Str(*Channel\Transponder) + #LF$
  Channel$ + Str(*Channel\ServiceID)
  AddGadgetItem(0, -1, Channel$)
EndProcedure

ClearGadgetItems(0)
For i = 1 To Channels
  ReadChannel(*Buffer + Ptr)
  Ptr + ChannelRecordSize
Next i

_________________
Woa, I set up a web server.


Top
 Profile  
 
 Post subject: Re: Allow to change a structure of a pointer
PostPosted: Sat Jan 21, 2012 6:25 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
Trond wrote:
Code:
Structure ChannelStr
  Name.s{32}
  Dummy0.a[12]
  Transponder.a
  ServiceID.u
  ;
  Dummy1_.a[0]    ;<-- If you omit this, I assume the code will fail?
  ;
EndStructure

Structure Channel100Str Extends ChannelStr
  Dummy1.a[15]
EndStructure

Structure Channel476Str Extends ChannelStr
  Dummy1.a[391]
EndStructure

Nice. 8) I noticed this behavior a while back and thought it was a mistake.
Even though you don't reference the extended structure, the fields are there for the taking.

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye