Design Pattern Examples
Overview of object-oriented design patterns
Helpers Namespace Reference

The namespace containing all the "helper" functions in the C++ code. More...

Classes

class  argumentinvalid_error
 Exception for arguments that are invalid. More...
 
class  argumentnull_error
 Exception for arguments that are null. More...
 
class  DateTime
 Represents a timestamp composed of a date and a time encoded in a time_t value. Provides ways of getting the current time and to format the time in a string. More...
 

Functions

bool checkforkey ()
 Determine if a key has been pressed.
 
void disableinputecho ()
 Disable echoing input until enableinputecho() is called.
 
void enableinputecho ()
 Enable echoing input, which should be the default mode. Call this only after calling disableinputecho();.
 
void setcursorposition (int row, int column)
 Move the text cursor to the specified screen coordinates.
 
void getcursorposition (int *row, int *column)
 Retrieve the current cursor position in the console window.
 
void enableVTMode ()
 On Windows, enable the virtual terminal processing mode on the Console's output handle. On all other operating systems, this function does nothing.
 
std::string formatstring (const char *fmt,...)
 Use the given string and arguments to return a buffer containing the formatted string. The format and arguments follow the printf() functionality.
 
struct tm * makelocaltime (const time_t *time, struct tm *timestruct)
 Convert the given time to local time.
 
int readkey ()
 Read a key from the keyboard, blocking if no key is pressed.
 
std::string Replace (const std::string &s, const char *str1, const char *str2, bool bCaseInsensitive=false)
 Replace all occurrences of narrow string str1 with narrow string str2 in s. If str2 is empty then all matches to str1 are effectively removed from the string.
 
std::string Replace (const std::string &s, char c1, char c2, bool bCaseInsensitive=false)
 Replace all occurrences of narrow character c1 with narrow character c2 in s. If c2 is empty then all matches to str1 are effectively removed from the string.
 
std::wstring Replace (const std::wstring &s, wchar_t c1, wchar_t c2, bool bCaseInsensitive=false)
 Replace all occurrences of wide character c1 with wide character c2 in s. If c2 is empty then all matches to str1 are effectively removed from the string.
 
std::wstring Replace (const std::wstring &s, const wchar_t *str1, const wchar_t *str2, bool bCaseInsensitive=false)
 Replace all occurrences of wide string str1 with wide string str2 in s. If str2 is empty then all matches to str1 are effectively removed from the string.
 
void sleep (int milliseconds)
 Sleep for the specified number of milliseconds. Does not return until after the sleep period.
 
std::vector< std::string > split (const char *pszString, const char *splitChars=" ")
 Split the given string into a list of strings given the character on which to split. If the split character does not appear, return the original string as the first entry in the list.
 
std::vector< std::string > split (const std::string &szString, const std::string &splitChars=" ")
 Split the given string into a list of strings given the character on which to split. If the split character does not appear, return the original string as the first entry in the list.
 
std::vector< std::wstring > split (const wchar_t *pszString, const wchar_t *splitChars=L" ")
 Split the given string into a list of strings given the character on which to split. If the split character does not appear, return the original string as the first entry in the list.
 
std::vector< std::wstring > split (const std::wstring &szString, const std::wstring &splitChars=L" ")
 Split the given string into a list of strings given the character on which to split. If the split character does not appear, return the original string as the first entry in the list.
 
int stricmp (const char *first, const char *second)
 Compare two strings in a case-insensitive manner to determine their alphabetical order relative to each other.
 
int stricmp (const std::string &first, const std::string &second)
 Compare two strings in a case-insensitive manner to determine their alphabetical order relative to each other.
 
char * strstri (const char *s1, const char *s2)
 Do case-insensitive search for string 2 (s2) in string 1 (s1). Similar to the C library's strstr().
 
std::string titlecase (const std::string &s)
 Convert the first word (or only word) in the given string to lowercase then make the first letter uppercase. It does not touch any of the other words after the first.
 
std::string uint32_to_binary (uint32_t number)
 Function to convert a 32-bit unsigned integer into a string representation containing all 32 bits.
 
wchar_t * wcsstri (const wchar_t *s1, const wchar_t *s2)
 Do case-insensitive search for string 2 (s2) in string 1 (s1). Similar to the C library's wcsstr().
 

