MemoryPool - SWTube/Darkest-Cave GitHub Wiki

cave::MemoryPool

  • defined in header "Memory/MemoryPool.h"

The class cave::MemoryPool provides support for memory allocation and management in the engine.

Type Definition
cave::MemoryPool cave::MemoryPool

Member functions

Name Description
(constructor) constructs a cave::MemoryPool
(public member function)
(destructor) destroys the memory pool, deallocating internal data blocks
(public member function)
  • Capacity
Name Description
GetFreeMemorySize returns the available memory size
(public member function)
GetCurrentStorage returns the size of memory being used
(public member function)
GetMaxNumDataBlocks returns the maximum number of data blocks
(public member function)
GetPoolSize returns the size of the pool
(public member function)
  • Operations
Name Description
Allocate returns a memory of given size
(public member function)
Deallocate retrieves the memory of given size
(public member function)

Constructor

MemoryPool(size_t maxPoolSize);

Constructs new memory pool according to the given pool size.

Paramters

Parameters Description
maxPoolSize maximum size of the pool

Complexity

Linear in smallest exponent of 2 that is larger than the pool size.

Example

#include "Memory/MemoryPool.h"

int main()
{    
    {
        cave::MemoryPool memoryPool(1024ul);
    }

	return 0;
}

Capacity

cave::MemoryPool::GetFreeMemorySize()

constexpr size_t GetFreeMemorySize() const;
  • Returns the size of free memory in pool

Parameters

(none)

Return value

Size of available memories in bytes

Complexity

Constant

Example

  • Samples/Main.cpp
#include "Memory/MemoryPool.h"

int main()
{
    cave::MemoryPool memoryPool(1024ul);
    assert(memoryPool.GetFreeMemorySize() == 1024ul);

    void* oneByte = memoryPool.Allocate(1);
    assert(memoryPool.GetFreeMemorySize() == 1023ul);

    return 0;
}

cave::MemoryPool::GetCurrentStorage()

size_t GetCurrentStorage() const;
  • Returns the size of memory being used

Parameters

(none)

Return value

Size of memory being used

Complexity

Linear to number of data blocks

Example

  • Samples/Main.cpp
#include "Memory/MemoryPool.h"

int main()
{
    cave::MemoryPool memoryPool(1024ul);
	assert(memoryPool.GetFreeMemorySize() == 1024ul);

    return 0;
}

Operations

cave::String::RemoveAt

bool RemoveAt(size_t index);

Removes specified characters from the string. Removes the character at index

Parameters

Parameters Description
index index to the character to remove

Return value

true if removal was successful, false if otherwise.

Example

#include "String/String.h"

int main()
{
	cave::String s = "This is an example";
	std::cout << s << '\n';

	s.RemoveAt(0);	// Remove 'T'
	std::cout << s << '\n';

	return 0;
}

Output:

This is an example
his is an example

cave::String::Append

void Append(const char* s);

Appends additional characters to the string. Appends the null-terminated character string pointed to by s. The length of the string is determined by the first null character using strlen(s).

Parameters

Parameters Description
s pointer to the character string to append

Return value

*this

Complexity

Linear in size of s

Example

#include "String/String.h

int main()
{
	cave::String str = "string";
	const char* cPtr = "C-string";
	const char cArr[] = "Two and one";

	cave::String output;

	// Append a whole C-string
	output.append(cPtr);
	std::cout << output << '\n';

	return 0;
}

Output:

C-string

cave::String::operator+=

String& operator+=(const String& str);	// (1)
String& operator+=(const char* s);		// (2)

Appends additional characters to the string.

  1. Appends string str
  2. Appends the null-terminated character string pointed to by s

Parameters

Parameters Description
str string to append
s pointer a null-terminated character string to append

Return value

*this

Complexity

  1. Linear in size of str
  2. Linear in size of s

Example

#include "String/String.h"

int main()
{
	cave::String str;
	std::cout << str << '\n';	// empty string

	str += "This";
   std::cout << str << '\n';
 
   str += cave::String(" is a");
   std::cout << str << '\n';
 
   str += {' ','s','t','r','i','n','g','.'};
   std::cout << str << '\n';
}

Output:


This
This is a
This is a string.

cave::String::operator+

String operator+(const String& rhs) const;

Returns a string containing characters from itself followed by the characters from rhs.

Parameters

Parameters Description
rhs string

Return value

A string containing the characters from itself followed by the characters from rhs.

Example

#include <iostream>

