Jamoma API  0.6.0.a19
TTGraphDescription.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup graphLibrary
4  *
5  * @brief Jamoma Asynchronous Object Graph Layer
6  *
7  * @details Creates a wrapper for TTObjectBases that can be used to build a control graph for asynchronous message passing
8  *
9  * @authors Timothy Place
10  *
11  * @copyright Copyright © 2010 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 "TTGraphDescription.h"
18 #include <fstream>
19 #include <iostream>
20 
21 using namespace std;
22 
23 
25 
26 
27 void TTGraphDescription::exportRuby(const TTString& fullpathToFile)
28 {
29  TTString rubyContent;
30  int index = -1;
31  TTStringVector nodeNames;
32  ofstream rubyFile(fullpathToFile.c_str());
33 
34  rubyContent += "require \"Jamoma\"\n\n";
35  exportRubyNode(rubyContent, index, nodeNames);
36  rubyFile.write(rubyContent.c_str(), rubyContent.size());
37  rubyFile.close();
38 }
39 
40 int TTGraphDescription::exportRubyNode(TTString& content, int& index, TTStringVector& nodeNames)
41 {
42  char objName[16];
43  int localIndex;
44 
45  index++;
46  localIndex = index;
47  snprintf(objName, 16, "obj%i", index);
48  nodeNames.push_back(TTString(objName));
49 
50  content += objName;
51  content += " = TTControl.new \"";
52  content += mClassName.c_str();
53  content += "\"\n";
54 
55  for (TTGraphDescriptionIter input = mInputDescriptions.begin(); input != mInputDescriptions.end(); input++) {
56  int inputIndex = input->exportRubyNode(content, index, nodeNames);
57  content += objName;
58  content += ".connect ";
59  content += nodeNames[inputIndex];
60  content += "\n";
61  }
62  return localIndex;
63 }
64 
65 
66 void TTGraphDescription::exportCpp(const TTString& fullpathToFile)
67 {
68  TTString content;
69  int index = -1;
70  TTStringVector nodeNames;
71  ofstream aFile(fullpathToFile.c_str());
72 
73  content += "#include \"TTGraphAPI.h\"\n\n";
74  content += "int main()\n{\n";
75  content += " TTGraphInit();\n\n";
76 
77  exportCppNode(content, index, nodeNames);
78 
79  content += " return 0;\n";
80  content += "}\n";
81  aFile.write(content.c_str(), content.size());
82  aFile.close();
83 }
84 
85 int TTGraphDescription::exportCppNode(TTString& content, int& index, TTStringVector& nodeNames)
86 {
87  char objName[16];
88  int localIndex;
89 
90  index++;
91  localIndex = index;
92  snprintf(objName, 16, "obj%i", index);
93  nodeNames.push_back(TTString(objName));
94 
95  content += " TTGraphObjectBasePtr ";
96  content += objName;
97  content += ";\n";
98  content += " TTObjectBaseInstantiate(TT(\"graph.object\"), (TTObjectBasePtr*)&";
99  content += objName;
100  content += ", TTValue(TT(\"";
101  content += mClassName.c_str();
102  content += "\")))\n\n";
103 
104  for (TTGraphDescriptionIter input = mInputDescriptions.begin(); input != mInputDescriptions.end(); input++) {
105  int inputIndex = input->exportCppNode(content, index, nodeNames); // note: calls into TTGraph's exportRubyNode
106  content += " ";
107  content += objName;
108  content += "->connect(";
109  content += nodeNames[inputIndex];
110  content += ");\n";
111  }
112  return localIndex;
113 }
114 
115 
116 void TTGraphDescription::exportMax(const TTString& fullpathToFile)
117 {
118  TTString content;
119  int index = -1;
120  TTStringVector nodeNames;
121  ofstream aFile(fullpathToFile.c_str());
122 
123  content += "{\n";
124  content += " \"patcher\" : {\n";
125  content += " \"boxes\" : [\n";
126 
127  exportMaxNode(content, index, nodeNames);
128 
129  content += " ]\n";
130  content += " }\n";
131  content += "}\n";
132  aFile.write(content.c_str(), content.size());
133  aFile.close();
134 }
135 
136 int TTGraphDescription::exportMaxNode(TTString& content, int& index, TTStringVector& nodeNames)
137 {
138  char objName[16];
139  char location[16];
140  int localIndex;
141 
142  index++;
143  localIndex = index;
144  snprintf(objName, 16, "obj%i", index);
145  nodeNames.push_back(TTString(objName));
146 
147  if (index > 0)
148  content += ",\n";
149 
150  content += " {\n";
151  content += " \"box\" : {\n";
152  content += " \"id\" : \"";
153  content += objName;
154  content += "\",\n";
155  content += " \"maxclass\" : \"newobj\",\n";
156  content += " \"text\" : \"jcom.";
157 
158  // TODO: is there a better way to know about object name mappings?
159  if (mClassName == TT("operator"))
160  content += "op";
161  else if (mClassName == TT("graph.input"))
162  content += "pack";
163  else if (mClassName == TT("graph.output"))
164  content += "unpack";
165  else
166  content += mClassName.c_str();
167 
168  content += "#\",\n";
169  content += " \"patching_rect\" : [ 50.0, ";
170  snprintf(location, 16, "%f", 400.0 - (index * 40.0));
171  content += location;
172  content += ", 100.0, 20.0]\n";
173  content += " }\n";
174  content += " }\n";
175 
176  for (TTGraphDescriptionIter input = mInputDescriptions.begin(); input != mInputDescriptions.end(); input++) {
177  int inputIndex = input->exportMaxNode(content, index, nodeNames);
178 
179  if (index == inputIndex) { // I think this means that we are processing the top of the chain?)
180  content += " ],";
181  content += " \"lines\" : [";
182  }
183  else
184  content += ",\n";
185 
186  content += " {\n";
187  content += " \"patchline\" : {\n";
188  content += " \"destination\" : [ \"";
189  content += objName;
190  content += "\", 0],\n";
191  content += " \"source\" : [ \"";
192  content += nodeNames[inputIndex];
193  content += "\", 0]\n";
194  content += " }\n";
195  content += " }\n";
196  }
197  return localIndex;
198 }
const char * c_str() const
Return a pointer to the internal C-string.
Definition: TTString.h:83
STL namespace.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
static TTInt32 sIndex
Used for creating mID.
The TTString class is used to represent a string.
Definition: TTString.h:34
Jamoma Asynchronous Object Graph Layer.