22 March 2015, 16:11 | #1 |
Registered User
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
|
Blitz2: Copy newtypes?
It's my understanding that one cannot simply assign one newtype to another, as you would with primitive variables. In other words, x=y would work if x and y are bytes, quicks, strings or words, but not if x and y are newtypes.
How would you go about copying one newtype to another, or am I mistaken? |
22 March 2015, 20:50 | #2 |
Registered User
Join Date: Oct 2014
Location: Europe
Posts: 470
|
Code:
newtype.foo a.w b.w c.w end newtype deftype.foo tsrc, tdest copymem_ &tsrc.foo,&tdest.foo,SizeOf.foo Beware of strings! a d.s inside a newtype is just a pointer, so the address (and length) of the string is provided, not the string itself! Instead, you could use a char array , e.g. d.b[32]. Make it a nice Statement with proper address checks and so on. |
23 March 2015, 02:07 | #3 |
Registered User
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
|
Thanks, I solved it using some inline assembler, but it's nice to see that there is a system call for doing it as well. And as usual, Blitz's online help is of no help – according to it, CopyMem_ takes arguments in parentheses. Your example is correct – no parens.
I was hoping there was a simpler way than actually copying memory, though. |
26 March 2015, 22:04 | #4 | |
Registered User
Join Date: Oct 2014
Location: Europe
Posts: 470
|
Quote:
The solution depends on what you want: Do you really want to COPY the type? If not, you could (but must not!) try to bend a pointer temporarily to the src address: Code:
Newtype.foo a.w b.w c.w End Newtype Deftype.foo tsrc tsrc\a=10,20,30 ;put in some values *tdst.foo = tsrc ;"clone" the struct by taking over the address if *tdst NPrint *tdst ;should be = &tsrc NPrint *tdst\a NPrint *tdst\b NPrint *tdst\c ; *tdst=0 ;make sure we are not trying to free that later (GURU) endif MouseWait End |
|
27 March 2015, 00:11 | #5 | ||
Registered User
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
|
Quote:
Yes, in fact, I need to copy a whole list of that type. Quote:
Code:
*tdst.foo = &tsrc |
||
28 March 2015, 00:37 | #6 |
Registered User
Join Date: Oct 2014
Location: Europe
Posts: 470
|
|
28 March 2015, 16:04 | #7 |
Registered User
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
|
Hence why I've never come to grips with pointers in Blitz; they seem so ad-hoc.
On a related topic, is it possible to treat a pointer to a list as a list? I have a list consisting of a newtype containing some primitive variables as well as a pointer to an item in a list consisting of another newtype. In other words: Newtype daughter x.w, y.w, z.q End Newtype Newtype mother a.w, b.w *pntr.daughter End Newtype Dim List daughters.daughter(20) AddItem daughters() daughters()\x=3,4,9.6 Dim List mothers.mother(10) AddItem mothers() mothers()\a=1 mothers()\b=2 mothers()\*pntr=&daughters() This works as far as mothers()\*pntr\y being 4, but is it possible to do a NextItem(mothers\*pntr) ? |
28 March 2015, 23:27 | #8 | |
Registered User
Join Date: Oct 2014
Location: Europe
Posts: 470
|
Quote:
I wouldn't do those kind of things. First, why using a pointer when you could use a .l as a solid address storage as well? Why do you use a List of .daughters, instead of Code:
Newtype mother a.w, b.w numchilds.b daughters.daughter[#maxchilds] End Newtype Anyway, you can still use execs linked list way, meaning adding nodes and so on, treating nodes as memory blocks of your own (newtype structs), but why bother? Find a simple way - this is my best hint. |
|
31 March 2015, 17:00 | #9 | |||
Registered User
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
|
Quote:
Quote:
Quote:
My hacky solution is to have a global list of daughters, with each mother containing a pointer to its first daughter and the number of daughter in that list. It works as long as all related daughters come in sequence, which they do. A more safe approach would be to have a unique ID for each mother, and to store that for each daughter in the list. |
|||
15 July 2015, 16:46 | #10 | ||
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
|
I know this is an old-ish thread, but just thought I'd add a couple of points for completeness...
Quote:
Quote:
|
||
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Blitz2: Hires laced | BippyM | Coders. Language | 25 | 06 February 2020 01:07 |
Unknown Copy-Dongle [SOLVED: Siegfried-Copy 1.9SE] | TheZock | support.Hardware | 4 | 26 November 2013 00:23 |
Blitz2 Newcommandset | BippyM | Coders. Language | 0 | 21 July 2012 23:59 |
Requester Bug when copying IPF to Standard ADF with X-Copy/Power Copy. | BarryB | support.WinUAE | 9 | 17 January 2012 20:20 |
Blitz2 Manual | AlfaRomeo | Amiga scene | 18 | 01 May 2009 10:53 |
|
|