Convert C array to Juce Array


#1

this is only marginally a juce question, I’m aware that really the issue is that i don’t know c++ well at all.

I want to make a juce Array from a C array

i see the constructor definition here:

https://www.juce.com/doc/classArray#aaa426011ebe1d6a9d32302f92ead8b98

but I don’t understand how to use that to actually write the code.

I have a C array

`std::array<int, 50> padToNoteMap;`

and I want to wrap it up as a juce Array so I can save it in a value tree and then save that to a file.

I came up with:

    Array<int, DummyCriticalSection, 50> padNoteWrap (array<int, 50> padToNoteMap);

which i then want to add to a datatree llike this:

    dataTree.setProperty(String("padNoteWrap"), padNoteWrap, 0);

but although this doesn’t give an error it represents the array just as “1” in the resulting xml file

could someone give me an example just of the correct way to create a juce array from a C array?

also - where should I read how to parse the function descriptions in the documentation, can anyone recommend a resource?

all the C++ resources I have give actual code examples and i’m struggling with the formal type/function definitions in the juce api


#2

I think you are almost there. But I think you have to give the number of items to the constructor:

Array<int, DummyCriticalSection, 50> padNoteWrap (array<int, 50> padToNoteMap, 50);

And in C++, please don’t use 0 as nullptr:

dataTree.setProperty(String("padNoteWrap"), padNoteWrap, nullptr);

#3

NB you can skip all the critical section stuff:

Array<int> padNoteWrap (padToNoteMap, 50);

#4

How does wrapping it in a juce array help with saving it into a ValueTree?

You can just copy it straight into the ValueTree with a for loop…?


#5

thanks for all the replies!

daniel - good point about the nullptr, I do know that in principle, but I had copied from an example elsewhere on the forum, and although I had looked at the formal reference here - https://www.juce.com/doc/classValueTree#ad236114dc2a8c41c799f1fc51d1614bd -since I’m not adept with interpreting those I had missed the fact that that parameter is a pointer.

daniel and jules - both this:

Array<int> padNoteWrap (array<int, 50> padToNoteMap, 50);

and this version:

Array<int, DummyCriticalSection, 50> padNoteWrap (array<int, 50> padToNoteMap, 50);

cause Xcode to give me an error “expected parameter declarator”

however it accepts this:

Array<int> padNoteWrap (array<int, 50> padToNoteMap);

it even accepts this:

Array<int> padNoteWrap(&padToNoteMap);

but i’m not yet sure if either work correctly.

bazrush - yes I think you’ve nailed it there - I’m no longer sure that it does help. I was under the impression that a whole array could be added to a value tree if it was a juce array and then somehow wrapped as a var, but I can’t work out how to do it.

is that actually possible? or do you always have to write a loop do get an array into a valuetree?

i ended up doing this:

    ValueTree padNoteTree(String("padNoteTree"));
    dataTree.addChild(padNoteTree, -1, nullptr);
    for (int i = 0; i < 50; ++i)
    {
         padNoteTree.setProperty("i"+String(i), padToNoteMap[i], nullptr);
    }

which works fine, although it won’t allow me to dispense with "i"+String(i) and just use String(i) - complains that it isn’t valid xml…surely a string is a string?


#6

I had (mis)understood jule’s comment here:

to mean that you could store an array as a var in a valuetree in one step, but i now think i probably misunderstood.

jules could you clarify what you were actually suggesting there?


#7

this gives a “no matching constructor” error


#8

Ah, yes, you can sort of do that. It’s got a couple of issues you, it won’t write to XML properly:

Unless you really want it in an Array I’d be tempted to go with something like this:

for (auto i: originalArray)
{
  ValueTree node{"n"};
  n.setProperty("value", i, nullptr);
  tree.addChild(node, -1, nullptr);
}

or similar which then gives you the option of writing to XML still. There may be reasons why Array would be better … someone else may have a view.


#9

Another way to save an existing vector or array of = float/double/int …,to a Valuetree is:
ValueTree v;
T *x;
int size = #values
MemoryBlock b(x, sizeof(T) * size);
v.setProperty(propertyName,b, nullptr);

Then read it back with:
auto p= v.getPropertyPointer(propertyName);
if (p && p->getBinaryData())
memcpy(x, p->getBinaryData()->getData(), sizeof(T)*size);