#include "String/String.h"
 
int main()
{
    cave::String s1 = "Hello";
    cave::String s2 = "world";
    std::cout << s1 + ' ' + s2 + "!\n";
}

Output:

Hello world!

cave::String::operator==

bool operator==(const String& rhs) const;

Compares the contents the string with another string.

  • Two strings are equal if both the size of itself and rhs are equal and each character of itself has equivalent character in rhs at the same position.

Compares itself with another String objects

Parameters

Parameters Description
rhs string whose contents to compare

Return value

true if the corresponding comparison holds, false otherwise.

Complexity

Linear in the size of the strings.

Search

cave::String::IndexOf

int32_t IndexOf(const char* s);

Finds the first substring equal to the given character sequence.

Finds the first substring equal to the character string pointed to by s. The length of the string is determined by the first null character using strlen(s)

Parameters

Parameters Description
s pointer to a character string to search for

Return value

Position of the first character of the found substring or NPOS if no such substring is found.

Example

#include <iostream>

#include "String/String.h"

void Print(size_t n, cave::String const &s)
{
    if (n == -1)
	{
        std::cout << "not found\n";
    }
	else
	{
        std::cout << "found" << &s.GetCString()[n] << '\n';
    }
}
 
int main()
{
    size_t n;
    cave::String const s = "This is a string";
 
    // search from beginning of string
    n = s.IndexOf("is");
    Print(n, s);
}

Output:

found: is is a string

cave::String::LastIndexOf

int32_t LastIndexOf(const char* s);

Finds the last substring equal to the given character sequence.

Finds the last substring equal to the character string pointed to by s. The length of the string is determined by the first null character using strlen(s).

Parameters

Parameters Description
s pointer to a character string to search for

Return value

Position of the first character of the found substring or NPOS if no such substring is found. Note that this is an offset from the string of the string, not the end.

If searching for an empty string(str.GetLength() is zero), NPOS is always returned.

Example

#include <iostream>

#include "String/String.h"

void Print(size_t n, cave::String const &s)
{
    if (n == -1)
	{
        std::cout << "not found\n";
    }
	else
	{
        std::cout << "found: \"" << &s.GetCString()[n] << "\" at " << n << '\n';
    }
}
 
int main()
{
    cave::size_t n;
    cave::String const s = "This is a string";
 
    // search backwards from end of string
    n = s.LastIndexOf("is");
    Print(n, s);
}

Output:

found: "is a string" at 5

Constants

cave::String::NPOS

static constexpr size_t NPOS = -1ul;

This is a special value equal to the maximum value representable by the type size_t. The exact meaning depends on context, but it is generally used either as end of string indicator by functions that expects a string index or as the error indicator by the functions that return a string index.

Note

Although the definition uses -1, size_t is an unsigned integer type, and the value of NPOS is the largest positive value it can hold, due to signed-to-unsigned implicit conversion. This is a portable way to specify the largest value of any unsigned type.

Example

#include <iostream>

#include "String/String.h"
 
int main()
{
    // string search functions return npos if nothing is found
    cave::String s = "test";
    if(s.IndexOf("a") == cave::String::NPOS)
	{
        std::cout << "no \"a\" in \"test\"\n";
	}
}

Output:

no "a" in "test"

Non-member functions

Input / Output

operator<<

std::ostream& operator<<(std::ostream& os, const cave::String& str);

Behaves as a FormattedOutputFunction. After constructing and checking the sentry object, determines the output format padding as follows:

  1. If str.GetLength() is not less than os.width(), uses the range [0, str.GetLength()) as-is
  2. Otherwise, if (os.flags() & ios_base::adjustfield) == ios_base::left, places os.width() - str.GetLength() copies of the os.fill() character after the character sequence.
  3. Otherwise, places os.width() - str.size() copies of the os.fill() character before the character sequence.

Then stored each character from the resulting sequence (the contents of str plus padding) to the output stream os as if by calling os.rdbuf()->sputn(seq, n), where n=std::max(os.width(), str.GetLength())

Finally, calls os.width(0) to cancel the effects of std::setw, if any.

Parameters

Parameters Description
os a character output stream
is a character input stream
str the string to be inserted

Return value

os

Example

#include <iostream>
#include <sstream>

#include "String/String.h"
 
int main()
{
    cave::String greeting = "Hello, whirled!";

    std::cout << greeting << '\n';
}

Output:

Hello, whirled!
⚠️ **GitHub.com Fallback** ⚠️