Best way to save and load classes (with sequencer tracks)?


I started programming a drum sequencer and am looking for advice.
What would be the cleanest way to save and load tracks.
I thought it would be possible to serialize the array of DrumTrack classes to a file, and load them later, but i see no such support in juce. Am I thinking in the wrong direction, or do i need an additional library ?

Who can advise me on a neat way to save and reload such a class ?

// The program is not yet usable, but here is a snippet of the class so far :
// I will post the link to my site on the forum as soon as it is up
(first trying to make it work)

class DrumTrack { public: String Name; int Length; int NoteNr; int Volume; UIButtons* TrackButtons; //for the moment i use button states for storing notes -- should have an alternative ? DrumTrack::DrumTrack(String Name,int Length,int NoteNr); String ExportTrack(void); };

Juce is so great !!! Thanks jules !

I’m not getting you. If you’ve serialized the data in some meaningful way, JUCE has plenty of tools for streaming the data to a file.

Assuming your data is serialized to some form of binary encoding, and not text, the easiest is probably just to use FileOutputStream.

hmm not sure if i understand you,
I will explain my point though:

I thought there could be an automatic way to save and later reload class instances. Some kind of persistence possibility, which i can inherit to my classes. I guess not

Would you suggest saving a binary dump of all instances,
and reloading them on import ? I thought this would not be feasible.

Anyway, i am now constructing xml elements for each track,
but it requires some coding for each property. I hoped some of you could tell me a better way

there is no better way, i was writing a sequencer myself. there is no way to dump complex classes to binary form, no magic serialize method is available, since classes can be very complex. i had to read this: to wrap my head around c++ and seiralization, after some time i went back XML and we are happy together till this day.

my suggestion is add a serialize and restore method for any object you write, no matter how simple, just return a XmlElement for each note for example, a track is built of notes, so a track can serialize itself by calling serialize() on all it’s notes and perhaps something of it’s own and so on.

this is how i did it.

thanks atom !

You know, i hate copy and paste,
but in fact I have implemented it quite like that, with xml elements, and yes it works okay.

I am always in doubt isnt there a simpler way to do it ?
Thanks for the confirmation.
It’s not too bad to do it like that.

There can’t be an easier way to do since at least some of the data your classes contain will be pointers to other objects, and this kind of thing can’t really be serialized automatically.

Your three main options are:

  1. as atom said, use XML.
  2. have a method that serializes your data to binary. Essentially the same as for XML but a little smaller in file size, and a little faster.
  3. use a lightweight database engine such as SQLite. In this case, you’ll essentially be storing your state all the time. The advantages are that if your app falls over, the user’s work wont be lost, since it’ll be getting written to file in real time; you’ll be getting all of the SQL goodness for free. The downside is quite obviously performance.

You could always try boost serialization ( if you’re not deterred by massive templatization…

Google had an interesting project for serialising object, using their own markup language and mini-compiler… But basically, there’s no easy way!

I am using the xml method and it works fine for my application.
Thanks for all the advice !!!