Detailed Description

The namespace containing all the "helper" functions in the C++ code.

Function Documentation

◆ checkforkey()

bool checkforkey ( )

Determine if a key has been pressed.

Determine if a key has been pressed on the keyboard.

Returns
Returns true if a key has been pressed; otherwise, returns false. Call readkey() to get the actual key.
Returns
Returns true if a key has been pressed; otherwise, returns false.

Definition at line 46 of file checkforkey.cpp.

References kbhit().

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise().

◆ disableinputecho()

void disableinputecho ( )

Disable echoing input until enableinputecho() is called.

Definition at line 118 of file cursor.cpp.

References _disableInputEcho(), and inputEchoDisabled.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise().

◆ enableinputecho()

void enableinputecho ( )

Enable echoing input, which should be the default mode. Call this only after calling disableinputecho();.

Definition at line 129 of file cursor.cpp.

References _enableInputEcho(), and inputEchoDisabled.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise().

◆ enableVTMode()

void enableVTMode ( )

On Windows, enable the virtual terminal processing mode on the Console's output handle. On all other operating systems, this function does nothing.

Definition at line 85 of file enablevtmode.cpp.

References enableVTMode().

Referenced by enableVTMode(), and main().

◆ formatstring()

std::string formatstring ( const char *  fmt,
  ... 
)

Use the given string and arguments to return a buffer containing the formatted string. The format and arguments follow the printf() functionality.

Parameters
fmtString containing formatting codes as used with the printf() function.
Returns
Returns a newly allocated buffer containing the zero-terminated formatted string. Use free() to delete the buffer. Returns NULL if no memory could be allocated.

Definition at line 17 of file formatstring.cpp.

Referenced by MessageWindow::_HandleButtonDownMessage(), MessageWindow::_HandleButtonUpMessage(), MessageWindow::_HandleCloseMessage(), Interpreter_Class::_InterpretToken(), UserGroupMediator::AddUserToGroup(), DataReaderWriter::BufferToString(), DesignPatternExamples_cpp::Command_Exercise(), Strategy_SortEntries_ClassFactory::Create(), DesignPatternExamples_cpp::Decorator_Exercise(), Real_Class::DoWork(), DesignPatternExamples_cpp::Flyweight_Exercise(), DesignPatternExamples_cpp::LoggerHelpers::FormatLogLine(), Composite_FileAccess::GetEntry(), UserGroupMediator::GetGroupsWithUser(), UserGroupMediator::GetUsersInGroup(), DesignPatternExamples_cpp::Interpreter_Exercise(), UserGroupMediator::IsUserInGroup(), DesignPatternExamples_cpp::Iterator_Exercise(), DesignPatternExamples_cpp::Mediator_Exercise(), DesignPatternExamples_cpp::Memento_Exercise(), DesignPatternExamples_cpp::NullObject_Exercise(), ObserverForDecimal::NumberChanged(), ObserverForHexaDecimal::NumberChanged(), ObserverForBinary::NumberChanged(), DesignPatternExamples_cpp::Observer_Exercise(), Visitor_Shop::PickupOrder(), Visitor_Shop::PlaceOrder(), DesignPatternExamples_cpp::Proxy_Exercise(), UserGroupMediator::RemoveUserFromAllGroups(), UserGroupMediator::RemoveUserFromGroup(), WhiteBackgroundDecorator::Render(), UnderlineDecorator::Render(), RedForegroundDecorator::Render(), MoveCommand::Show(), Strategy_ShowEntries_Class::ShowEntries(), Command::ToString(), HandlerChain::ToString(), MessagePosition::ToString(), Message::ToString(), WindowRectangle::ToString(), MessageWindow::ToString(), EntryInformation::ToString(), and DesignPatternExamples_cpp::Visitor_Exercise().

◆ getcursorposition()

void getcursorposition ( int *  row,
int *  column 
)

Retrieve the current cursor position in the console window.

Parameters
rowReturns the row index from the top, starting at 0.
columnReturns the column index from the left, starting at 0.

Definition at line 149 of file cursor.cpp.

References _countof, _disableInputEcho(), _enableInputEcho(), inputEchoDisabled, and split().

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise().

