Code Search for Developers
 
 
  

Aligned.h from redshed at Krugle


Show Aligned.h syntax highlighted

/****************************************************************************************
	Aligned.h $Revision: 10 $
		<http://rentzsch.com/align>
	
	Copyright © 1999-2002 Red Shed Software. All rights reserved.
	by Jonathan 'Wolf' Rentzsch (jon at redshed dot net)
	
	************************************************************************************/

#ifndef		_Aligned_
#define		_Aligned_

#include	"Align.h"
#include	<new>

/*	wolf -- Fri, Aug 6, 1999: CWPro5 fixes the behavior documented below.
//#include	<new.h>
//	Sadly, CWPro2's placement new operator is defined as throw(),
//	which causes an illegal function overloading error which I can't figure out
//	how to fix. So instead of #including new.h, I just define my own placement new
//	operator here.
	inline
	void*
operator new(
	size_t,
	void	*location )
{	return( location ); }*/

template	<class	TYPE, UInt8 ALIGNMENT = 4>
class	Aligned	{
	public:
		Aligned()
		:	ptr_( (TYPE*) this )
		{	AlignX( &ptr_, ALIGNMENT );
			ptr_ = new (ptr_) TYPE; }
		
		Aligned(
			const	TYPE	&value )
		:	ptr_( (TYPE*) this )
		{	AlignX( (void**) &ptr_, ALIGNMENT );
			ptr_ = (TYPE*) (new (ptr_) TYPE( value )); }
		
		~Aligned()
		{	ptr_->~TYPE(); }
		
		operator	TYPE()	const
		{	return( *(TYPE*) ptr_ ); }
		
		TYPE&	operator*()
		{	return( *ptr_ ); }
		
		TYPE*	operator->()
		{	return( ptr_ ); }
		
		TYPE*	operator&()
		{	return( ptr_ ); }
	private:
		UInt8	padding_[ ALIGNMENT ];
		UInt8	storage_[ sizeof( TYPE ) ];
		TYPE	*ptr_;
};

#endif	//	_Aligned_



See more files for this project here

redshed

Code for Mac+WebObjects.

Project homepage: http://sourceforge.net/projects/redshed
Programming language(s): C,Java,Objective C
License: other

  Align.c
  Align.h
  Aligned.h