Jamoma API  0.6.0.a19
Max/library/Internals/j.send.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxLibrary
4  *
5  * @brief Jamoma For Max Shared Library
6  *
7  * @details Send messages to remote
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2006, Tim Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "Jamoma.h"
18 
19 /** Send Object */
20 typedef struct _send{
21  t_object ob; ///< REQUIRED: Our object
22  TTSenderPtr sender; ///< the TTObject class to send data
23 } t_send;
24 
25 // Prototypes
26 /** Called at object instantiation.
27  @param s Pointer to symbol bassed as message argument to the object.
28  @param argc The number of arguments passed to the object.
29  @param argv Pointer to arguments as an array of atoms.
30  @return Pointer to the newly created object.
31  */
32 void *send_new(t_symbol *s, long argc, t_atom *argv);
33 
34 
35 /** Free up and dispose of the object.
36  @param x Pointer to this object.
37  */
38 void send_free(t_send *x);
39 
40 
41 /** Method for displaying assist strings for inlets and outlets.
42  @param x Pointer to this object.
43  @param b
44  @param msg
45  @param argc
46  @param argv
47  */
48 void send_assist(t_send *x, void *b, long msg, long arg, char *dst);
49 
50 
51 t_max_err send_setaddress(t_send *x, void *attr, AtomCount argc, AtomPtr argv);
52 t_max_err send_getaddress(t_send *x, void *attr, AtomCount *argc, AtomPtr *argv);
53 
54 t_max_err send_setattribute(t_send *x, void *attr, AtomCount argc, AtomPtr argv);
55 t_max_err send_getattribute(t_send *x, void *attr, AtomCount *argc, AtomPtr *argv);
56 
57 /** Forward a bang to the associated receive objects.
58  @param c Pointer to this object.
59  */
60 void send_bang(t_send *x);
61 
62 
63 /** Forward an int to the associated receive objects.
64  @param c Pointer to this object.
65  @param value The value to send.
66  */
67 void send_int(t_send *x, long value);
68 
69 
70 /** Forward a float to the associated receive objects.
71  @param c Pointer to this object.
72  @param value The value to send.
73  */
74 void send_float(t_send *x, double value);
75 
76 
77 /** Forward a list or message to the associated receive objects.
78  @param c Pointer to this object.
79  @param msg The message to send as pointer to a symbol.
80  @param argc The number of arguments of the message.
81  @param argv The arguments as a pointer to an array of atoms.
82  */
83 void send_list(t_send *x, t_symbol *msg, long argc, t_atom *argv);
84 
85 // Globals
86 static t_class *s_send_class; // Required: Global pointer for our class
87 
88 
89 /************************************************************************************/
90 // Main() Function
91 
92 void send_initclass()
93 {
94  t_class *c;
95  t_object *attr;
96 
97  // Define our class
98  c = class_new("j.send",
99  (method)send_new,
100  (method)send_free,
101  sizeof(t_send),
102  (method)0L,
103  A_GIMME,
104  0);
105 
106  // Make methods accessible for our class:
107  class_addmethod(c, (method)send_bang, "bang", 0L);
108  class_addmethod(c, (method)send_int, "int", A_LONG, 0L);
109  class_addmethod(c, (method)send_float, "float", A_FLOAT, 0L);
110  class_addmethod(c, (method)send_list, "list", A_GIMME, 0L);
111  class_addmethod(c, (method)send_list, "anything", A_GIMME, 0L);
112  class_addmethod(c, (method)send_assist, "assist", A_CANT, 0L);
113  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT,0);
114 
115  // ATTRIBUTE: address
116  attr = attr_offset_new("address", _sym_symbol, 0,
117  (method)send_getaddress, (method)send_setaddress, NULL);
118  class_addattr(c, attr);
119 
120  // ATTRIBUTE: attribute
121  attr = attr_offset_new("attribute", _sym_symbol, 0,
122  (method)send_getattribute, (method)send_setattribute, NULL);
123  class_addattr(c, attr);
124 
125  // Finalize our class
126  class_register(CLASS_BOX, c);
127  s_send_class = c;
128 }
129 
130 
131 /************************************************************************************/
132 // Object Life
133 
134 // Create
135 void *send_new(t_symbol *s, long argc, t_atom *argv)
136 {
137  long attrstart = attr_args_offset(argc, argv); // support normal arguments
138  t_send *x = (t_send *)object_alloc(s_send_class);
139 
140  if(x){
141  object_obex_store((void *)x, _sym_dumpout, (object *)outlet_new(x, NULL));
142 
143  attr_args_process(x, argc, argv); // handle attribute args
144 
145  // If no address was specified as an attribute
146  if(attrstart > 0)
147  ;//jamoma_sender_create((ObjectPtr)x, atom_getsym(argv), &x->sender);
148 
149  }
150  return x;
151 }
152 
154 {
155  TTObjectRelease(TTObjectHandle(&x->sender));
156 }
157 
158 
159 /************************************************************************************/
160 // Methods bound to input/inlets
161 
162 // Method for Assistance Messages
163 void send_assist(t_send *x, void *b, long msg, long arg, char *dst)
164 {
165  if(msg==1) // Inlets
166  strcpy(dst, "input to dispatch to j.receive objects");
167  else if(msg==2) // Outlets
168  strcpy(dst, "dumpout");
169 }
170 
171 // ATTRIBUTE: address
172 t_max_err send_setaddress(t_send *x, void *attr, AtomCount argc, AtomPtr argv)
173 {
174  TTValue v;
175 
176  if (atom_gettype(argv) == A_SYM) {
177 
178  v.append(TT(atom_getsym(argv)->s_name));
179  x->sender->setAttributeValue(TTSymbol("Address"), v);
180 
181  }
182  else
183  return MAX_ERR_GENERIC;
184 
185  return MAX_ERR_NONE;
186 }
187 
188 t_max_err send_getaddress(t_send *x, void *attr, AtomCount *argc, AtomPtr *argv)
189 {
190  TTValue v;
191  TTSymbol s;
192 
193  *argc = 1;
194 
195  if (!(*argv)) { // otherwise use memory passed in
196  *argv = (t_atom *)sysmem_newptr(sizeof(t_atom));
197 
198  x->sender->getAttributeValue(TTSymbol("Address"), v);
199  s = NULL;
200  v.get(0, s);
201  atom_setsym(*argv, gensym((char*)s.c_str()));
202  }
203  else
204  return MAX_ERR_GENERIC;
205 
206  return MAX_ERR_NONE;
207 }
208 
209 // ATTRIBUTE: attribute
210 t_max_err send_setattribute(t_send *x, void *attr, AtomCount argc, AtomPtr argv)
211 {
212  TTValue v;
213 
214  if (atom_gettype(argv) == A_SYM) {
215 
216  v.append(TT(atom_getsym(argv)->s_name));
217  x->sender->setAttributeValue(TTSymbol("Attribute"), v);
218 
219  }
220  else
221  return MAX_ERR_GENERIC;
222 
223  return MAX_ERR_NONE;
224 }
225 
226 t_max_err send_getattribute(t_send *x, void *attr, AtomCount *argc, AtomPtr *argv)
227 {
228  TTValue v;
229  TTSymbol s;
230 
231  *argc = 1;
232 
233  if (!(*argv)) { // otherwise use memory passed in
234  *argv = (t_atom *)sysmem_newptr(sizeof(t_atom));
235 
236  x->sender->getAttributeValue(TTSymbol("Attribute"), v);
237  s = NULL;
238  v.get(0, s);
239  atom_setsym(*argv, gensym((char*)s.c_str()));
240  }
241  else
242  return MAX_ERR_GENERIC;
243 
244  return MAX_ERR_NONE;
245 }
246 
248 {
249  send_list(x, _sym_bang, 0, NULL);
250 }
251 
252 void send_int(t_send *x, long value)
253 {
254  t_atom a;
255 
256  atom_setlong(&a, value);
257  send_list(x, _sym_int, 1, &a);
258 }
259 
260 
261 void send_float(t_send *x, double value)
262 {
263  t_atom a;
264 
265  atom_setfloat(&a, value);
266  send_list(x, _sym_float, 1, &a);
267 }
268 
269 void send_list(t_send *x, t_symbol *msg, long argc, t_atom *argv)
270 {
271  TTValue c;
272  TTSenderPtr aTempSender;
273 
274  // Make a temporary sender
275  if (msg->s_name[0] == C_SEPARATOR)
276  {
277  ;/*if (jamoma_sender_create((ObjectPtr)x, msg, &aTempSender)) {
278  jamoma_sender_send(aTempSender, _sym_list, argc, argv);
279  TTObjectRelease(TTObjectHandle(&aTempSender));
280  }*/
281  }
282  else
283  jamoma_sender_send(x->sender, msg, argc, argv);
284 }
void * send_new(t_symbol *s, long argc, t_atom *argv)
Called at object instantiation.
void send_list(t_send *x, t_symbol *msg, long argc, t_atom *argv)
Forward a list or message to the associated receive objects.
TTErr getAttributeValue(const TTSymbol name, TTValue &value)
Get an attribute value for an object.
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
void send_int(t_send *x, long value)
Forward an int to the associated receive objects.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
void get(const TTUInt16 index, T &returnedElementValue) const
DEPRECATED.
Definition: TTValue.h:591
void send_bang(t_send *x)
Forward a bang to the associated receive objects.
const char * c_str() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbol.h:77
void send_assist(t_send *x, void *b, long msg, long arg, char *dst)
Method for displaying assist strings for inlets and outlets.
TTErr JAMOMA_EXPORT jamoma_sender_send(TTObject &aSender, t_symbol *msg, long argc, const t_atom *argv)
Send Max data using a TTSender object.
void send_free(t_send *x)
Free up and dispose of the object.
TTSenderPtr sender
the TTObject class to send data
TTSender ...
Definition: TTSender.h:27
t_object ob
REQUIRED: Our object.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
void send_float(t_send *x, double value)
Forward a float to the associated receive objects.