first commit

This commit is contained in:
2026-06-22 11:49:35 +02:00
commit d805f2ba86
619 changed files with 126873 additions and 0 deletions

View File

@ -0,0 +1,60 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NBITFIELD__
#define __NBITFIELD__
#include "ntypes.h"
namespace GS {
class BitField
{
uint v;
public:
BitField &operator &= (uint b) { v &= b; return *this; }
BitField &operator |= (uint b) { v |= b; return *this; }
BitField &operator = (uint b) { v = b; return *this; }
BitField &operator &= (const BitField &b) { v &= b.v; return *this; }
BitField &operator |= (const BitField &b) { v |= b.v; return *this; }
BitField &operator = (const BitField &b) { v = b.v; return *this; }
bool operator == (const BitField &b) const { return v == b.v; }
bool operator != (const BitField &b) const { return v != b.v; }
inline uint Get() const
{
return v;
}
inline uint Set(uint b)
{
v |= b;
return v;
}
inline uint Remove(uint b)
{
v &= ~b;
return v;
}
inline uint Raise(uint b, bool raise = true)
{
return raise ? Set(b) : Remove(b);
}
inline bool IsSet(uint b) const { return asbool(v & b); }
BitField() : v(0) {}
};
}
#endif // __NBITFIELD__

View File

@ -0,0 +1,52 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NENDIAN__
#define __NENDIAN__
#include "ntypes.h"
namespace GS {
namespace Endian {
enum Config
{
Undefined = 0,
Big,
Little,
Motorola = Big,
Intel = Little
};
/// Swap bytes in memory.
void SwapBytes(void *, size_t);
/// Return the current host memory configuration.
Config GetHostConfiguration();
/// Convert a memory block to the host configuration.
void *ToHost(void *, size_t, Config = Big);
/// Convert a value to the host configuration.
template <class T> T ToHost(const T &v, Config cfg = Big)
{
if (GetHostConfiguration() == cfg)
return v;
T host_value = v;
SwapBytes(&host_value, sizeof(T));
return host_value;
}
} // Endian
} // GS
#endif //__NENDIAN__

View File

@ -0,0 +1,36 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NMEMORY__
#define __NMEMORY__
#include "ntypes.h"
namespace GS {
namespace Memory {
/// Return smallest number of bits that can be used to represent a value.
uchar GetBitCount(int);
/// Return the position in bit of the first non-zero bit.
uchar GetShiftCount(int);
/// Return the number of bit set in a given value.
uchar CountSetBit(int);
void WriteBit(uchar *, uint offset_in_bit, uint bit_count, uint value);
uint ReadBit(uchar *, uint offset_in_bit, uint bit_count);
bool Compare(const void *, const void *, size_t);
void Copy(void *, const void *, size_t);
void Set(void *, char, size_t);
void Fill(void *, const char *pattern, size_t);
} // Memory
} // GS
#endif // __NMEMORY__

View File

