Returns a view of this structure's memory as an array of structures. Note that this Structure must have a public, no-arg constructor. If the structure is currently using auto-allocated Memory backing, the memory will be resized to fit the entire array. @param array Structure[]
(Structure[] array)
| 1709 | * @return array of Structure mapped onto the available memory |
| 1710 | */ |
| 1711 | public Structure[] toArray(Structure[] array) { |
| 1712 | ensureAllocated(); |
| 1713 | if (this.memory instanceof AutoAllocated) { |
| 1714 | // reallocate if necessary |
| 1715 | Memory m = (Memory)this.memory; |
| 1716 | int requiredSize = array.length * size(); |
| 1717 | if (m.size() < requiredSize) { |
| 1718 | useMemory(autoAllocate(requiredSize)); |
| 1719 | } |
| 1720 | } |
| 1721 | // TODO: optimize - check whether array already exists |
| 1722 | array[0] = this; |
| 1723 | int size = size(); |
| 1724 | for (int i=1;i < array.length;i++) { |
| 1725 | array[i] = newInstance(getClass(), memory.share(i*size, size)); |
| 1726 | array[i].conditionalAutoRead(); |
| 1727 | } |
| 1728 | |
| 1729 | if (!(this instanceof ByValue)) { |
| 1730 | // keep track for later auto-read/writes |
| 1731 | this.array = array; |
| 1732 | } |
| 1733 | |
| 1734 | return array; |
| 1735 | } |
| 1736 | |
| 1737 | /** Returns a view of this structure's memory as an array of structures. |
| 1738 | * Note that this <code>Structure</code> must have a public, no-arg |