◆ makelocaltime()

struct tm * makelocaltime ( const time_t *  time,
struct tm *  timestruct 
)

Convert the given time to local time.

Hides the differences between multiple C++ library implementations.

Parameters
timePointer to the time_t object representing the time to convert.
timestructPointer to a struct tm in which to put the results of converting to local time.
Returns
Returns the pointer to the struct tm if all is well; otherwise, returns nullptr, indicating an error (runtime constraint violation or a failure to convert the specified time to local calendar time).

This is an example of a function acting as an adapter. The rest of the program just wants to convert a time stamp to local time and doesn't want to know about the differences between implementations of the C++ library.

Definition at line 17 of file makelocaltime.cpp.

Referenced by DateTime::ToString().

◆ readkey()

int readkey ( )

Read a key from the keyboard, blocking if no key is pressed.

Returns
Returns the ASCII for the key pressed.

Definition at line 17 of file readkey.cpp.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise().

◆ Replace() [1/4]

std::string Replace ( const std::string &  s,
char  c1,
char  c2,
bool  bCaseInsensitive = false 
)

Replace all occurrences of narrow character c1 with narrow character c2 in s. If c2 is empty then all matches to str1 are effectively removed from the string.

Parameters
sString to search
c1Character to replace
c2Character to replace with
bCaseInsensitivetrue if to perform case-insensitive search; otherwise, use case-sensitive search
Returns
Returns the updated string

Definition at line 140 of file replace.cpp.

References Replace().

◆ Replace() [2/4]

std::string Replace ( const std::string &  s,
const char *  str1,
const char *  str2,
bool  bCaseInsensitive = false 
)

Replace all occurrences of narrow string str1 with narrow string str2 in s. If str2 is empty then all matches to str1 are effectively removed from the string.

Parameters
sString to search
str1String to replace
str2String to replace with
bCaseInsensitivetrue if to perform case-insensitive search; otherwise, use case-sensitive search
Returns
Returns the updated string

Definition at line 47 of file replace.cpp.

References strstri().

Referenced by Composite_FileAccess::GetEntry(), Program::Help(), and Replace().

◆ Replace() [3/4]

std::wstring Replace ( const std::wstring &  s,
const wchar_t *  str1,
const wchar_t *  str2,
bool  bCaseInsensitive = false 
)

Replace all occurrences of wide string str1 with wide string str2 in s. If str2 is empty then all matches to str1 are effectively removed from the string.

Parameters
sString to search
str1String to replace
str2String to replace with
bCaseInsensitivetrue if to perform case-insensitive search; otherwise, use case-sensitive search
Returns
Returns the updated string

Definition at line 45 of file replacew.cpp.

References wcsstri().

◆ Replace() [4/4]

std::wstring Replace ( const std::wstring &  s,
wchar_t  c1,
wchar_t  c2,
bool  bCaseInsensitive = false 
)

Replace all occurrences of wide character c1 with wide character c2 in s. If c2 is empty then all matches to str1 are effectively removed from the string.

Parameters
sString to search
c1Character to replace
c2Character to replace with
bCaseInsensitivetrue if to perform case-insensitive search; otherwise, use case-sensitive search
Returns
Returns the updated string

Definition at line 135 of file replacew.cpp.

References Replace().

◆ setcursorposition()

void setcursorposition ( int  row,
int  column 
)

Move the text cursor to the specified screen coordinates.

Parameters
rowRow index from top, starting at 0.
columnColumn index from left, starting at 0

Definition at line 140 of file cursor.cpp.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise().

◆ sleep()

void sleep ( int  milliseconds)

Sleep for the specified number of milliseconds. Does not return until after the sleep period.

Parameters
millisecondsNumber of milliseconds to sleep.

Definition at line 15 of file sleep.cpp.

Referenced by DesignPatternExamples_cpp::Flyweight_Exercise().

◆ split() [1/4]

std::vector< std::string > split ( const char *  pszString,
const char *  splitChars = " " 
)

Split the given string into a list of strings given the character on which to split. If the split character does not appear, return the original string as the first entry in the list.

Parameters
pszStringThe string to split.
splitCharsThe character or characters to split on. This parameter is treated as a list of characters on which to split the string.
Returns
A list of 1 or more strings resulting from the split.

