Utilities
This section lists utility classes and functions, which help to implement Circle applications.
CString
#include <circle/string.h>
-
class CString
This class encapsulates a character string and allows different manipulations on it. The methods of this class are not reentrant.
-
CString::CString(const char *pString)
Creates a string object. Sets the string initially to
pString
.
-
CString::CString(const CString &rString)
Copy constructor. Creates a new string object. Sets the string initially to the value of
rString
.rString
remains unchanged.
-
CString::CString(CString &&rrString)
Move constructor. Creates a new string object. Sets the string initially to the value of
rrString
.rrString
is set to an empty string.
-
CString::operator const char*(void) const
Returns a pointer to the string buffer, which is terminated with a zero-character.
-
const char *CString::operator=(const char *pString)
Assigns a new string. Returns a pointer to the string buffer, which is terminated with a zero-character.
-
CString &CString::operator=(const CString &rString)
Assigns a new string. Returns a reference to the string object.
-
CString &CString::operator=(CString &&rrString)
Move assignment. Assigns a new string.
rrString
is set to an empty string. Returns a reference to the string object.
-
size_t CString::GetLength(void) const
Returns the length of the string in number of characters (zero for empty string).
-
int CString::Compare(const char *pString) const
Compares
pString
with the string. Returns:zero, if the strings are identical
a negative value, if the string is smaller than
pString
a positive value, if the string is greater than
pString
-
int CString::Find(char chChar) const
Searches for
chChar
in the string. Returns the zero-based index of the character or -1, if it is not found.
-
int CString::Replace(const char *pOld, const char *pNew)
Replaces all occurrences of
pOld
withpNew
in the string. Returns the number of occurrences.
-
void CString::Format(const char *pFormat, ...)
Formats a string as known from
sprintf()
. Does support only a subset of the known format specifiers:%[#][[-][0]len][.prec][l|ll]{c|d|f|i|o|p|s|u|x|X}
Field
Description
#
insert prefix 0, 0x or 0X for %o, %x or %X
-
left justify output
0
insert leading zeros
len
decimal number specifying the length of the field
.prec
decimal number specifying the precision for %f
l
type is
long
ll
type is
long long
(with STDLIB_SUPPORT >= 1 only)c
insert
char
d
insert decimal
int
,long
orlong long
(maybe with sign)f
insert
double
i
same as %d
o
insert octal
unsigned
,unsigned long
orunsigned long long
p
same as %x
s
insert string (type is
const char *
)u
insert decimal
unsigned
,unsigned long
orunsigned long long
x
insert hex
unsigned
,unsigned long
orunsigned long long
(lower case)X
insert hex
unsigned
,unsigned long
orunsigned long long
(upper case)
CPtrArray
#include <circle/ptrarray.h>
-
class CPtrArray
This class implements a dynamic array of pointers. The methods of this class are not reentrant.
-
CPtrArray::CPtrArray(unsigned nInitialSize = 100, unsigned nSizeIncrement = 100)
Creates a
CPtrArray
object with initially space fornInitialSize
elements. The memory allocation will be increased bynSizeIncrement
elements, when the array is full.
-
void *CPtrArray::operator[](unsigned nIndex) const
Returns the pointer for the array element at
nIndex
(based on zero).nIndex
must be smaller than the value returned fromGetCount()
.
CPtrList
#include <circle/ptrlist.h>
-
class CPtrList
This class implements a linked list of pointers. The methods of this class are not reentrant.
-
type TPtrListElement
Opaque type definition.
-
TPtrListElement *CPtrList::GetNext(TPtrListElement *pElement)
Returns the next element following
pElement
, or 0 if nothing follows.
-
void CPtrList::InsertBefore(TPtrListElement *pAfter, void *pPtr)
Inserts
pPtr
before the elementpAfter
, which must not be 0.
CPtrListFIQ
#include <circle/ptrlistfiq.h>
-
CPtrListFIQ::CPtrListFIQ(unsigned nMaxElements)
Creates a pointer list with up to
nMaxElements
elements.
CNumberPool
#include <circle/numberpool.h>
-
class CNumberPool
This class implements an allocation pool for numbers. The methods of this class are not reentrant.
-
static const unsigned Limit = 63
Allowed maximum of an allocated number.
-
CNumberPool::CNumberPool(unsigned nMin, unsigned nMax = Limit)
Creates a number pool.
nMin
is the minimal returned number byAllocateNumber()
.nMax
is the maximal returned number.
-
unsigned CNumberPool::AllocateNumber(boolean bMustSucceed, const char *pFrom = "numpool")
Allocates a number from the number pool and returns it. If there are no more numbers available, this method returns
CNumberPool::Invalid
, ifbMustSucceed
isFALSE
, or the system halts with a panic message otherwise. This message has the prefixpFrom
.
-
void CNumberPool::FreeNumber(unsigned nNumber)
Returns
nNumber
, which has been allocated earlier, to the number pool for reuse.
Atomic memory access
#include <circle/atomic.h>
This header file defines some functions, which implement an atomic access to an aligned int
variable in memory. These functions can be useful for synchronization purposes, especially for multi-core applications, where using a spin lock would be too time consuming. All accesses to such a variable must use one of the following functions, to ensure them being atomic.
-
int AtomicGet(volatile const int *pVar)
Returns the value of the
int
variable atpVar
.
-
int AtomicSet(volatile int *pVar, int nValue)
Sets the
int
variable atpVar
tonValue
and returnsnValue
.
-
int AtomicExchange(volatile int *pVar, int nValue)
Sets the
int
variable atpVar
tonValue
and returns the previous value.
-
int AtomicCompareExchange(volatile int *pVar, int nCompare, int nValue)
Sets the
int
variable atpVar
tonValue
, if the previous value of the variable wasnCompare
, and returns the previous value of the variable.
-
int AtomicAdd(volatile int *pVar, int nValue)
Adds
nValue
to theint
variable atpVar
. Returns the result of the operation.
-
int AtomicSub(volatile int *pVar, int nValue)
Subtracts
nValue
from theint
variable atpVar
. Returns the result of the operation.
-
int AtomicIncrement(volatile int *pVar)
Increments the
int
variable atpVar
by 1. Returns the result of the operation.
-
int AtomicDecrement(volatile int *pVar)
Decrements the
int
variable atpVar
by 1. Returns the result of the operation.
C standard library functions
#include <circle/util.h>
This header file defines some functions, known from the C standard library.
Memory functions
-
void *memset(void *pBuffer, int nValue, size_t nLength)
-
void *memcpy(void *pDest, const void *pSrc, size_t nLength)
-
void *memmove(void *pDest, const void *pSrc, size_t nLength)
-
int memcmp(const void *pBuffer1, const void *pBuffer2, size_t nLength)
String functions
-
size_t strlen(const char *pString)
-
int strcmp(const char *pString1, const char *pString2)
-
int strcasecmp(const char *pString1, const char *pString2)
-
int strncmp(const char *pString1, const char *pString2, size_t nMaxLen)
-
int strncasecmp(const char *pString1, const char *pString2, size_t nMaxLen)
-
char *strcpy(char *pDest, const char *pSrc)
-
char *strncpy(char *pDest, const char *pSrc, size_t nMaxLen)
-
char *strcat(char *pDest, const char *pSrc)
-
char *strchr(const char *pString, int chChar)
-
char *strstr(const char *pString, const char *pNeedle)
-
char *strtok_r(char *pString, const char *pDelim, char **ppSavePtr)
Number conversion
-
unsigned long strtoul(const char *pString, char **ppEndPtr, int nBase)
-
unsigned long long strtoull(const char *pString, char **ppEndPtr, int nBase)
-
int atoi(const char *pString)
Other functions
#include <circle/util.h>
-
u16 bswap16(u16 usValue)
-
u32 bswap32(u32 ulValue)
Swaps the byte order of a 16- or 32-bit value.
-
int parity32(unsigned nValue)
Returns the number of 1-bits in
nValue
modulo 1.
Macros
#include <circle/macros.h>
-
PACKED
Packs a
struct
definition. The members will be stored tightly, not aligned as usual.
-
ALIGN(n)
Aligns a variable or member to a boundary of
n
in memory.
-
NORETURN
Append this to the prototype of a function, which never returns.
-
BIT(n)
Returns the bit mask
(1U << (n))
.
-
likely(exp)
-
unlikely(exp)
In time critical code this gives the compiler a hint, which result of the boolean expression
exp
is normally expected. This can result in faster code.