1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4     This file is part of COLLADASaxFrameworkLoader.
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 __COLLADASAXFWL_SOURCEARRAYLOADER_H__
12 #define __COLLADASAXFWL_SOURCEARRAYLOADER_H__
13 
14 #include "COLLADASaxFWLPrerequisites.h"
15 #include "COLLADASaxFWLSource.h"
16 #include "COLLADASaxFWLFilePartLoader.h"
17 #include "COLLADASaxFWLXmlTypes.h"
18 #include "COLLADAFWFloatOrDoubleArray.h"
19 
20 
21 namespace COLLADASaxFWL
22 {
23 
24     /**
25     Base class for elements, that contain source arrays. The derived classes need to implement
26 	the corresponding begin_source and end_soure callback function and call the beginSource and
27 	endSource function. All the tags in between are handled by this class. It provides an array,
28 	containing all the loaded sources.
29     */
30     class SourceArrayLoader : public FilePartLoader
31     {
32 	public:
33 		static const COLLADAFW::FloatOrDoubleArray::DataType DATA_TYPE_REAL;
34 
35 	protected:
36 
37         /**
38         * Provides the bulk of the mesh's vertex data. See main entry.
39         */
40         SourceArray mSourceArray;
41 
42 		/** The source currently being parsed.*/
43 		SourceBase* mCurrentSoure;
44 
45 		/** The id of the source being parsed.*/
46 		String mCurrentSourceId;
47 
48 		/** The id of the array being parsed.*/
49 		String mCurrentArrayId;
50 
51 	public:
52 		/** Takes a null terminated string, that represents an uriFragment of URIFragmentType defined in the
53 		COLLADA XSD and returns the id it points to.*/
54 		static String getIdFromURIFragmentType( const char* uriFragment );
55 
56 		/** Copies the values contained in @a realSource into @a realsArray .*/
57 		static void setRealValues( COLLADAFW::FloatOrDoubleArray& realsArray, const RealSource* realSource );
58 
59 	protected:
60 
61         /** Constructor. */
62 		SourceArrayLoader ( IFilePartLoader* callingFilePartLoader );
63 
64         /** Destructor. */
65 		virtual ~SourceArrayLoader();
66 
67 		/** Assigns the float or double values, stored in @a source in @a floatOrDoubleArray. If type of
68 		@a source is neither float nor double, false is returned and no assignment performed. The values
69 		are not copied, but the source number array is assignment to the @a floatOrDoubleArray array.*/
70 		bool assignSourceValuesToFloatOrDoubleArray( SourceBase* source, COLLADAFW::FloatOrDoubleArray& floatOrDoubleArray);
71 
72 		/** Returns the id of the source being parsed.*/
getCurrentSourceId()73 		const String& getCurrentSourceId() const { return mCurrentSourceId; }
74 
75 		/** Clears all the source loaded by the source array loader.*/
76 		void clearSources();
77 
78         /**
79         * Provides the bulk of the mesh's vertex data. See main entry.
80         * @param sourceArraySize Parameter to get the size of the source array.
81         * @return const SourceArray The source array.
82         */
83         const SourceArray& getSourceArray () const;
84 
85         /**
86         * Provides the bulk of the mesh's vertex data. See main entry.
87         * @param sourceArray The source array.
88         * @param sourceArraySize The size of the source array.
89         */
90         void setSourceArray ( const SourceArray& sourceArray );
91 
92         /**
93         * Returns the source element of the source array with the given id or 0 if it not exist.
94         * @param sourceId The source id of the searched source element.
95         * @return COLLADAFW::Source The source element with the given id or 0 if it not exist.
96         */
97         const SourceBase* getSourceById ( const String& sourceId ) const;
98 
99         /**
100         * Returns the source element of the source array with the given id or 0 if it not exist.
101         * @param sourceId The source id of the searched source element.
102         * @return COLLADAFW::Source The source element with the given id or 0 if it not exist.
103         */
104         SourceBase* getSourceById ( const String& sourceId );
105 
106 		/** Handles the beginning of a source element. Should be called by derived classes,
107 		when an opening \<source\> tag is detected.*/
108 		bool beginSource(const source__AttributeData& attributes);
109 
110 		/** Handles the ending of a source element. Should be called by derived classes,
111 		when a closing \<source\> tag is detected.*/
112 		bool endSource();
113 
114 		/** Handles the beginning of a array element. Should be called when ever an array is opened.
115 		@tparam SourceType Type of source to create a new instance from for the opened array, e.g. FloatSource for <float_array>
116 		@param count The Value of the count attribute of the aray
117 		@param id The if of the array element
118 		@return The new created source*/
119 		template<class SourceType>
120 		SourceType* beginArray( uint64 count, const ParserChar* id );
121 
122 
123     public:
124 		/** Sax callback function for the beginning of a float array element.*/
125 		virtual bool begin__float_array( const float_array__AttributeData& attributeData );
126 
127 		/** Sax callback function for the ending of a float array element.*/
128 		virtual bool end__float_array();
129 
130 		/** Sax callback function for the float data of a float array element.*/
131 		virtual bool data__float_array( const float* data, size_t length );
132 
133 
134 		/** Sax callback function for the beginning of a technique_common element.*/
135 		virtual bool begin__animation__source__technique_common();
136 
137 		/** Sax callback function for the ending of a technique_common element.*/
138 		virtual bool end__animation__source__technique_common();
139 
140 
141 		/** Sax callback function for the beginning of a accessor element.*/
142 		virtual bool begin__accessor( const accessor__AttributeData& attributeData );
143 
144 		/** Sax callback function for the ending of a accessor element.*/
145 		virtual bool end__accessor();
146 
147 
148 		/** Store the accessor parameter in the source's accessor.*/
149 		virtual bool begin__param( const param__AttributeData& attributeData );
150 
151 		/** We don't need to do anything here.*/
end__param()152 		virtual bool end__param(){return true;}
153 
154 		/** We don't need to do anything here.*/
data__param(const ParserChar * value,size_t length)155 		virtual bool data__param( const ParserChar* value, size_t length ){return true;}
156 
157 
158 	private:
159 
160         /** Disable default copy ctor. */
161 		SourceArrayLoader( const SourceArrayLoader& pre );
162 
163         /** Disable default assignment operator. */
164 		const SourceArrayLoader& operator= ( const SourceArrayLoader& pre );
165 	};
166 
167 
168 	//------------------------------
169 	template<class SourceType>
beginArray(uint64 count,const ParserChar * id)170 	SourceType* SourceArrayLoader::beginArray( uint64 count,  const ParserChar* id )
171 	{
172 		SourceType* newSource = new SourceType();
173 		newSource->getArrayElement().getValues().allocMemory((size_t)count);
174 		newSource->setId(mCurrentSourceId);
175 		mCurrentSoure = newSource;
176 		if ( id )
177 			mCurrentArrayId = id;
178 		return newSource;
179 	}
180 
181 
182 
183 } // namespace COLLADAFW
184 
185 #endif // __COLLADASAXFWL_SOURCEARRAYLOADER_H__
186