Skip to content

Commit

Permalink
Test object init map. Add additional objects (e.g. list family).
Browse files Browse the repository at this point in the history
  • Loading branch information
mhroth committed Aug 12, 2011
1 parent 55a1cd6 commit 3ca91d3
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 40 deletions.
34 changes: 33 additions & 1 deletion src/MessageListAppend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,41 @@
*/

#include "MessageListAppend.h"
#include "MessageListLength.h"
#include "MessageListPrepend.h"
#include "MessageListSplit.h"
#include "MessageListTrim.h"

// MessageListAppend is the default factor for all list objects
MessageObject *MessageListAppend::newObject(PdMessage *initMessage, PdGraph *graph) {
return new MessageListAppend(initMessage, graph);
if (initMessage->isSymbol(0)) {
if (initMessage->isSymbol(0, "append") ||
initMessage->isSymbol(0, "prepend") ||
initMessage->isSymbol(0, "split")) {
int numElements = initMessage->getNumElements()-1;
PdMessage *message = PD_MESSAGE_ON_STACK(numElements);
message->initWithTimestampAndNumElements(0.0, numElements);
memcpy(message->getElement(0), initMessage->getElement(1), numElements*sizeof(MessageAtom));
MessageObject *messageObject = NULL;
if (initMessage->isSymbol(0, "append")) {
messageObject = new MessageListAppend(message, graph);
} else if (initMessage->isSymbol(0, "prepend")) {
messageObject = new MessageListPrepend(message, graph);
} else if (initMessage->isSymbol(0, "split")) {
messageObject = new MessageListSplit(message, graph);
}
return messageObject;
} else if (initMessage->isSymbol(0, "trim")) {
// trim and length do not act on the initMessage
return new MessageListTrim(initMessage, graph);
} else if (initMessage->isSymbol(0, "length")) {
return new MessageListLength(initMessage, graph);
} else {
return new MessageListAppend(initMessage, graph);
}
} else {
return new MessageListAppend(initMessage, graph);
}
}

