mirror of
https://github.com/WinampDesktop/winamp.git
synced 2025-06-18 14:05:46 -04:00
Initial community commit
This commit is contained in:
176
Src/nswasabi/AutoCharNX.h
Normal file
176
Src/nswasabi/AutoCharNX.h
Normal file
@ -0,0 +1,176 @@
|
||||
#pragma once
|
||||
#include "nx/nxstring.h"
|
||||
#include "nx/nxuri.h"
|
||||
#include "foundation/error.h"
|
||||
|
||||
template <nx_charset_t charset>
|
||||
class AutoCharNX
|
||||
{
|
||||
public:
|
||||
AutoCharNX()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
AutoCharNX(size_t bytes)
|
||||
{
|
||||
Init();
|
||||
ptr = (char *)malloc(bytes);
|
||||
malloc_size = bytes;
|
||||
}
|
||||
|
||||
AutoCharNX(nx_string_t string)
|
||||
{
|
||||
Init();
|
||||
|
||||
Set(string);
|
||||
}
|
||||
|
||||
AutoCharNX(nx_uri_t filename)
|
||||
{
|
||||
Init();
|
||||
|
||||
Set(filename);
|
||||
}
|
||||
|
||||
~AutoCharNX()
|
||||
{
|
||||
if (owned)
|
||||
free(ptr);
|
||||
if (reference_string)
|
||||
NXStringRelease(reference_string);
|
||||
}
|
||||
|
||||
int Set(nx_string_t string)
|
||||
{
|
||||
if (reference_string == string)
|
||||
return NErr_Success;
|
||||
|
||||
if (reference_string)
|
||||
NXStringRelease(reference_string);
|
||||
reference_string=0;
|
||||
|
||||
size_t byte_count=0;
|
||||
int ret = NXStringGetBytesSize(&byte_count, string, charset, nx_string_get_bytes_size_null_terminate);
|
||||
if(ret == NErr_DirectPointer)
|
||||
{
|
||||
if (owned)
|
||||
{
|
||||
free(ptr);
|
||||
ptr=0;
|
||||
length=0;
|
||||
malloc_size=0;
|
||||
}
|
||||
ret = NXStringGetBytesDirect((const void **)&ptr, &length, string, charset, nx_string_get_bytes_size_null_terminate);
|
||||
reference_string = NXStringRetain(string);
|
||||
owned=false;
|
||||
}
|
||||
else if (ret == NErr_Success)
|
||||
{
|
||||
if (owned)
|
||||
{
|
||||
if (byte_count > malloc_size)
|
||||
{
|
||||
ptr = (char *)realloc(ptr, byte_count);
|
||||
malloc_size = byte_count;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* not owned. need to allocate */
|
||||
ptr = (char *)malloc(byte_count);
|
||||
malloc_size = byte_count;
|
||||
owned=true;
|
||||
}
|
||||
|
||||
if (ptr)
|
||||
{
|
||||
ret = NXStringGetBytes(&length, string, ptr, byte_count, charset, nx_string_get_bytes_size_null_terminate);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NErr_OutOfMemory;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int Set(nx_uri_t filename)
|
||||
{
|
||||
int ret;
|
||||
nx_string_t string;
|
||||
ret = NXURIGetNXString(&string, filename);
|
||||
if (ret == NErr_Success)
|
||||
{
|
||||
ret = Set(string);
|
||||
NXStringRelease(string);
|
||||
}
|
||||
else
|
||||
{
|
||||
Clear();
|
||||
// failed! we need to clean up
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
operator const char *() const
|
||||
{
|
||||
if (length)
|
||||
return ptr;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* this one will never return a NULL, always a valid string */
|
||||
const char *GetValidString() const
|
||||
{
|
||||
if (length)
|
||||
return ptr;
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
/* the Clear function clears the string but doesn't deallocate memory */
|
||||
void Clear()
|
||||
{
|
||||
if (!owned)
|
||||
ptr=0;
|
||||
length=0;
|
||||
|
||||
if (reference_string)
|
||||
NXStringRelease(reference_string);
|
||||
reference_string=0;
|
||||
}
|
||||
|
||||
private:
|
||||
void Init()
|
||||
{
|
||||
ptr=0;
|
||||
length=0;
|
||||
owned=false;
|
||||
reference_string=0;
|
||||
malloc_size=0;
|
||||
}
|
||||
char *ptr;
|
||||
size_t length;
|
||||
size_t malloc_size;
|
||||
bool owned;
|
||||
nx_string_t reference_string;
|
||||
};
|
||||
|
||||
typedef AutoCharNX<nx_charset_utf8> AutoCharUTF8;
|
||||
#define AutoCharPrintfUTF8(x) (AutoCharUTF8(x).GetValidString())
|
||||
class AutoCharNative
|
||||
{
|
||||
public:
|
||||
};
|
||||
|
||||
class AutoFilename
|
||||
{
|
||||
public:
|
||||
};
|
||||
|
185
Src/nswasabi/ReferenceCounted.h
Normal file
185
Src/nswasabi/ReferenceCounted.h
Normal file
@ -0,0 +1,185 @@
|
||||
#pragma once
|
||||
#include "foundation/dispatch.h"
|
||||
#include "foundation/atomics.h"
|
||||
#include <new>
|
||||
#include "nx/nxstring.h"
|
||||
#include "nx/nxuri.h"
|
||||
|
||||
#define REFERENCE_COUNT_AS(x) size_t Retain() { return x::Retain(); } size_t Release() { return x::Release(); }
|
||||
template <class t>
|
||||
class ReferenceCounted : public t
|
||||
{
|
||||
public:
|
||||
ReferenceCounted() { reference_count = 1; }
|
||||
protected:
|
||||
/* Dispatchable implementation */
|
||||
|
||||
size_t WASABICALL Dispatchable_Retain()
|
||||
{
|
||||
return nx_atomic_inc(&reference_count);
|
||||
}
|
||||
|
||||
size_t WASABICALL Dispatchable_Release()
|
||||
{
|
||||
if (!reference_count)
|
||||
return reference_count;
|
||||
size_t r = nx_atomic_dec(&reference_count);
|
||||
if (!r)
|
||||
{
|
||||
#if defined(__ARM_ARCH_7A__)
|
||||
__asm__ __volatile__ ("dmb" : : : "memory");
|
||||
#endif
|
||||
delete(this);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
size_t reference_count;
|
||||
};
|
||||
|
||||
template <class t>
|
||||
class ReferenceCountedObject
|
||||
{
|
||||
public:
|
||||
ReferenceCountedObject()
|
||||
{
|
||||
ptr = new (std::nothrow) ReferenceCounted<t>;
|
||||
};
|
||||
|
||||
~ReferenceCountedObject()
|
||||
{
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
}
|
||||
|
||||
operator t *()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
t *operator ->()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
t *ptr;
|
||||
};
|
||||
|
||||
template <class t>
|
||||
class ReferenceCountedPointer
|
||||
{
|
||||
public:
|
||||
ReferenceCountedPointer()
|
||||
{
|
||||
ptr = 0;
|
||||
};
|
||||
|
||||
ReferenceCountedPointer(t *new_ptr)
|
||||
{
|
||||
ptr = new_ptr;
|
||||
};
|
||||
|
||||
~ReferenceCountedPointer()
|
||||
{
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
}
|
||||
|
||||
operator t *()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
t *operator ->()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
t **operator &()
|
||||
{
|
||||
// if there's something already in here, we need to release it first
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
ptr=0;
|
||||
return &ptr;
|
||||
}
|
||||
|
||||
t *operator =(t *new_ptr)
|
||||
{
|
||||
if (ptr)
|
||||
ptr->Release();
|
||||
ptr=0;
|
||||
ptr = new_ptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
t *ptr;
|
||||
};
|
||||
|
||||
class ReferenceCountedNXString
|
||||
{
|
||||
public:
|
||||
ReferenceCountedNXString()
|
||||
{
|
||||
ptr = 0;
|
||||
};
|
||||
|
||||
~ReferenceCountedNXString()
|
||||
{
|
||||
NXStringRelease(ptr);
|
||||
}
|
||||
|
||||
operator nx_string_t()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
nx_string_t *operator &()
|
||||
{
|
||||
// if there's something already in here, we need to release it first
|
||||
if (ptr)
|
||||
NXStringRelease(ptr);
|
||||
ptr=0;
|
||||
return &ptr;
|
||||
}
|
||||
|
||||
nx_string_t operator ->()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
nx_string_t ptr;
|
||||
};
|
||||
|
||||
class ReferenceCountedNXURI
|
||||
{
|
||||
public:
|
||||
ReferenceCountedNXURI()
|
||||
{
|
||||
ptr = 0;
|
||||
};
|
||||
|
||||
~ReferenceCountedNXURI()
|
||||
{
|
||||
NXURIRelease(ptr);
|
||||
}
|
||||
|
||||
operator nx_uri_t()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
nx_uri_t *operator &()
|
||||
{
|
||||
// if there's something already in here, we need to release it first
|
||||
if (ptr)
|
||||
NXURIRelease(ptr);
|
||||
ptr=0;
|
||||
return &ptr;
|
||||
}
|
||||
|
||||
nx_uri_t operator ->()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
nx_uri_t ptr;
|
||||
};
|
Reference in New Issue
Block a user