* refactored (un)marshalling of DeltaCommand into cleaner code using sigc

git-svn-id: svn://localhost/ardour2/branches/3.0@3245 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Hans Baier 2008-04-10 07:30:10 +00:00
parent 1a0044b35d
commit 9aa8af5a28
2 changed files with 22 additions and 30 deletions

View file

@ -124,20 +124,16 @@ public:
void remove(const boost::shared_ptr<Note> note);
private:
class NoteMarshaller {
public:
XMLNode *operator()(const boost::shared_ptr<Note> note);
};
XMLNode &marshal_note(const boost::shared_ptr<Note> note);
boost::shared_ptr<Note> unmarshal_note(XMLNode *xml_note);
class NoteUnmarshaller {
public:
boost::shared_ptr<Note> operator()(XMLNode *xml_note);
};
MidiModel& _model;
const std::string _name;
std::list< boost::shared_ptr<Note> > _added_notes;
std::list< boost::shared_ptr<Note> > _removed_notes;
typedef std::list< boost::shared_ptr<Note> > NoteList;
NoteList _added_notes;
NoteList _removed_notes;
};
MidiModel::DeltaCommand* new_delta_command(const std::string name="midi edit");

View file

@ -658,8 +658,8 @@ MidiModel::DeltaCommand::undo()
_model.ContentsChanged(); /* EMIT SIGNAL */
}
XMLNode *
MidiModel::DeltaCommand::NoteMarshaller::operator()(const boost::shared_ptr<Note> note)
XMLNode &
MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr<Note> note)
{
XMLNode *xml_note = new XMLNode("note");
ostringstream note_str(ios::ate);
@ -682,11 +682,11 @@ MidiModel::DeltaCommand::NoteMarshaller::operator()(const boost::shared_ptr<Note
velocity_str << (unsigned int) note->velocity();
xml_note->add_property("velocity", velocity_str.str());
return xml_note;
return *xml_note;
}
boost::shared_ptr<Note>
MidiModel::DeltaCommand::NoteUnmarshaller::operator()(XMLNode *xml_note)
MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
{
unsigned int note;
istringstream note_str(xml_note->property("note")->value());
@ -722,9 +722,6 @@ MidiModel::DeltaCommand::NoteUnmarshaller::operator()(XMLNode *xml_note)
int
MidiModel::DeltaCommand::set_state (const XMLNode& delta_command)
{
cerr << "Unmarshalling Deltacommand" << endl;
if(delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
return 1;
}
@ -733,13 +730,13 @@ MidiModel::DeltaCommand::set_state (const XMLNode& delta_command)
XMLNode *added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
XMLNodeList notes = added_notes->children();
transform(notes.begin(), notes.end(), back_inserter(_added_notes),
MidiModel::DeltaCommand::NoteUnmarshaller());
sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
_removed_notes.clear();
XMLNode *removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
notes = removed_notes->children();
transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
MidiModel::DeltaCommand::NoteUnmarshaller());
sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
return 0;
}
@ -749,19 +746,18 @@ MidiModel::DeltaCommand::get_state ()
{
XMLNode *delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
delta_command->add_property("midi_source", _model.midi_source().id().to_s());
delta_command->add_property("midi_source_name", _model.midi_source().name());
XMLNode *added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
for (std::list< boost::shared_ptr<Note> >::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
NoteMarshaller marshaller;
added_notes->add_child_nocopy(*marshaller(*i));
}
XMLNode *removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
for (std::list< boost::shared_ptr<Note> >::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
NoteMarshaller marshaller;
removed_notes->add_child_nocopy(*marshaller(*i));
}
for_each(_added_notes.begin(), _added_notes.end(),
sigc::compose(sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
XMLNode *removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
for_each(_removed_notes.begin(), _removed_notes.end(),
sigc::compose(sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
return *delta_command;
}