yum-archive/TaSTT-Whisper

High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model

git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper

KonstantinSource codes8c4603c

master
1.9 KiB110 linesraw
1#pragma once
2
3namespace ComLight
4{
5	// COM smart pointer, very comparable to CComPtr from ATL
6	template <class I>
7	class CComPtr
8	{
9		I* p;
10
11		void callAddRef() const
12		{
13			if( nullptr == p )
14				return;
15			p->AddRef();
16		}
17
18	public:
19
20		// Construct with nullptr
21		CComPtr() : p( nullptr ) { }
22
23		// Release the pointer
24		void release()
25		{
26			if( nullptr == p )
27				return;
28			p->Release();
29			p = nullptr;
30		}
31
32		~CComPtr()
33		{
34			release();
35		}
36
37		// Attach without AddRef()
38		void attach( I* raw )
39		{
40			release();
41			p = raw;
42		}
43
44		// Detach without Release(), set this pointer to nullptr
45		I* detach()
46		{
47			I* const result = p;
48			p = nullptr;
49			return result;
50		}
51
52		// Detach without Release() and place to the specified address, set this pointer to nullptr
53		template<class Other>
54		void detach( Other** pp )
55		{
56			// If the argument points to a non-empty object, release the old instance: would leak memory otherwise.
57			if( nullptr != *pp )
58				( *pp )->Release();
59			( *pp ) = detach();
60		}
61
62		// Set and AddRef()
63		void assign( I* raw )
64		{
65			release();
66			attach( raw );
67			callAddRef();
68		}
69
70		void swap( CComPtr<I>& that )
71		{
72			std::swap( p, that.p );
73		}
74
75		// Set and AddRef()
76		CComPtr( I* raw ) : p( raw )
77		{
78			callAddRef();
79		}
80
81		// Set and AddRef()
82		CComPtr( const CComPtr<I>& that ) : CComPtr( that.p ) { }
83		// Move constructor
84		CComPtr( CComPtr<I>&& that ) : p( that.p ) { that.p = nullptr; }
85
86		// Set and AddRef()
87		void operator=( I* raw )
88		{
89			assign( raw );
90		}
91
92		// Set and AddRef()
93		void operator=( const CComPtr<I>& that )
94		{
95			assign( that.p );
96		}
97
98		// Move assignment operator, destroys the other one
99		void operator=( CComPtr<I>&& that )
100		{
101			attach( that.detach() );
102		}
103
104		operator I*( ) const { return p; }
105		I* operator -> () const { return p; }
106		I** operator &() { return &p; }
107
108		operator bool() const { return nullptr != p; }
109	};
110}