1 /*! \file	mdtraits.h
2  *	\brief	Definition of traits for MDType definitions
3  *
4  *	\version $Id: mdtraits.h,v 1.8 2007/03/31 14:43:16 matt-beard Exp $
5  *
6  */
7 /*
8  *	Copyright (c) 2003, Matt Beard
9  *
10  *	This software is provided 'as-is', without any express or implied warranty.
11  *	In no event will the authors be held liable for any damages arising from
12  *	the use of this software.
13  *
14  *	Permission is granted to anyone to use this software for any purpose,
15  *	including commercial applications, and to alter it and redistribute it
16  *	freely, subject to the following restrictions:
17  *
18  *	  1. The origin of this software must not be misrepresented; you must
19  *	     not claim that you wrote the original software. If you use this
20  *	     software in a product, an acknowledgment in the product
21  *	     documentation would be appreciated but is not required.
22  *
23  *	  2. Altered source versions must be plainly marked as such, and must
24  *	     not be misrepresented as being the original software.
25  *
26  *	  3. This notice may not be removed or altered from any source
27  *	     distribution.
28  */
29 #ifndef MXFLIB__MDTRAITS_H
30 #define MXFLIB__MDTRAITS_H
31 
32 namespace mxflib
33 {
34 	// Forward declare so the class can include pointers to itself (if required)
35 	class MDTraits;
36 
37 	//! A smart pointer to an MDTraits object
38 	typedef SmartPtr<MDTraits> MDTraitsPtr;
39 
40 	//! A list of smart pointers to MDTraits objects
41 	typedef std::list<MDTraitsPtr> MDTraitsList;
42 
43 	//! A map of smart pointers to MDTraits objects, indexed by name
44 	typedef std::map<std::string, MDTraitsPtr> MDTraitsMap;
45 }
46 
47 
48 namespace mxflib
49 {
50 	//! Soft limit for strings returned by MDTraits
51 	/*! \note This is a soft limit in that it is not enforced strictly.
52 	 *        It is possible for string values to be returned that are longer than this value, but where
53 	 *		  the string is built by several passes around a loop that loop should exit once this value
54 	 *		  has been reached
55 	 *
56      * TODO: Apply this limit to everywhere it is required!!
57 	 */
58 	extern UInt32 MDTraits_StringLimit;
59 
60 	//! Set the string size soft limit
SetStringLimit(UInt32 StringLimit)61 	inline void SetStringLimit(UInt32 StringLimit) { MDTraits_StringLimit = StringLimit; }
62 
63 	//! Get the current string size soft limit
GetStringLimit(void)64 	inline UInt32 GetStringLimit(void) { return MDTraits_StringLimit; }
65 }
66 
67 
68 namespace mxflib
69 {
70 	//! Flag to modify string behaviour to terminate all strings written
71 	/*! \note This only works for UTF16 and ISO7 string SetString traits
72 	 */
73 	extern bool TerminateStrings;
74 
75 	//! Set the string termination flag
SetStringTermination(bool Value)76 	inline void SetStringTermination(bool Value) { TerminateStrings = Value; }
77 
78 	//! Get the string termination flag
GetStringTermination(void)79 	inline bool GetStringTermination(void) { return TerminateStrings; }
80 
81 	//! Options for converting labels to a string of text or hex
82 	enum LabelFormat
83 	{
84 		LabelFormatText = 0,			//!< Use just the text, if known, otherwise use hex
85 		LabelFormatHex,				//!< Use only the hex
86 		LabelFormatTextHex,			//!< Use the text, if known, but append the hex
87 		LabelFormatTextHexMask			//!< Use the text, if known, but append the hex if a mask was used in label matching
88 	};
89 
90 	//! The current options for converting labels to strings
91 	extern LabelFormat LabelFormatOption;
92 
93 	//! Set the options for converting labels to strings
SetLabelFormat(LabelFormat Value)94 	inline void SetLabelFormat(LabelFormat Value) { LabelFormatOption = Value; }
95 
96 	//! Get the options for converting labels to strings
GetLabelFormat(void)97 	inline LabelFormat GetLabelFormat(void) { return LabelFormatOption; }
98 }
99 
100 
101 namespace mxflib
102 {
103 	class MDTraits : public RefCount<MDTraits>
104 	{
105 	protected:
106 		//! List of all traits that exist
107 		static MDTraitsMap AllTraits;
108 
109 		//! Protected constructor so all traits need to be created via Create()
MDTraits()110 		MDTraits() {};
111 
112 	public:
113 		//! Allow virtual destruction
~MDTraits()114 		virtual ~MDTraits() {}
115 
116 		//! Does this trait take control of all sub-data and build values in the values own DataChunk?
117 		/*! Normally any contained sub-types (such as array items or compound members) hold their own data */
HandlesSubdata(void)118 		virtual bool HandlesSubdata(void) const { return false; };
119 
120 		//! A unique name for this trait
121 		virtual std::string Name() const = 0;
122 
123 	/* Static Methods */
124 	public:
125 		//! Add a new trait to the list of known traits
126 		/*! \ret True is all went well, else false
127 		 */
128 		static bool Add(std::string Name, MDTraitsPtr Trait);
129 
130 		//! Replace the named trait in the list of known traits
131 		/*! \ret True is all went well, else false
132 		 */
133 		static bool Replace(std::string Name, MDTraitsPtr Trait);
134 
135 		//! Locate a named trait in the list of known traits
136 		/*! \ret A pointer to the named trait, or NULL if not found
137 		 */
138 		static MDTraitsPtr Find(std::string Name);
139 
140 	/* Default implementations */
141 	protected:
142 		virtual void SetInt(MDValuePtr Object, Int32 Val);
143 		virtual void SetInt64(MDValuePtr Object, Int64 Val);
144 		virtual void SetUInt(MDValuePtr Object, UInt32 Val);
145 		virtual void SetUInt64(MDValuePtr Object, UInt64 Val);
146 		virtual void SetString(MDValuePtr Object, std::string Val);
147 		virtual Int32 GetInt(MDValuePtr Object);
148 		virtual Int64 GetInt64(MDValuePtr Object);
149 		virtual UInt32 GetUInt(MDValuePtr Object);
150 		virtual UInt64 GetUInt64(MDValuePtr Object);
151 		virtual std::string GetString(MDValuePtr Object);
152 
153 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
154 
155 		//! Support old capitalization of SetUInt
SetUint(MDValuePtr Object,UInt32 Val)156 		inline void SetUint(MDValuePtr Object, UInt32 Val) { SetUInt(Object, Val); }
157 
158 		//! Support old capitalization of SetUInt64
SetUint64(MDValuePtr Object,UInt64 Val)159 		inline void SetUint64(MDValuePtr Object, UInt64 Val) { SetUInt64(Object, Val); }
160 
161 		//! Support old capitalization of GetUInt
GetUint(MDValuePtr Object)162 		inline UInt32 GetUint(MDValuePtr Object) { return GetUInt(Object); }
163 
164 		//! Support old capitalization of GetUInt64
GetUint64(MDValuePtr Object)165 		inline UInt64 GetUint64(MDValuePtr Object) { return GetUInt64(Object); }
166 
167 		// Give the MDValue class access to our internals to call Set/Get functions
168 		friend class MDValue;
169 	};
170 
171 	//! Create a new trait of this type and add it to the known traits list
172 	/*! \ret The name of the trait as added to the list
173 		*/
CreateMDTraits(void)174 	template<class C> std::string CreateMDTraits(void)
175 	{
176 		MDTraitsPtr Tr = new C;
177 		MDTraits::Add(Tr->Name(), Tr );
178 		return Tr->Name();
179 	}
180 
181 	class MDTraits_DefaultTraits : public MDTraits
182 	{
183 	public:
184 		//! A unique name for this trait
Name()185 		virtual std::string Name() const { return "mxflib::MDTraits_DefaultTraits"; };
186 	};
187 
188 	// Extended implementations
189 	class MDTraits_BasicInt : public MDTraits
190 	{
191 	public:
192 		//! A unique name for this trait
Name()193 		virtual std::string Name() const { return "mxflib::MDTraits_BasicInt"; };
194 
195 	protected:
196 		virtual void SetInt64(MDValuePtr Object, Int64 Val);
197 		virtual void SetUInt(MDValuePtr Object, UInt32 Val);
198 		virtual void SetUInt64(MDValuePtr Object, UInt64 Val);
199 		virtual void SetString(MDValuePtr Object, std::string Val);
200 		virtual Int64 GetInt64(MDValuePtr Object);
201 		virtual UInt64 GetUInt64(MDValuePtr Object);
202 		virtual std::string GetString(MDValuePtr Object);
203 
204 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
205 	};
206 
207 	//! Special unsigned integer ReadValue
208 	size_t ReadValueUInt(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
209 
210 	class MDTraits_Int8 : public MDTraits_BasicInt
211 	{
212 	public:
213 		//! A unique name for this trait
Name()214 		virtual std::string Name() const { return "mxflib::MDTraits_Int8"; };
215 
216 	protected:
217 		virtual void SetInt(MDValuePtr Object, Int32 Val);
218 		virtual Int32 GetInt(MDValuePtr Object);
219 		virtual UInt32 GetUInt(MDValuePtr Object);
220 	};
221 
222 	class MDTraits_UInt8 : public MDTraits_Int8
223 	{
224 	public:
225 		//! A unique name for this trait
Name()226 		virtual std::string Name() const { return "mxflib::MDTraits_UInt8"; };
227 
228 	protected:
229 		virtual std::string GetString(MDValuePtr Object);
230 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
231 	};
232 
233 	class MDTraits_Int16 : public MDTraits_BasicInt
234 	{
235 	public:
236 		//! A unique name for this trait
Name()237 		virtual std::string Name() const { return "mxflib::MDTraits_Int16"; };
238 
239 	protected:
240 		virtual void SetInt(MDValuePtr Object, Int32 Val);
241 		virtual Int32 GetInt(MDValuePtr Object);
242 		virtual UInt32 GetUInt(MDValuePtr Object);
243 	};
244 
245 	class MDTraits_UInt16 : public MDTraits_Int16
246 	{
247 	public:
248 		//! A unique name for this trait
Name()249 		virtual std::string Name() const { return "mxflib::MDTraits_UInt16"; };
250 
251 	protected:
252 		virtual std::string GetString(MDValuePtr Object);
253 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
254 	};
255 
256 	class MDTraits_Int32 : public MDTraits_BasicInt
257 	{
258 	public:
259 		//! A unique name for this trait
Name()260 		virtual std::string Name() const { return "mxflib::MDTraits_Int32"; };
261 
262 	protected:
263 		virtual void SetInt(MDValuePtr Object, Int32 Val);
264 		virtual Int32 GetInt(MDValuePtr Object);
265 		virtual UInt32 GetUInt(MDValuePtr Object);
266 	};
267 
268 	class MDTraits_UInt32 : public MDTraits_Int32
269 	{
270 	public:
271 		//! A unique name for this trait
Name()272 		virtual std::string Name() const { return "mxflib::MDTraits_UInt32"; };
273 
274 	protected:
275 		virtual std::string GetString(MDValuePtr Object);
276 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
277 	};
278 
279 	class MDTraits_Int64 : public MDTraits
280 	{
281 	public:
282 		//! A unique name for this trait
Name()283 		virtual std::string Name() const { return "mxflib::MDTraits_Int64"; };
284 
285 	protected:
286 		virtual void SetInt(MDValuePtr Object, Int32 Val);
287 		virtual void SetInt64(MDValuePtr Object, Int64 Val);
288 		virtual void SetUInt(MDValuePtr Object, UInt32 Val);
289 		virtual void SetUInt64(MDValuePtr Object, UInt64 Val);
290 		virtual void SetString(MDValuePtr Object, std::string Val);
291 		virtual Int32 GetInt(MDValuePtr Object);
292 		virtual UInt32 GetUInt(MDValuePtr Object);
293 		virtual Int64 GetInt64(MDValuePtr Object);
294 		virtual UInt64 GetUInt64(MDValuePtr Object);
295 		virtual std::string GetString(MDValuePtr Object);
296 	};
297 
298 	class MDTraits_UInt64 : public MDTraits_Int64
299 	{
300 	public:
301 		//! A unique name for this trait
Name()302 		virtual std::string Name() const { return "mxflib::MDTraits_UInt64"; };
303 
304 	protected:
305 		virtual std::string GetString(MDValuePtr Object);
306 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
307 	};
308 
309 	class MDTraits_ISO7 : public MDTraits_UInt8
310 	{
311 	public:
312 		//! A unique name for this trait
Name()313 		virtual std::string Name() const { return "mxflib::MDTraits_ISO7"; };
314 
315 	protected:
316 		virtual void SetString(MDValuePtr Object, std::string Val);
317 		virtual std::string GetString(MDValuePtr Object);
318 	};
319 
320 	class MDTraits_UTF16 : public MDTraits_UInt16
321 	{
322 	public:
323 		//! A unique name for this trait
Name()324 		virtual std::string Name() const { return "mxflib::MDTraits_UTF16"; };
325 
326 	protected:
327 		virtual void SetString(MDValuePtr Object, std::string Val);
328 		virtual std::string GetString(MDValuePtr Object);
329 	};
330 
331 	class MDTraits_Raw : public MDTraits
332 	{
333 	public:
334 		//! A unique name for this trait
Name()335 		virtual std::string Name() const { return "mxflib::MDTraits_Raw"; };
336 
337 		//! Does this trait take control of all sub-data and build values in the values own DataChunk?
338 		/*! Normally any contained sub-types (such as array items or compound members) hold their own data */
HandlesSubdata(void)339 		virtual bool HandlesSubdata(void) const { return false; };
340 
341 	protected:
342 		//DRAGONS: Should probably have set integer functions as well
343 		virtual void SetString(MDValuePtr Object, std::string Val);
344 		virtual Int32 GetInt(MDValuePtr Object);
345 		virtual Int64 GetInt64(MDValuePtr Object);
346 		virtual UInt32 GetUInt(MDValuePtr Object);
347 		virtual UInt64 GetUInt64(MDValuePtr Object);
348 		virtual std::string GetString(MDValuePtr Object);
349 
350 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
351 	};
352 
353 
354 	class MDTraits_BasicArray : public MDTraits
355 	{
356 	public:
357 		//! A unique name for this trait
Name()358 		virtual std::string Name() const { return "mxflib::MDTraits_BasicArray"; };
359 
360 	protected:
361 		virtual void SetInt(MDValuePtr Object, Int32 Val);
362 		virtual void SetInt64(MDValuePtr Object, Int64 Val);
363 		virtual void SetUInt(MDValuePtr Object, UInt32 Val);
364 		virtual void SetUInt64(MDValuePtr Object, UInt64 Val);
365 		virtual void SetString(MDValuePtr Object, std::string Val);
366 		virtual Int32 GetInt(MDValuePtr Object);
367 		virtual Int64 GetInt64(MDValuePtr Object);
368 		virtual UInt32 GetUInt(MDValuePtr Object);
369 		virtual UInt64 GetUInt64(MDValuePtr Object);
370 		virtual std::string GetString(MDValuePtr Object);
371 
372 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
373 	};
374 
375 	class MDTraits_BasicStringArray : public MDTraits_BasicArray
376 	{
377 	public:
378 		//! A unique name for this trait
Name()379 		virtual std::string Name() const { return "mxflib::MDTraits_BasicStringArray"; };
380 
381 	protected:
382 		virtual void SetString(MDValuePtr Object, std::string Val);
383 		virtual std::string GetString(MDValuePtr Object);
384 	};
385 
386 	class MDTraits_UTF16String : public MDTraits_BasicStringArray
387 	{
388 	public:
389 		//! A unique name for this trait
Name()390 		virtual std::string Name() const { return "mxflib::MDTraits_UTF16String"; };
391 
392 	protected:
393 		virtual void SetString(MDValuePtr Object, std::string Val);
394 		virtual std::string GetString(MDValuePtr Object);
395 	};
396 
397 	class MDTraits_RawArray : public MDTraits_BasicArray
398 	{
399 	public:
400 		//! A unique name for this trait
Name()401 		virtual std::string Name() const { return "mxflib::MDTraits_RawArray"; };
402 
403 	protected:
404 		virtual void SetString(MDValuePtr Object, std::string Val);
405 		virtual std::string GetString(MDValuePtr Object);
406 	};
407 
408 	class MDTraits_UUID : public MDTraits_Raw
409 	{
410 	public:
411 		//! Does this trait take control of all sub-data and build values in the values own DataChunk?
412 		/*! The entire UUID is held locally */
HandlesSubdata(void)413 		virtual bool HandlesSubdata(void) const { return true; };
414 
415 		//! A unique name for this trait
Name()416 		virtual std::string Name() const { return "mxflib::MDTraits_UUID"; };
417 
418 	protected:
419 		virtual void SetString(MDValuePtr Object, std::string Val);
420 		virtual std::string GetString(MDValuePtr Object);
421 	};
422 
423 	class MDTraits_Label : public MDTraits_Raw
424 	{
425 	public:
426 		//! Does this trait take control of all sub-data and build values in the values own DataChunk?
427 		/*! The entire Label is held locally */
HandlesSubdata(void)428 		virtual bool HandlesSubdata(void) const { return true; };
429 
430 		//! A unique name for this trait
Name()431 		virtual std::string Name() const { return "mxflib::MDTraits_Label"; };
432 
433 	protected:
434 		virtual void SetString(MDValuePtr Object, std::string Val);
435 		virtual std::string GetString(MDValuePtr Object);
436 	};
437 
438 	class MDTraits_UMID : public MDTraits_Raw
439 	{
440 	public:
441 		//! Does this trait take control of all sub-data and build values in the values own DataChunk?
442 		/*! The entire UMID is held locally */
HandlesSubdata(void)443 		virtual bool HandlesSubdata(void) const { return true; };
444 
445 		//! A unique name for this trait
Name()446 		virtual std::string Name() const { return "mxflib::MDTraits_UMID"; };
447 
448 	protected:
449 		virtual void SetString(MDValuePtr Object, std::string Val);
450 		virtual std::string GetString(MDValuePtr Object);
451 	};
452 
453 	class MDTraits_RawArrayArray : public MDTraits_BasicArray
454 	{
455 	public:
456 		//! A unique name for this trait
Name()457 		virtual std::string Name() const { return "mxflib::MDTraits_RawArrayArray"; };
458 
459 	protected:
460 		virtual void SetString(MDValuePtr Object, std::string Val);
461 		virtual std::string GetString(MDValuePtr Object);
462 	};
463 
464 	class MDTraits_BasicCompound : public MDTraits
465 	{
466 	public:
467 		//! A unique name for this trait
Name()468 		virtual std::string Name() const { return "mxflib::MDTraits_BasicCompound"; };
469 
470 	protected:
471 		// DRAGONS: What about all the other set and get functions?
472 		virtual void SetString(MDValuePtr Object, std::string Val);
473 		virtual std::string GetString(MDValuePtr Object);
474 
475 		virtual size_t ReadValue(MDValuePtr Object, const UInt8 *Buffer, size_t Size, int Count=0);
476 	};
477 
478 	class MDTraits_BasicEnum : public MDTraits
479 	{
480 	public:
481 		//! A unique name for this trait
Name()482 		virtual std::string Name() const { return "mxflib::MDTraits_BasicEnum"; };
483 
484 	protected:
485 		// DRAGONS: What about all the other set and get functions?
486 		virtual void SetString(MDValuePtr Object, std::string Val);
487 		virtual std::string GetString(MDValuePtr Object);
488 	};
489 
490 	class MDTraits_Rational : public MDTraits_BasicCompound
491 	{
492 	public:
493 		//! A unique name for this trait
Name()494 		virtual std::string Name() const { return "mxflib::MDTraits_Rational"; };
495 
496 	protected:
497 		virtual void SetString(MDValuePtr Object, std::string Val);
498 		virtual std::string GetString(MDValuePtr Object);
499 	};
500 
501 	class MDTraits_TimeStamp : public MDTraits_BasicCompound
502 	{
503 	public:
504 		//! A unique name for this trait
Name()505 		virtual std::string Name() const { return "mxflib::MDTraits_TimeStamp"; };
506 
507 	protected:
508 		virtual void SetString(MDValuePtr Object, std::string Val);
509 		virtual std::string GetString(MDValuePtr Object);
510 	};
511 }
512 
513 #endif // MXFLIB__MDTRAITS_H
514