Definition at line 13 of file split.cpp.

Referenced by Composite_FileAccess::_FindEntry(), getcursorposition(), and split().

◆ split() [2/4]

std::vector< std::string > split ( const std::string &  szString,
const std::string &  splitChars = " " 
)

Split the given string into a list of strings given the character on which to split. If the split character does not appear, return the original string as the first entry in the list.

Parameters
szStringThe string to split.
splitCharsThe character or characters to split on. This parameter is treated as a list of characters on which to split the string.
Returns
A list of 1 or more strings resulting from the split.

Definition at line 39 of file split.cpp.

References split().

◆ split() [3/4]

std::vector< std::wstring > split ( const std::wstring &  szString,
const std::wstring &  splitChars = L" " 
)

Split the given string into a list of strings given the character on which to split. If the split character does not appear, return the original string as the first entry in the list.

Parameters
szStringThe string to split.
splitCharsThe character or characters to split on. This parameter is treated as a list of characters on which to split the string.
Returns
A list of 1 or more strings resulting from the split.

Definition at line 38 of file splitw.cpp.

References split().

◆ split() [4/4]

std::vector< std::wstring > split ( const wchar_t *  pszString,
const wchar_t *  splitChars = L" " 
)

Split the given string into a list of strings given the character on which to split. If the split character does not appear, return the original string as the first entry in the list.

Parameters
pszStringThe string to split.
splitCharsThe character or characters to split on. This parameter is treated as a list of characters on which to split the string.
Returns
A list of 1 or more strings resulting from the split.

Definition at line 12 of file splitw.cpp.

◆ stricmp() [1/2]

int stricmp ( const char *  first,
const char *  second 
)

Compare two strings in a case-insensitive manner to determine their alphabetical order relative to each other.

Parameters
firstFirst string compare.
secondSecond string to compare
Returns
Returns < 0 if first comes before second; 0 if first equals second; and > 0 if first comes after second.

Definition at line 16 of file stricmp.cpp.

Referenced by GroupList::GroupNames(), and stricmp().

◆ stricmp() [2/2]

int stricmp ( const std::string &  first,
const std::string &  second 
)

Compare two strings in a case-insensitive manner to determine their alphabetical order relative to each other.

Parameters
firstFirst string compare.
secondSecond string to compare
Returns
Returns < 0 if first comes before second; 0 if first equals second; and > 0 if first comes after second.

Definition at line 59 of file stricmp.cpp.

References stricmp().

◆ strstri()

char * strstri ( const char *  s1,
const char *  s2 
)

Do case-insensitive search for string 2 (s2) in string 1 (s1). Similar to the C library's strstr().

Parameters
s1Pointer to the string to search
s2Pointer to the string to search for
Returns
Pointer into the string to search where the first match was found; otherwise, return nullptr.

Definition at line 35 of file strstri.cpp.

Referenced by Replace().

◆ titlecase()

std::string titlecase ( const std::string &  s)

Convert the first word (or only word) in the given string to lowercase then make the first letter uppercase. It does not touch any of the other words after the first.

Note that this is not locale-sensitive.

Parameters
sA string to work on.
Returns
Returns a new string containing the modified string.

Definition at line 11 of file titlecase.cpp.

Referenced by Interpreter_Class::Interpret().

◆ uint32_to_binary()

std::string uint32_to_binary ( uint32_t  number)

Function to convert a 32-bit unsigned integer into a string representation containing all 32 bits.

Need this since C does not normally provide a printf() converter to binary.

Parameters
numberThe number to convert to a string representation in binary.
Returns
Returns a string containing the binary string.

Definition at line 18 of file uint32_to_binary.cpp.

Referenced by ObserverForBinary::NumberChanged().

◆ wcsstri()

wchar_t * wcsstri ( const wchar_t *  s1,
const wchar_t *  s2 
)

Do case-insensitive search for string 2 (s2) in string 1 (s1). Similar to the C library's wcsstr().

Parameters
s1Pointer to the string to search
s2Pointer to the string to search for
Returns
Pointer into the string to search where the first match was found; otherwise, return nullptr.

Definition at line 30 of file wcsstri.cpp.

Referenced by Replace().