/****************************************************************************** libx42.net - skinned vertex animation library (.NET API) Copyright (C) 2007 HermitWorks Entertainment Corporation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA. ******************************************************************************/ #pragma once; #include "BasicCollection.h" namespace Libx42 { ref class Model; generic< typename T > where T : value class public ref class DataStream : BasicCollection< T > { public: property int Count { virtual int get( void ) override sealed { return count; } } property T default[int] { virtual T get( int index ) override sealed { if( index < 0 || index >= count ) throw gcnew ArgumentOutOfRangeException( "index" ); T ret; memcpy( &ret, (const Byte*)pBase + stride * index, sizeof( T ) ); return ret; } virtual void set( int index, T value ) override sealed { if( !writable ) throw gcnew InvalidOperationException( "This DataStream is not writable." ); if( index < 0 || index >= count ) throw gcnew ArgumentOutOfRangeException( "index" ); memcpy( (Byte*)pBase + stride * index, &value, sizeof( T ) ); } } virtual void CopyTo( array< T >^ array, int arrayIndex ) override sealed { if( !array ) throw gcnew ArgumentNullException( "array" ); if( arrayIndex < 0 || arrayIndex + count > array->Length ) throw gcnew ArgumentOutOfRangeException( "arrayIndex" ); pin_ptr< T > pinArray = &array[arrayIndex]; memcpy( pinArray, (const Byte*)pBase, sizeof( T ) * count ); } virtual array< T >^ ToArray( void ) override sealed { array< T > ^ret = gcnew array< T >( count ); CopyTo( ret, 0 ); return ret; } IntPtr GetPointerToElement( int index ) { if( index < 0 || index >= count ) throw gcnew ArgumentOutOfRangeException( "index" ); return IntPtr( (Byte*)pBase + stride * index ); } void CopyToBuffer( int index, int count, IntPtr dest, int stride, int padBytes ); void CopyToBuffer( int index, int count, IntPtr dest, int stride ) { CopyToBuffer( index, count, dest, stride, 0 ); } void CopyToBuffer( int index, int count, IntPtr dest ) { CopyToBuffer( index, count, dest, 12, 0 ); } generic< typename O > where O : value class DataStream< O >^ Reinterpret( int baseIndex, int count ) { if( baseIndex < 0 || count < 0 || baseIndex + count > this->count ) throw gcnew ArgumentOutOfRangeException(); if( sizeof( O ) > sizeof( T ) ) throw gcnew ArgumentException(); return gcnew DataStream< O >( owner, (Byte*)pBase + stride * baseIndex, stride, padBytes, count ); } generic< typename O > where O : value class DataStream< O >^ Reinterpret( void ) { return Reinterpret< O >( 0, count ); } internal: void *pBase; DataStream( Model ^owner, void *pBase, size_t stride, size_t padBytes, int count ) : owner( owner ), pBase( pBase ), stride( stride ), padBytes( padBytes ), count( count ), writable( false ) { } DataStream( Model ^owner, void *pBase, size_t stride, size_t padBytes, int count, bool writable ) : owner( owner ), pBase( pBase ), stride( stride ), padBytes( padBytes ), count( count ), writable( writable ) { } protected: Model ^owner; size_t stride; size_t padBytes; int count; bool writable; }; }