1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4 	This file is part of COLLADAStreamWriter.
5 
6     Licensed under the MIT Open Source License,
7     for details please see LICENSE file or the website
8     http://www.opensource.org/licenses/mit-license.php
9 */
10 
11 #ifndef __COLLADASTREAMWRITER_SOURCE_H__
12 #define __COLLADASTREAMWRITER_SOURCE_H__
13 
14 #include "COLLADASWPrerequisites.h"
15 #include "COLLADASWElementWriter.h"
16 #include "COLLADASWConstants.h"
17 
18 #include <string>
19 #include <vector>
20 
21 namespace COLLADASW
22 {
23 
24     /** A class to add a source, including the array and an accessor.
25     It is the base class for the Source template class.*/
26 
27     class SourceBase : public ElementWriter
28     {
29 
30     public:
31 
32         /** A list that contains the names of all the parameter in the accessor*/
33         typedef std::vector<String> ParameterNameList;
34 
35     private:
36 
37         TagCloser mSourceCloser;
38 
39         /** The id of the source*/
40         String mNodeId;
41 
42         /** The name of source node */
43         String mNodeName;
44 
45         /** The id of the array*/
46         String mArrayId;
47 
48         /** The value of the count attribute of the accessor*/
49         unsigned long mAccessorCount;
50 
51         /** The value of the stride attribute of the accessor*/
52         unsigned long mAccessorStride;
53 
54         /** The list with the parameters. */
55         ParameterNameList mParameterNameList;
56 
57     public:
58 
SourceBase(StreamWriter * streamWriter)59     	SourceBase ( StreamWriter* streamWriter )
60             : ElementWriter ( streamWriter )
61             , mAccessorCount (0)
62             , mAccessorStride (0)
63         {}
64 
65         /** Returns a reference to the id of the source*/
getId()66         const String& getId() const
67         {
68             return mNodeId;
69         }
70 
71         /** Sets the id of the source*/
setId(const String & id)72         void setId ( const String& id )
73         {
74             mNodeId = id;
75         }
76 
77         /** Returns a reference to the name of the source*/
getNodeName()78         const String& getNodeName() const
79         {
80             return mNodeName;
81         }
82 
83         /** Sets the name of the source*/
setNodeName(const String & name)84         void setNodeName ( const String& name )
85         {
86             mNodeName = name;
87         }
88 
89         /** Returns a reference to the id of the array of the node*/
getArrayId()90         const String& getArrayId() const
91         {
92             return mArrayId;
93         }
94 
95         /** Sets the id of the array of the node*/
setArrayId(const String & arrayId)96         void setArrayId ( const String& arrayId )
97         {
98             mArrayId = arrayId;
99         }
100 
101         /** Returns the value of the count attribute of the accessor*/
getAccessorCount()102         unsigned long getAccessorCount() const
103         {
104             return mAccessorCount;
105         }
106 
107         /** Sets the count attribute of the accessor*/
setAccessorCount(unsigned long accessorCount)108         void setAccessorCount ( unsigned long accessorCount )
109         {
110             mAccessorCount = accessorCount;
111         }
112 
113         /** Returns the value of the stride attribute of the accessor*/
getAccessorStride()114         unsigned long getAccessorStride() const
115         {
116             return mAccessorStride;
117         }
118 
119         /** Sets the stride attribute of the accessor*/
setAccessorStride(unsigned long accessorStride)120         void setAccessorStride ( unsigned long accessorStride )
121         {
122             mAccessorStride = accessorStride;
123         }
124 
125         /** Returns a reference to the parameter list*/
getParameterNameList()126         ParameterNameList& getParameterNameList()
127         {
128             return mParameterNameList;
129         }
130 
131 
132     protected:
133 
134         /** Prepares to fill the array. This member must be called exactly once
135         before add is called the first time.*/
136         void prepareBaseToAppendValues ( const String* arrayName );
137 
138         /** This function must be called after the last value has been added to the array and before another
139         element has been opened*/
140         void finishBase ( const String* parameterTypeName, bool closeSourceElement=true );
141 
142         /** Close the array element. */
143         void closeArray();
144 
145         /** Close the source element. */
146         void closeSource();
147 
148         /** Adds the base technique common to the source. */
149         void addBaseTechnique ( const String* parameterTypeName );
150 
151     };
152 
153     /** A class template to add a source, including an the array and an accessor*/
154     template < class Type, const String* arrayName, const String* parameterTypeName >
155     class Source : public SourceBase
156     {
157 
158     private:
159 
160         const String* mParameterTypeName;
161 
162     public:
Source(StreamWriter * streamWriter)163         Source ( StreamWriter * streamWriter )
164         : SourceBase ( streamWriter )
165         , mParameterTypeName ( parameterTypeName )
166         {}
167 
168         /** Prepares to fill the array. This member must be called exactly once
169         before add is called the first time.*/
prepareToAppendValues()170         inline void prepareToAppendValues()
171         {
172             prepareBaseToAppendValues ( arrayName );
173         }
174 
175         /** Adds @a value to the array*/
appendValues(const double matrix[][4])176         void appendValues ( const double matrix[][4] )
177         {
178             mSW->appendValues ( matrix );
179         }
180 
181         /** Adds @a value to the array*/
appendValues(const float matrix[][4])182         void appendValues ( const float matrix[][4] )
183         {
184             mSW->appendValues ( matrix );
185         }
186 
187         /** Adds @a value to the array*/
appendValues(const std::vector<Type> & value)188         void appendValues ( const std::vector<Type>& value )
189         {
190             mSW->appendValues ( value );
191         }
192 
193         /** Adds @a value to the array*/
appendValues(const Type value)194         void appendValues ( const Type value )
195         {
196             mSW->appendValues ( value );
197         }
198 
199         /** Adds @a value1  and @a value2 to the array*/
appendValues(const Type value1,const Type value2)200         void appendValues ( const Type value1, const Type value2 )
201         {
202             mSW->appendValues ( value1, value2 );
203         }
204 
205         /** Adds @a value1, @a value2 and @a value3 to the array*/
appendValues(const Type value1,const Type value2,const Type value3)206         void appendValues ( const Type value1, const Type value2, const Type value3 )
207         {
208             mSW->appendValues ( value1, value2, value3 );
209         }
210 
211         /** Adds @a value1, @a value2, @a value3 and @a value4 to the array*/
appendValues(const Type value1,const Type value2,const Type value3,const Type value4)212         void appendValues ( const Type value1, const Type value2, const Type value3, const Type value4 )
213         {
214             mSW->appendValues ( value1, value2, value3, value4 );
215         }
216 
217         /**
218          * This function must be called after the last value has been added to the array
219          * and before another element has been opened.
220          * @param closeSourceElement false, if we want to add some extra tags and close the element later.
221          */
222         void finish ( bool closeSourceElement=true )
223         {
224             finishBase ( mParameterTypeName, closeSourceElement );
225         }
226 
227         /** Close the source element. */
closeSourceElement()228         void closeSourceElement()
229         {
230             closeSource();
231         }
232 
233         /** Sets the parameter type name */
setParameterTypeName(const String * paramTypeName)234         void setParameterTypeName ( const String* paramTypeName )
235         {
236             mParameterTypeName = paramTypeName;
237         }
238     };
239 
240     /** Param type independent source with double values. */
241     typedef Source < double, &CSWC::CSW_ELEMENT_FLOAT_ARRAY, &CSWC::EMPTY_STRING > TypeIndependentSource;
242     /** Param type independent source with float values. */
243     typedef Source < float, &CSWC::CSW_ELEMENT_FLOAT_ARRAY, &CSWC::EMPTY_STRING > TypeIndependentSourceF;
244 
245     /** Param type "FLOAT" source with double values. */
246     typedef Source < double, &CSWC::CSW_ELEMENT_FLOAT_ARRAY, &CSWC::CSW_VALUE_TYPE_FLOAT > FloatSource;
247     /** Param type "FLOAT" source with float values. */
248     typedef Source < float, &CSWC::CSW_ELEMENT_FLOAT_ARRAY, &CSWC::CSW_VALUE_TYPE_FLOAT > FloatSourceF;
249 
250     /** Param type "FLOAT4x4" source with double values. */
251     typedef Source < double, &CSWC::CSW_ELEMENT_FLOAT_ARRAY, &CSWC::CSW_VALUE_TYPE_FLOAT4x4 > Float4x4Source;
252     /** Param type "FLOAT4x4" source with double values. */
253     typedef Source < float, &CSWC::CSW_ELEMENT_FLOAT_ARRAY, &CSWC::CSW_VALUE_TYPE_FLOAT4x4 > Float4x4SourceF;
254 
255     /** Param type "NAME" source with String values. */
256     typedef Source < String, &CSWC::CSW_ELEMENT_NAME_ARRAY, &CSWC::CSW_VALUE_TYPE_NAME > NameSource;
257 
258     /** Param type "NAME" source with String values. */
259     typedef Source < String, &CSWC::CSW_ELEMENT_IDREF_ARRAY, &CSWC::CSW_VALUE_TYPE_IDREF > IdRefSource;
260 
261 } //namespace COLLADASW
262 
263 
264 #endif //__COLLADASTREAMWRITER_SOURCE_H_
265