@ -0,0 +1,78 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NAUTO_PTR__
#define __NAUTO_PTR__
#include "alloc/ialloc.h"
#include "billing/billing.h"
namespace GS {
/*!
@short Auto pointer.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
template <class T> class AutoPtr
{
private:
T *o;
public:
inline operator T*() const
{ return o; }
inline T *c_ptr() const
{ return o; }
inline bool IsNull() const { return o ? false : true; }
inline bool IsValid() const { return o ? true : false; }
T *operator = (T *p)
{
if (o != p)
{
delete(o);
o = p;
}
return o;
}
inline T &operator[] (size_t n) { return o[n]; }
inline bool operator == (const T *p) const
{ return o == p; }
inline bool operator != (const T *p) const
{ return o != p; }
inline T *operator -> () const
{ return o; }
inline T &operator * () const
{ return *o; }
inline T *Detach()
{
T *p = o;
o = 0;
return p;
}
explicit AutoPtr(T *p = 0) : o(p)
{}
~AutoPtr()
{
delete(o);
o = 0;
}
};
} // GS
#endif // __NAUTO_PTR__

View File

@ -0,0 +1,150 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NSHARED_PTR__
#define __NSHARED_PTR__
#include "thread/atomic_value.h"
namespace GS {
/// Weak reference to a shared object.
class WeakRef
{
friend class SharedObject;
bool is_valid;
Threading::Atomic32 refc;
WeakRef() : is_valid(true) {}
public:
bool IsValid() const { return is_valid; }
int GetRefCount() const { return refc.Get(); }
void AddRef() { refc.Inc(); }
void RemoveRef()
{
if ((refc.Dec() == 0) && !is_valid)
delete this;
}
};
/*!
@short Reference counted object.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class SharedObject
{
Threading::Atomic32 refc;
WeakRef *weak_ref;
protected:
virtual ~SharedObject()
{
if (weak_ref)
{
if (weak_ref->refc.Get() == 0)
delete weak_ref;
else
weak_ref->is_valid = false;
}
}
public:
WeakRef *GetWeakRef()
{
if (!weak_ref)
weak_ref = new WeakRef; // TODO have a specialized, fixed pool allocator for these.
return weak_ref;
}
int GetRefCount() const { return refc.Get(); }
void AddRef() { refc.Inc(); }
void RemoveRef()
{
if (refc.Dec() == 0)
{
if (weak_ref)
weak_ref->is_valid = false;
delete this;
}
}
SharedObject() : weak_ref(0) {}
};
/*!
@short Strong smart pointer to a reference counted object.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
template <class T> class SharedPtr
{
T *rc_o;
public:
inline operator T *() const { return rc_o; }
inline T *c_ptr() const { return rc_o; }
inline bool IsNull() const { return rc_o ? false : true; }
inline bool IsValid() const { return rc_o ? true : false; }
void Init()
{
if (rc_o)
rc_o->AddRef();
}
inline bool operator == (const T *p) const { return rc_o == p; }
inline bool operator != (const T *p) const { return rc_o != p; }
inline bool operator == (T *p) const { return rc_o == p; }
inline bool operator != (T *p) const { return rc_o != p; }
inline bool operator == (const SharedPtr &p) const { return rc_o == p.rc_o; }
inline bool operator != (const SharedPtr &p) const { return rc_o != p.rc_o; }
T *operator = (T *p)
{
if (rc_o != p)
{
if (rc_o)
rc_o->RemoveRef();
rc_o = p;
Init();
}
return p;
}
SharedPtr <T> &operator = (const SharedPtr &p)
{
*this = p.c_ptr();
return *this;
}
inline T *operator -> () const { return rc_o; }
inline T &operator * () const { return *rc_o; }
explicit SharedPtr(T *p) : rc_o(p) { Init(); }
SharedPtr(const SharedPtr &p) : rc_o(p.rc_o) { Init(); }
SharedPtr() : rc_o(0) {}
~SharedPtr()
{
if (rc_o)
rc_o->RemoveRef();
}
};
} // GS
#endif // __NSHARED_PTR__

View File

@ -0,0 +1,101 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NWEAK_PTR__
#define __NWEAK_PTR__
#include "memory/nshared_ptr.h"
namespace GS {
/*!
@short Weak pointer to a reference counted object.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
template <class T> class WeakPtr
{
private:
T *rc_o;
WeakRef *wref;
public:
T *c_ptr() const { return wref && wref->IsValid() ? rc_o : 0; }
bool IsValid() const { return wref && wref->IsValid() ? true : false; }
bool IsNull() const { return !IsValid(); }
/// Lock pointed object to a strong pointer.
bool Lock(SharedPtr <T> &p)
{
if (IsNull())
return false;
p = rc_o;
return true;
}
SharedPtr <T> Lock() const { return SharedPtr <T> (IsValid() ? rc_o : 0); }
void Init()
{
if (!rc_o)
wref = 0;
else
{
wref = rc_o->GetWeakRef();
wref->AddRef();
}
}
inline bool operator == (const T *p) const
{ return rc_o == p; }
inline bool operator != (const T *p) const
{ return rc_o != p; }
inline bool operator == (T *p) const
{ return rc_o == p; }
inline bool operator != (T *p) const
{ return rc_o != p; }
inline bool operator == (const WeakPtr &p) const
{ return rc_o == p.rc_o; }
inline bool operator != (const WeakPtr &p) const
{ return rc_o != p.rc_o; }
WeakPtr <T> &operator = (const WeakPtr &p)
{
if (rc_o != p.rc_o)
{
WeakRef *old_wref = wref;
rc_o = p.rc_o;
Init();
if (old_wref)
old_wref->RemoveRef();
}
return *this;
}
inline T *operator -> () const
{ return rc_o; }
inline T &operator * () const
{ return *rc_o; }
WeakPtr(T *p = 0) : rc_o(p) { Init(); }
WeakPtr(const WeakPtr &p) : rc_o(p.rc_o) { Init(); }
WeakPtr(const SharedPtr <T> &p) : rc_o(p.c_ptr()) { Init(); }
~WeakPtr()
{
if (wref)
wref->RemoveRef();
}
};
} // GS
#endif // __NWEAK_PTR__

View File

@ -0,0 +1,80 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NRINGBUFFER__
#define __NRINGBUFFER__
#include "container/narray.h"
namespace GS {
/*
@short Ring buffer.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
template <class T> class RingBuffer
{
Array <T> buffer;
uint put, get, usage;
public:
/// Get the maximum number of element in buffer.
uint GetCount() const
{ return buffer.GetCount(); }
/// Number of T left to put in the buffer.
uint GetFree() const
{ return buffer.GetCount() - usage; }
/// Number of T available to get from the buffer.
uint GetUsage() const
{ return usage; }
/// Increment the put pointer, returns true when successful.
bool Produce()
{
if (usage == buffer.GetCount())
return false;
put = (put + 1) % buffer.GetCount();
usage++;
return true;
}
/// Increment the get pointer, returns true when successful.
bool Consume()
{
if (usage == 0)
return false;
get = (get + 1) % buffer.GetCount();
usage--;
return true;
}
/// Retrieve the object at the current get pointer.
T &CurrentGet()
{ return buffer[get]; }
/// Retrieve the object at the current put pointer.
T &CurrentPut()
{ return buffer[put]; }
bool Allocate(uint count)
{
put = get = usage = 0;
return buffer.Allocate(count);
}
RingBuffer() : put(0), get(0), usage(0) {}
};
} // GS
#endif // __NRINGBUFFER__

View File

@ -0,0 +1,28 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __SINGLETON__
#define __SINGLETON__
namespace GS {
template <class T> class Singleton
{
static T *i;
public:
static T &Get() { return *i; }
static void Set(T *s) { i = s; }
static void Free() { delete i; i = nullptr; }
};
}
#endif // __SINGLETON__