Jamoma API  0.6.0.a19
j.iter.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternalsGraph
4  *
5  * @brief j.iter# - Iterate key/value pairs from a dictionary to a sequence of Max messages.
6  *
7  * @details
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2011 by Timothy 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 "maxGraph.h"
18 #include "TTCallback.h"
19 
20 
21 // Data Structure for this object
22 struct Iter {
23  t_object obj;
24  TTGraphObjectBasePtr graphObject;
25  TTPtr graphOutlets[16]; // this _must_ be third (for the setup call)
26  TTObject* callback; // TTCallback object that attaches to the graphObject to be notified when there is new data to output.
27 };
28 typedef Iter* IterPtr;
29 
30 
31 // Prototypes for methods
32 IterPtr IterNew (t_symbol* msg, long argc, t_atom* argv);
33 void IterFree (IterPtr self);
34 void IterAssist (IterPtr self, void* b, long msg, long arg, char* dst);
35 void IterGraphCallback (IterPtr self, TTValue& arg);
36 
37 
38 // Globals
39 static t_class* sIterClass;
40 
41 
42 /************************************************************************************/
43 // Main() Function
44 
45 int C74_EXPORT main(void)
46 {
47  t_class* c;
48 
49  TTGraphInit();
50  common_symbols_init();
51 
52  c = class_new("j.iter-", (method)IterNew, (method)IterFree, sizeof(Iter), (method)0L, A_GIMME, 0);
53 
54  class_addmethod(c, (method)MaxGraphReset, "graph.reset", A_CANT, 0);
55  class_addmethod(c, (method)MaxGraphConnect, "graph.connect", A_OBJ, A_LONG, 0);
56  class_addmethod(c, (method)MaxGraphDrop, "graph.drop", A_CANT, 0);
57  class_addmethod(c, (method)MaxGraphObject, "graph.object", A_CANT, 0);
58 
59  class_addmethod(c, (method)IterAssist, "assist", A_CANT, 0);
60  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT, 0);
61 
62  class_register(_sym_box, c);
63  sIterClass = c;
64  return 0;
65 }
66 
67 
68 /************************************************************************************/
69 // Object Creation Method
70 
71 IterPtr IterNew(t_symbol* msg, long argc, t_atom* argv)
72 {
73  IterPtr self;
74  TTValue v, none;
75  TTErr err;
76 
77  self = IterPtr(object_alloc(sIterClass));
78  if (self) {
79  object_obex_store((void*)self, _sym_dumpout, (t_object*)outlet_new(self, NULL)); // dumpout
80  self->graphOutlets[0] = outlet_new(self, NULL);
81 
82  v.resize(2);
83  v[0] = "graph.output";
84  v[1] = 1;
85  err = TTObjectBaseInstantiate(TT("graph.object"), (TTObjectBasePtr*)&self->graphObject, v);
86 
87  if (!self->graphObject->mKernel.valid()) {
88  object_error(SELF, "cannot load Jamoma object");
89  return NULL;
90  }
91 
92  self->callback = new TTObject("callback");
93 
94  self->callback->set(TT("function"), TTPtr(&IterGraphCallback));
95  self->callback->set(TT("baton"), TTPtr(self));
96  // dynamically add a message to the callback object so that it can handle the 'dictionaryReceived' notification
97  self->callback->instance()->registerMessage(TT("dictionaryReceived"), (TTMethod)&TTCallback::notify, kTTMessagePassValue);
98  // tell the graph object that we want to watch it
99  self->graphObject->mKernel.registerObserverForNotifications(*self->callback);
100 
101  attr_args_process(self, argc, argv);
102  }
103  return self;
104 }
105 
106 
107 // Memory Deallocation
108 void IterFree(IterPtr self)
109 {
110  delete self->callback;
111 }
112 
113 
114 /************************************************************************************/
115 // Methods bound to input/inlets
116 
117 // Method for Assistance Messages
118 void IterAssist(IterPtr self, void* b, long msg, long arg, char* dst)
119 {
120  if (msg==1) // Inlets
121  strcpy (dst, "multichannel input and control messages");
122  else if (msg==2) { // Outlets
123  if (arg == 0)
124  strcpy(dst, "multichannel output");
125  else
126  strcpy(dst, "dumpout");
127  }
128 }
129 
130 
131 // C Callback from any Graph Source objects we are observing
132 void IterGraphCallback(IterPtr self, TTValue& arg)
133 {
134  TTDictionaryPtr aDictionary = NULL;
135  TTValue v;
136  TTValue keys;
137  TTUInt32 numKeys;
138 
139  aDictionary = (TTDictionaryPtr)(TTPtr)arg[0];
140  //aDictionary->getValue(v);
141 
142  aDictionary->getKeys(keys);
143  numKeys = keys.size();
144 
145  for (TTUInt32 k=0; k<numKeys; k++) {
146  TTSymbol key;
147  long ac = 0;
148  t_atom* ap = NULL;
149 
150  key = keys[k];
151  aDictionary->lookup(key, v);
152 
153  ac = v.size();
154  if (ac) {
155  ap = new t_atom[ac];
156  for (int i=0; i<ac; i++) {
157  if (v[i].type() == kTypeInt8 ||
158  v[i].type() == kTypeUInt8 ||
159  v[i].type() == kTypeInt16 ||
160  v[i].type() == kTypeUInt16 ||
161  v[i].type() == kTypeInt32 ||
162  v[i].type() == kTypeUInt32 ||
163  v[i].type() == kTypeInt64 ||
164  v[i].type() == kTypeUInt64)
165  {
166  TTInt32 ival;
167 
168  ival = v[i];
169  atom_setlong(ap+i, ival);
170  }
171  else if (v[i].type() == kTypeFloat32 || v[i].type() == kTypeFloat64)
172  {
173  atom_setfloat(ap+i, v[i]);
174  }
175  else if (v[i].type() == kTypeSymbol)
176  {
177  TTSymbol s;
178 
179  s = v[i];
180  atom_setsym(ap+i, gensym((char*)s.c_str()));
181  }
182  }
183 
184  outlet_anything(self->graphOutlets[0], gensym(key.c_str()), ac, ap);
185  delete ap;
186  }
187 
188  }
189 }
190 
191 
The TTGraphObjectBase wraps a TTDSP object such that it is possible to build a dynamic graph of audio...
8-bit unsigned integer, range is 0 through 255.
Definition: TTBase.h:274
Create and use Jamoma object instances.
Definition: TTObject.h:29
64-bit unsigned integer, range is 0 through 18,446,744,073,709,551,615.
Definition: TTBase.h:280
size_type size() const noexcept
Return the number of elements.
TTErr getKeys(TTValue &aSetOfKeys)
Get an array of all of the keys for the hash table.
Definition: TTDictionary.h:250
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
TTErr(TTObjectBase::* TTMethod)(const TTSymbol methodName, const TTValue &anInputValue, TTValue &anOutputValue)
A type that can be used to store a pointer to a message for an object.
Definition: TTObjectBase.h:46
Symbol type.
Definition: TTBase.h:282
16-bit unsigned integer, range is 0 through 65,535.
Definition: TTBase.h:276
A type that represents the key as a C-String and the value as a pointer to the matching TTSymbol obje...
Definition: TTDictionary.h:47
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
This class is used to create a backward communication channel to notify a client that something chang...
16-bit signed integer, range is −32,768 through 32,767.
Definition: TTBase.h:275
64-bit floating point
Definition: TTBase.h:272
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
const char * c_str() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbol.h:77
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
64-bit signed integer, ragne is −9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
Definition: TTBase.h:279
32-bit floating point
Definition: TTBase.h:271
Set this flag if the method you are binding to this message is prototyped with a single TTValue& argu...
Definition: TTObjectBase.h:87
32-bit signed integer, range is -2,147,483,648 through 2,147,483,647.
Definition: TTBase.h:277
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr lookup(const TTSymbol aKey, TTValue &aValue) const
Find the value for the given key.
Definition: TTDictionary.h:221
int C74_EXPORT main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
Definition: j.iter.cpp:45
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
32-bit unsigned integer, range is 0 through 4,294,967,295.
Definition: TTBase.h:278
8-bit signed integer, range is -128 through 127.
Definition: TTBase.h:273
TTErr notify(const TTValue &anInputValue, TTValue &anUnusedOutputValue)
Message called because we are registered as an observer to some other object, and then calls our exte...
Definition: TTCallback.cpp:69
TTDictionary * TTDictionaryPtr
[doxygenAppendixC_typedefExample]
Definition: TTDictionary.h:279
void resize(size_type n)
Change the number of elements.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34