Jamoma API  0.6.0.a19
TTMapperManager.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup modularLibrary
4  *
5  * @brief A MapperManager Object
6  *
7  * @details
8  *
9  * @authors Laurent Garnier
10  *
11  * @copyright © 2010, Laurent Garnier @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 "TTMapperManager.h"
18 #include <libxml/encoding.h>
19 #include <libxml/xmlwriter.h>
20 #include <libxml/xmlreader.h>
21 
22 #define thisTTClass TTMapperManager
23 #define thisTTClassName "MapperManager"
24 #define thisTTClassTags "mapper, manager"
25 
26 
27 TT_MODULAR_CONSTRUCTOR,
28 mAddress(kTTAdrsEmpty)
29 {
30  TTValue v;
31 
33 
34  addMessage(New);
35 
36  // needed to be handled by a TTXmlHandler
37  addMessageWithArguments(WriteAsXml);
38  addMessageWithArguments(ReadFromXml);
39 }
40 
41 TTMapperManager::~TTMapperManager()
42 {
43  New();
44 }
45 
46 TTErr TTMapperManager::New()
47 {
48  mMapperList.clear();
49  return kTTErrNone;
50 }
51 
52 TTErr TTMapperManager::WriteAsXml(const TTValue& inputValue, TTValue& outputValue)
53 {
54  TTObject o = inputValue[0];
55  TTXmlHandlerPtr aXmlHandler = (TTXmlHandlerPtr)o.instance();
56  if (!aXmlHandler)
57  return kTTErrGeneric;
58 
59  TTObject aMapper;
60  TTValue v, attributes;
61  TTSymbol attributeName;
62  TTString s;
63 
64  // Browse the mapper list
65  for (mMapperList.begin(); mMapperList.end(); mMapperList.next()) {
66 
67  xmlTextWriterStartElement((xmlTextWriterPtr)aXmlHandler->mWriter, BAD_CAST "mapper");
68 
69  aMapper = mMapperList.current()[0];
70 
71  // Get mapper attributes
72  aMapper.attributes(attributes);
73  for (TTInt32 i = 0; i < (TTInt32) attributes.size(); i++) {
74 
75  attributeName = attributes[i];
76 
77  // Get string value and fill xml except for FunctionLibrary & FunctionSamples attributes (don't need in xml)
78  if (attributeName != TTSymbol("functionLibrary") && attributeName != TTSymbol("functionSamples") && attributeName != TTSymbol("functionParameters")) {
79 
80  aMapper.get(attributeName, v);
81  v.toString();
82  s = TTString(v[0]);
83  xmlTextWriterWriteFormatAttribute((xmlTextWriterPtr)aXmlHandler->mWriter, BAD_CAST attributeName.c_str(), "%s", BAD_CAST s.c_str());
84  }
85  }
86 
87  aXmlHandler->setAttributeValue(kTTSym_object, aMapper);
88  aXmlHandler->sendMessage(TTSymbol("Write"));
89 
90  // End a mapper
91  xmlTextWriterEndElement((xmlTextWriterPtr)aXmlHandler->mWriter);
92  }
93 
94  return kTTErrNone;
95 }
96 
97 TTErr TTMapperManager::ReadFromXml(const TTValue& inputValue, TTValue& outputValue)
98 {
99  TTObject o = inputValue[0];
100  TTXmlHandlerPtr aXmlHandler = (TTXmlHandlerPtr)o.instance();
101  if (!aXmlHandler)
102  return kTTErrGeneric;
103 
104  TTSymbol attributeName, mute;
105  TTObject newMapper;
106  TTValue v;
107 
108  // Switch on the name of the XML node
109 
110  // Starts reading
111  if (aXmlHandler->mXmlNodeName == TTSymbol("start")) {
112  New();
113  return kTTErrNone;
114  }
115 
116  // Ends reading
117  if (aXmlHandler->mXmlNodeName == TTSymbol("end"))
118  return kTTErrNone;
119 
120 
121  // Comment node
122  if (aXmlHandler->mXmlNodeName == TTSymbol("#comment"))
123  return kTTErrNone;
124 
125  // Mapper node
126  if (aXmlHandler->mXmlNodeName == TTSymbol("mapper")) {
127  mute = TTSymbol("false");
128 
129  // get mute state
130  if (xmlTextReaderMoveToAttribute((xmlTextReaderPtr)aXmlHandler->mReader, BAD_CAST "mute") == 1) {
131  aXmlHandler->fromXmlChar(xmlTextReaderValue((xmlTextReaderPtr)aXmlHandler->mReader), v);
132 
133  if (v.size() == 1)
134  if (v[0].type() == kTypeSymbol)
135  mute = v[0];
136  }
137 
138  if (mute == TTSymbol("false")) {
139 
140  // Create a new mapper
141  newMapper = TTObject(kTTSym_Mapper);
142 
143  // Browse attributes in xml
144  while (xmlTextReaderMoveToNextAttribute((xmlTextReaderPtr)aXmlHandler->mReader) == 1) {
145 
146  // Get attribute name
147  aXmlHandler->fromXmlChar(xmlTextReaderName((xmlTextReaderPtr)aXmlHandler->mReader), v);
148 
149  if (v.size() == 1) {
150  if (v[0].type() == kTypeSymbol) {
151  attributeName = v[0];
152  v.clear();
153 
154  // Get attribute value
155  aXmlHandler->fromXmlChar(xmlTextReaderValue((xmlTextReaderPtr)aXmlHandler->mReader), v);
156 
157  // fill the current mapper
158  newMapper.set(attributeName, v);
159  }
160  }
161  }
162 
163  // Add Mapper in the list
164  mMapperList.append(newMapper);
165  }
166  }
167 
168  return kTTErrNone;
169 }
170 
171 TTErr TTMapperManager::setAddress(const TTValue& value)
172 {
173  New();
174  mAddress = value[0];
175  return kTTErrNone;
176 }
TTErr sendMessage(const TTSymbol name)
TODO: Document this function.
TTString toString(TTBoolean quotes=YES) const
Return the content as a single string with spaces between elements.
Definition: TTValue.h:351
TTErr fromXmlChar(const void *xCh, TTValue &v, TTBoolean addQuote=NO, TTBoolean numberAsSymbol=NO)
TTXmlReader make a TTValue from an xmlChar* using the fromString method (see in TTValue.h)
const char * c_str() const
Return a pointer to the internal C-string.
Definition: TTString.h:83
Create and use Jamoma object instances.
Definition: TTObject.h:29
size_type size() const noexcept
Return the number of elements.
A MapperManager Object.
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
Symbol type.
Definition: TTBase.h:282
TTErr get(const TTSymbol aName, T &aReturnedValue) const
Get an attribute value for an object.
TTErr set(const TTSymbol aName, T aValue)
Set an attribute value for an object.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
#define addMessageWithArguments(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:27
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
void clear()
Clear all values from the vector, leaving with size of 0.
Definition: TTValue.h:131
Something went wrong, but what exactly is not known. Typically used for context-specific problems...
Definition: TTBase.h:344
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
void attributes(TTValue &returnedAttributeNames) const
Return a list of names of the available attributes.
Definition: TTObject.cpp:111
TTSymbol mXmlNodeName
the Node name being read by the Reader
Definition: TTXmlHandler.h:67
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
#define addMessage(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:19
Write / Read mecanism.
Definition: TTXmlHandler.h:48
TTObjectBase * instance() const
Return a direct pointer to the internal instance.
Definition: TTObject.cpp:105
No Error.
Definition: TTBase.h:343
The TTString class is used to represent a string.
Definition: TTString.h:34
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34