[vsipl++] Matlab IO

Jules Bergmann jules at codesourcery.com
Fri Jun 30 18:27:46 UTC 2006


>> You can wrap the 'Swap_value' class with a function:
>>
>> template <typename T>
>> void
>> swap_value(T& value, bool swap)
>> {
>>   if (swap)
>>     swap_value<T, sizeof(T), true>::swap(&value);
>> }
>>
> The reason that I didn't want this to be a function is because if I call 
> this in a loop, it will execute a condition every iteration of the loop. 
> If it is a template parameter, it might be a little better. What do you 
> think?

The template parameter version would be faster than the runtime version 
if called directly.  However, since it is called through a function 
pointer, the benefit is somewhat offset.

To move the conditional and function pointer out of the inside loop, you 
can do something like:

	top_level_read()
	{
	  ... read matlab header ...
	
	  bool swap_bytes = (... check endian ...);

	  if (swap_bytes)
	    low_level_read<true>();
	  else
	    low_level_read<false>();
	}

	template <bool SwapBytes>
	low_level_read()
	{
	  ...
	  swap_value<T, sizeof(T), SwapBytes>::swap(&value);
	}

Even in this case you could benefit from a helper function that deduces 
T and calls sizeof():

	template <bool     SwapBytes,
	          typename T>
	void
	swap_value(T& value)
	{
	  swap_value<T, sizeof(T), SwapBytes>::swap(&value);
	}

which you would call as:

	  swap_value<SwapBytes>(value);

				-- Jules

-- 
Jules Bergmann
CodeSourcery
jules at codesourcery.com
(650) 331-3385 x705



More information about the vsipl++ mailing list