Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 166 lines (137 sloc) 6.365 kB
4b71bbe @mhroth Add LGPL license to remaining files.
authored
1 /*
5db1810 @mhroth Allow new messages to be created with a string in ZenGarden.h
authored
2 * Copyright 2009,2011,2012 Reality Jockey, Ltd.
4b71bbe @mhroth Add LGPL license to remaining files.
authored
3 * info@rjdj.me
4 * http://rjdj.me/
5 *
6 * This file is part of ZenGarden.
7 *
8 * ZenGarden is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ZenGarden is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with ZenGarden. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
abb33ec First commit of current source.
Martin Roth authored
23 #ifndef _PD_MESSAGE_H_
24 #define _PD_MESSAGE_H_
25
f3c0bff Update to new PdMessage syntax. Comment out difficult parts of Messag…
Martin Roth authored
26 #include <alloca.h>
735c588 @mhroth Add varargs function setMessage to PdMessage. This is in preparation …
authored
27 #include <stdarg.h>
f3c0bff Update to new PdMessage syntax. Comment out difficult parts of Messag…
Martin Roth authored
28 #include <stdio.h>
29 #include <stdlib.h>
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
30 #include <string.h>
31 #include "MessageElementType.h"
32
5db1810 @mhroth Allow new messages to be created with a string in ZenGarden.h
authored
33 #define PD_MESSAGE_ON_STACK(_x) ((PdMessage *) alloca(PdMessage::numBytes(_x)));
f3c0bff Update to new PdMessage syntax. Comment out difficult parts of Messag…
Martin Roth authored
34
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
35 typedef struct MessageAtom {
36 MessageElementType type;
37 union {
38 float constant;
39 char *symbol;
40 };
41 } MessageAtom;
abb33ec First commit of current source.
Martin Roth authored
42
f2ae825 @mhroth add pdm10 branch
authored
43
44 /** Implements a Pd message. */
abb33ec First commit of current source.
Martin Roth authored
45 class PdMessage {
46
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
47 public:
fbf7f65 @mhroth First attempt at comprehensive argument resolution (e.g. $1).
authored
48 /**
05a1dcd Update header comments and remove old code in PdMessage.
Martin Roth authored
49 * Resolve arguments in a string with a given argument list into the given <code>buffer</code>
50 * with <code>bufferLength</code>.
fbf7f65 @mhroth First attempt at comprehensive argument resolution (e.g. $1).
authored
51 * The <code>offset</code> parameter adjusts the resolution of the <code>$</code> argument.
52 * In the case of message boxes, <code>$0</code> refers to the graph id, which is the first
53 * argument in a graph's list of arguments. For all other objects, <code>$1</code> refers
54 * to the first (0th) element of the argument. The offset is used to distinguish between these
55 * cases, by offsetting the argument index resolution.
cd64960 @mhroth First attempt at argument resolution.
authored
56 */
5cc26d8 In PdGraph, remove unnecessary imports, old class variables, set Mess…
Martin Roth authored
57 static void resolveString(char *initString, PdMessage *arguments, unsigned int offset,
58 char *buffer, unsigned int bufferLength);
cd64960 @mhroth First attempt at argument resolution.
authored
59
60 /**
a61bf70 @mhroth Update trigger, pack, and unpack to take advantage of PdMessage::reso…
authored
61 * Converts symbolic elements referring to message element types (e.g., float or f) to those
62 * types. For instance, if an element has a symbolic value of "float", then that element is
63 * converted into one of type <code>FLOAT</code>.
64 */
65 void resolveSymbolsToType();
66
e0b13ab Refactor PdMessage to resolve strings in an init-style function.
Martin Roth authored
67 void initWithTimestampAndNumElements(double aTimestamp, unsigned int numElem);
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
68 void initWithTimestampAndFloat(double aTimestamp, float constant);
69 void initWithTimestampAndBang(double aTimestamp);
70 void initWithTimestampAndSymbol(double aTimestamp, char *symbol);
71
664f229 @mhroth Properly make resolution buffer for messages available at a point whe…
authored
72 /**
73 * Initialise the message with a string, arguments, and a resolution buffer. The string will
74 * be resolved into the buffer using the arguments. Any resolved strings in the message will
75 * point into the resolution buffer. The buffer is generally intended to be a temporary storage
76 * for such strings while objects are created.
77 */
78 void initWithSARb(unsigned int maxElements, char *initString, PdMessage *arguments, char *buffer,
79 unsigned int bufferLength);
e0b13ab Refactor PdMessage to resolve strings in an init-style function.
Martin Roth authored
80
81 /**
5db1810 @mhroth Allow new messages to be created with a string in ZenGarden.h
authored
82 * Adds elements to the message by tokenizing the given string. If a token is numeric then it is
83 * automatically resolved to a float. Otherwise the string is interpreted as a symbol. Note that
84 * the <code>initString</code> is tokenized and should be provided in a buffer which may be edited.
e0b13ab Refactor PdMessage to resolve strings in an init-style function.
Martin Roth authored
85 */
5db1810 @mhroth Allow new messages to be created with a string in ZenGarden.h
authored
86 void initWithString(double timestamp, unsigned int maxElements, char *initString);
e0b13ab Refactor PdMessage to resolve strings in an init-style function.
Martin Roth authored
87
79eb749 Set a PdMessage message element to a FLOAT or SYMBOL according to an …
Martin Roth authored
88 /** Sets the given message element to a FLOAT or SYMBOL depending on contents of string. */
b303d47 @mhroth Allow bang to be described as "!" in a message string.
authored
89 void parseAndSetMessageElement(unsigned int index, char *initString);
79eb749 Set a PdMessage message element to a FLOAT or SYMBOL according to an …
Martin Roth authored
90
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
91 MessageAtom *getElement(unsigned int index);
92
93 bool atomIsEqualTo(unsigned int index, MessageAtom *messageAtom);
abb33ec First commit of current source.
Martin Roth authored
94
95 int getNumElements();
96
f2ae825 @mhroth add pdm10 branch
authored
97 /** Get the global timestamp of this message (in milliseconds). */
98 double getTimestamp();
99
100 /** Set the global timestamp of this message (in milliseconds). */
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
101 void setTimestamp(double timestamp); // NOTE(mhroth): necessary?
abb33ec First commit of current source.
Martin Roth authored
102
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
103 /**
104 * Returns a copy of the message to the heap. Messages usually only exist temporarily on the
f3c0bff Update to new PdMessage syntax. Comment out difficult parts of Messag…
Martin Roth authored
105 * stack and should be copied to the heap if it should persist. Symbol pointers are copied
106 * independently to the heap.
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
107 */
108 PdMessage *copyToHeap();
e575039 @mhroth Add MessageListSplit. Update PdMessage to be able to quickly clear an…
authored
109
05a1dcd Update header comments and remove old code in PdMessage.
Martin Roth authored
110 /** The message memory is freed from the heap, including symbols. */
e0b13ab Refactor PdMessage to resolve strings in an init-style function.
Martin Roth authored
111 void freeMessage();
cc816bc toString method on PdMessage objects
chrism authored
112
f2ae825 @mhroth add pdm10 branch
authored
113 /**
114 * Create a string representation of the message. Suitable for use by the print object.
115 * The resulting string must be <code>free()</code>ed by the caller.
116 */
cc816bc toString method on PdMessage objects
chrism authored
117 char *toString();
f2ae825 @mhroth add pdm10 branch
authored
118
288d022 @mhroth Add isFloat() and getFloat() convenience functions to PdMessage. Disa…
authored
119 /** Convenience function to determine if a particular message element is a float. */
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
120 bool isFloat(unsigned int index);
121 bool isSymbol(unsigned int index);
122 bool isSymbol(unsigned int index, const char *test);
123 bool isBang(unsigned int index);
5811ab8 @mhroth Architecture updates to dsp objects.
authored
124 bool hasFormat(const char *format);
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
125 MessageElementType getType(unsigned int index);
288d022 @mhroth Add isFloat() and getFloat() convenience functions to PdMessage. Disa…
authored
126
127 /**
128 * Convenience function to get the float value from a particular message element. The user
129 * is responsible for checking that the indexed <code>MessageElement</code> is truly a float.
e572e69 @mhroth Add setFloat and setSymbol convenience functions to PdMessage
authored
130 * This function does not check for the existence of the message element.
288d022 @mhroth Add isFloat() and getFloat() convenience functions to PdMessage. Disa…
authored
131 */
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
132 float getFloat(unsigned int index);
133 char *getSymbol(unsigned int index);
e572e69 @mhroth Add setFloat and setSymbol convenience functions to PdMessage
authored
134
135 /**
136 * Convenience function to set a message element to a float value. This function does not check
137 * for the existence of a message element.
138 */
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
139 void setFloat(unsigned int index, float value);
140 void setSymbol(unsigned int index, char *symbol);
141 void setBang(unsigned int index);
142 void setAnything(unsigned int index);
e0b13ab Refactor PdMessage to resolve strings in an init-style function.
Martin Roth authored
143 void setList(unsigned int index);
5db1810 @mhroth Allow new messages to be created with a string in ZenGarden.h
authored
144
145 /** Returns the number of bytes in a PdMessage structure with <code>x</code> number of elements. */
146 static inline unsigned int numBytes(unsigned int x) {
147 return sizeof(PdMessage) + (((x>0)?(x-1):0) * sizeof(MessageAtom));
148 }
149
150 /**
151 * Returns the number of bytes in the PdMessage structure
152 * (as it is variable depending on the number of elements).
153 */
154 unsigned int numBytes();
cc816bc toString method on PdMessage objects
chrism authored
155
f2ae825 @mhroth add pdm10 branch
authored
156 private:
05a1dcd Update header comments and remove old code in PdMessage.
Martin Roth authored
157 PdMessage();
e0b13ab Refactor PdMessage to resolve strings in an init-style function.
Martin Roth authored
158 ~PdMessage();
5cc26d8 In PdGraph, remove unnecessary imports, old class variables, set Mess…
Martin Roth authored
159
f2ae825 @mhroth add pdm10 branch
authored
160 double timestamp;
29f83db Towards an a stack-based messaging system. First commit.
Martin Roth authored
161 int numElements;
162 MessageAtom messageAtom;
abb33ec First commit of current source.
Martin Roth authored
163 };
164
165 #endif // _PD_MESSAGE_H_
Something went wrong with that request. Please try again.