MessageListAppend::MessageListAppend(PdMessage *initMessage, PdGraph *graph) : MessageObject(2, 1, graph) {
Expand Down
4 changes: 0 additions & 4 deletions src/MessageListLength.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@

#include "MessageListLength.h"

MessageObject *MessageListLength::newObject(PdMessage *initMessage, PdGraph *graph) {
return new MessageListLength(initMessage, graph);
}

MessageListLength::MessageListLength(PdMessage *initMessage, PdGraph *graph) : MessageObject(1, 1, graph) {
// nothing to do
}
Expand Down
1 change: 0 additions & 1 deletion src/MessageListLength.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
class MessageListLength : public MessageObject {

public:
static MessageObject *newObject(PdMessage *initMessage, PdGraph *graph);
MessageListLength(PdMessage *initMessage, PdGraph *graph);
~MessageListLength();

Expand Down
4 changes: 0 additions & 4 deletions src/MessageListPrepend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@

#include "MessageListPrepend.h"

MessageObject *MessageListPrepend::newObject(PdMessage *initMessage, PdGraph *graph) {
return new MessageListPrepend(initMessage, graph);
}

MessageListPrepend::MessageListPrepend(PdMessage *initMessage, PdGraph *graph) : MessageObject(2, 1, graph) {
prependMessage = initMessage->copyToHeap();
}
Expand Down
1 change: 0 additions & 1 deletion src/MessageListPrepend.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
class MessageListPrepend : public MessageObject {

public:
static MessageObject *newObject(PdMessage *initMessage, PdGraph *graph);
MessageListPrepend(PdMessage *initMessage, PdGraph *graph);
~MessageListPrepend();

Expand Down
4 changes: 0 additions & 4 deletions src/MessageListSplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
#include <string.h>
#include "MessageListSplit.h"

MessageObject *MessageListSplit::newObject(PdMessage *initMessage, PdGraph *graph) {
return new MessageListSplit(initMessage, graph);
}

MessageListSplit::MessageListSplit(PdMessage *initMessage, PdGraph *graph) : MessageObject(2, 3, graph) {
splitIndex = initMessage->isFloat(0) ? (int) initMessage->getFloat(0) : 0;
}
Expand Down
1 change: 0 additions & 1 deletion src/MessageListSplit.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
class MessageListSplit : public MessageObject {

public:
static MessageObject *newObject(PdMessage *initMessage, PdGraph *graph);
MessageListSplit(PdMessage *initMessage, PdGraph *graph);
~MessageListSplit();

Expand Down
4 changes: 0 additions & 4 deletions src/MessageListTrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@

#include "MessageListTrim.h"

MessageObject *MessageListTrim::newObject(PdMessage *initMessage, PdGraph *graph) {
return new MessageListTrim(initMessage, graph);
}

MessageListTrim::MessageListTrim(PdMessage *initMessage, PdGraph *graph) : MessageObject(1, 1, graph) {
// nothing to do
}
Expand Down
1 change: 0 additions & 1 deletion src/MessageListTrim.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
class MessageListTrim : public MessageObject {

public:
static MessageObject *newObject(PdMessage *initMessage, PdGraph *graph);
MessageListTrim(PdMessage *initMessage, PdGraph *graph);
~MessageListTrim();

Expand Down
36 changes: 20 additions & 16 deletions src/PdContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ PdContext::PdContext(int numInputChannels, int numOutputChannels, int blockSize,
blockStartTimestamp = 0.0;
blockDurationMs = ((double) blockSize / (double) sampleRate) * 1000.0;
messageCallbackQueue = new OrderedMessageQueue();
initObjectInitMap(); // initialise the object factory map

numBytesInInputBuffers = blockSize * numInputChannels * sizeof(float);
numBytesInOutputBuffers = blockSize * numOutputChannels * sizeof(float);
Expand Down Expand Up @@ -229,7 +230,7 @@ void PdContext::initObjectInitMap() {
objectInitMap[string(MessageLessThan::getObjectLabel())] = &MessageLessThan::newObject;
objectInitMap[string(MessageLessThanOrEqualTo::getObjectLabel())] = &MessageLessThanOrEqualTo::newObject;
objectInitMap[string(MessageLine::getObjectLabel())] = &MessageLine::newObject;
objectInitMap[string()] = &MessageLine::newObject; // TODO(mhroth): MessageList collection
objectInitMap[string("list")] = &MessageListAppend::newObject; // MessageListAppend factory creates any kind of list object
objectInitMap[string(MessageLoadbang::getObjectLabel())] = &MessageLoadbang::newObject;
objectInitMap[string(MessageLog::getObjectLabel())] = &MessageLog::newObject;
objectInitMap[string(MessageLogicalAnd::getObjectLabel())] = &MessageLogicalAnd::newObject;
Expand All @@ -245,6 +246,7 @@ void PdContext::initObjectInitMap() {
objectInitMap[string(MessageNotein::getObjectLabel())] = &MessageNotein::newObject;
objectInitMap[string(MessageNotEquals::getObjectLabel())] = &MessageNotEquals::newObject;
objectInitMap[string(MessageOpenPanel::getObjectLabel())] = &MessageOpenPanel::newObject;
objectInitMap[string(MessageOutlet::getObjectLabel())] = &MessageOutlet::newObject;
objectInitMap[string(MessagePack::getObjectLabel())] = &MessagePack::newObject;
objectInitMap[string(MessagePipe::getObjectLabel())] = &MessagePipe::newObject;
objectInitMap[string(MessagePow::getObjectLabel())] = &MessagePow::newObject;
Expand Down Expand Up @@ -316,6 +318,7 @@ void PdContext::initObjectInitMap() {
objectInitMap[string(DspRfft::getObjectLabel())] = &DspRfft::newObject;
objectInitMap[string(DspRifft::getObjectLabel())] = &DspRifft::newObject;
objectInitMap[string(DspSend::getObjectLabel())] = &DspSend::newObject;
objectInitMap[string("s~")] = &DspSend::newObject;
objectInitMap[string(DspSignal::getObjectLabel())] = &DspSignal::newObject;
objectInitMap[string(DspSnapshot::getObjectLabel())] = &DspSnapshot::newObject;
objectInitMap[string(DspSqrt::getObjectLabel())] = &DspSqrt::newObject;
Expand Down Expand Up @@ -506,7 +509,7 @@ bool PdContext::configureEmptyGraphWithParser(PdGraph *emptyGraph, PdFileParser
char resBuffer[RESOLUTION_BUFFER_LENGTH];
initMessage->initWithSARb(INIT_MESSAGE_MAX_ELEMENTS, objectInitString, graph->getArguments(),
resBuffer, RESOLUTION_BUFFER_LENGTH);
MessageObject *messageObject = newObject(objectType, objectLabel, initMessage, graph);
MessageObject *messageObject = newObject(objectLabel, initMessage, graph);
if (messageObject == NULL) {
char *filename = StaticUtils::concatStrings(objectLabel, ".pd");
char *directory = graph->findFilePath(filename);
Expand Down Expand Up @@ -627,21 +630,21 @@ void PdContext::unattachGraph(PdGraph *graph) {
unlock();
}

MessageObject *PdContext::newObject(const char *objectType, const char *objectLabel, PdMessage *initMessage, PdGraph *graph) {
MessageObject *PdContext::newObject(const char *objectLabel, PdMessage *initMessage, PdGraph *graph) {
MessageObject *(*objFactory)(PdMessage *, PdGraph *) = objectInitMap[string(objectLabel)];
if (objFactory != NULL) {
// the factory has been indenified.
return objFactory(initMessage, graph);
} else if(StaticUtils::isNumeric(objectLabel)) {
// special case for constructing a float object from a number
PdMessage *initMessage = PD_MESSAGE_ON_STACK(1);
initMessage->initWithTimestampAndFloat(0.0, atof(objectLabel));
return new MessageFloat(initMessage, graph);
} else {
return NULL; // unknown object
}
/*
if (strcmp(objectType, "obj") == 0) {

MessageObject *(*objFactory)(PdMessage *, PdGraph *) = objectInitMap[string(objectLabel)];
if (objFactory != NULL) {
return objFactory(initMessage, graph);
} else if(StaticUtils::isNumeric(objectLabel)) {
PdMessage *initMessage = PD_MESSAGE_ON_STACK(1);
initMessage->initWithTimestampAndFloat(0.0, atof(objectLabel));
return new MessageFloat(initMessage, graph);
} else {
return NULL;
}


if (strcmp(objectLabel, "+") == 0) {
return new MessageAdd(initMessage, graph);
} else if (strcmp(objectLabel, "-") == 0) {
Expand Down Expand Up @@ -932,6 +935,7 @@ MessageObject *PdContext::newObject(const char *objectType, const char *objectLa
// object was not recognised. It has probably not been implemented yet or exists as an abstraction
return NULL;
*/
}

#pragma mark -
Expand Down
3 changes: 1 addition & 2 deletions src/PdContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ class PdContext {
float getValueForName(char *name);

/** Create a new object in a graph. */
MessageObject *newObject(const char *objectType, const char *objectLabel,
PdMessage *initMessage, PdGraph *graph);
MessageObject *newObject(const char *objectLabel, PdMessage *initMessage, PdGraph *graph);

void registerExternalReceiver(const char *receiverName);
void unregisterExternalReceiver(const char *receiverName);
Expand Down
2 changes: 1 addition & 1 deletion src/ZenGarden.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ZGObject *zg_graph_add_new_object(PdGraph *graph, const char *objectString, floa
char resolutionBuffer[256];
PdMessage *initMessage = PD_MESSAGE_ON_STACK(32);
initMessage->initWithSARb(32, initString, graph->getArguments(), resolutionBuffer, 256);
MessageObject *messageObject = graph->getContext()->newObject((char *) "obj", objectLabel, initMessage, graph);
MessageObject *messageObject = graph->getContext()->newObject(objectLabel, initMessage, graph);
free(objectStringCopy);

if (messageObject != NULL) {
Expand Down

0 comments on commit 3ca91d3

Please sign in to comment.