mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

ManagedString Class Reference

ManagedString Class Reference

Class definition for a ManagedString. More...

#include <ManagedString.h>

Public Member Functions

 ManagedString (StringData *ptr)
 Constructor.
StringData * leakData ()
 Get current ptr, do not decr() it, and set the current instance to empty string.
 ManagedString (const char *str)
 Constructor.
 ManagedString (const int value)
 Constructor.
 ManagedString (const char value)
 Constructor.
 ManagedString (PacketBuffer buffer)
 Constructor.
 ManagedString (const char *str, const int16_t length)
 Constructor.
 ManagedString (const ManagedString &s)
 Copy constructor.
 ManagedString ()
 Default constructor.
 ~ManagedString ()
 Destructor.
ManagedStringoperator= (const ManagedString &s)
 Copy assign operation.
bool operator== (const ManagedString &s)
 Equality operation.
bool operator< (const ManagedString &s)
 Inequality operation.
bool operator> (const ManagedString &s)
 Inequality operation.
ManagedString substring (int16_t start, int16_t length)
 Extracts a ManagedString from this string, at the position provided.
char charAt (int16_t index)
 Provides a character value at a given position in the string, indexed from zero.
const char * toCharArray () const
 Provides an immutable 8 bit wide character buffer representing this string.
int16_t length () const
 Determines the length of this ManagedString in characters.

Static Public Attributes

static ManagedString EmptyString
 Empty String constant.

Friends

ManagedString operator+ (const ManagedString &lhs, const ManagedString &rhs)
 Concatenates two strings.

Detailed Description

Class definition for a ManagedString.

Uses basic reference counting to implement a copy-assignable, immutable string.

This maps closely to the constructs found in many high level application languages, such as Touch Develop.

Written from first principles here, for several reasons: 1) std::shared_ptr is not yet availiable on the ARMCC compiler

2) to reduce memory footprint - we don't need many of the other features in the std library

3) it makes an interesting case study for anyone interested in seeing how it works!

4) we need explicit reference counting to inter-op with low-level application langauge runtimes.

5) the reference counting needs to also work for read-only, flash-resident strings

Definition at line 59 of file ManagedString.h.


Constructor & Destructor Documentation

ManagedString ( StringData *  p )

Constructor.

Create a managed string from a specially prepared string literal.

Parameters:
ptrThe literal - first two bytes should be 0xff, then the length in little endian, then the literal. The literal has to be 4-byte aligned.
 static const char hello[] __attribute__ ((aligned (4))) = "\xff\xff\x05\x00" "Hello";
 ManagedString s((StringData*)(void*)hello);

Definition at line 92 of file ManagedString.cpp.

ManagedString ( const char *  str )

Constructor.

Create a managed string from a pointer to an 8-bit character buffer.

The buffer is copied to ensure safe memory management (the supplied character buffer may be declared on the stack for instance).

Parameters:
strThe character array on which to base the new ManagedString.
 ManagedString s("abcdefg");

Definition at line 160 of file ManagedString.cpp.

ManagedString ( const int  value )

Constructor.

Create a managed string from a given integer.

Parameters:
valueThe integer from which to create the ManagedString.
 ManagedString s(20);

Definition at line 121 of file ManagedString.cpp.

ManagedString ( const char  value )

Constructor.

Create a managed string from a given char.

Parameters:
valueThe character from which to create the ManagedString.
 ManagedString s('a');

Definition at line 139 of file ManagedString.cpp.

ManagedString ( PacketBuffer  buffer )

Constructor.

Create a ManagedString from a PacketBuffer. All bytes in the PacketBuffer are added to the ManagedString.

Parameters:
bufferThe PacktBuffer from which to create the ManagedString.
 ManagedString s = radio.datagram.recv();

Definition at line 211 of file ManagedString.cpp.

ManagedString ( const char *  str,
const int16_t  length 
)

Constructor.

Create a ManagedString from a pointer to an 8-bit character buffer of a given length.

The buffer is copied to ensure sane memory management (the supplied character buffer may be declared on the stack for instance).

Parameters:
strThe character array on which to base the new ManagedString.
lengthThe length of the character array
 ManagedString s("abcdefg",7);

Definition at line 238 of file ManagedString.cpp.

ManagedString ( const ManagedString s )

Copy constructor.

Makes a new ManagedString identical to the one supplied.

Shares the character buffer and reference count with the supplied ManagedString.

Parameters:
sThe ManagedString to copy.
 ManagedString s("abcdefg");
 ManagedString p(s);

Definition at line 270 of file ManagedString.cpp.

Default constructor.

Create an empty ManagedString.

Definition at line 286 of file ManagedString.cpp.

~ManagedString (  )

Destructor.

Free this ManagedString, and decrement the reference count to the internal character buffer.

If we're holding the last reference, also free the character buffer.

Definition at line 299 of file ManagedString.cpp.


Member Function Documentation

char charAt ( int16_t  index )

Provides a character value at a given position in the string, indexed from zero.

Parameters:
indexThe position of the character to return.
Returns:
the character at posisiton index, zero if index is invalid.
 MicroBitDisplay display;
 ManagedString s("abcd");

 display.scroll(s.charAt(1)) // scrolls "b"
Parameters:
indexThe position of the character to return.
Returns:
the character at position index, zero if index is invalid.
 MicroBitDisplay display;
 ManagedString s("abcd");

 display.scroll(s.charAt(1)) // scrolls "b"

Definition at line 483 of file ManagedString.cpp.

StringData * leakData (  )

Get current ptr, do not decr() it, and set the current instance to empty string.

This is to be used by specialized runtimes which pass StringData around.

Definition at line 103 of file ManagedString.cpp.

int16_t length (  ) const

Determines the length of this ManagedString in characters.

Returns:
the length of the string in characters.
 MicroBitDisplay display;
 ManagedString s("abcd");

 display.scroll(s.length()) // scrolls "4"

Definition at line 359 of file ManagedString.h.

bool operator< ( const ManagedString s )

Inequality operation.

Called when one ManagedString is tested to be less than another using the '<' operator.

Parameters:
sThe ManagedString to test ourselves against.
Returns:
true if this ManagedString is alphabetically less than to the one supplied, false otherwise.
 MicroBitDisplay display;
 ManagedString s("a");
 ManagedString p("b");

 if(s < p)
     display.scroll("a is before b!"); //a is before b
 else
     display.scroll("b is before a!");

Definition at line 380 of file ManagedString.cpp.

ManagedString & operator= ( const ManagedString s )

Copy assign operation.

Called when one ManagedString is assigned the value of another.

If the ManagedString being assigned is already referring to a character buffer, decrement the reference count and free up the buffer as necessary.

Then, update our character buffer to refer to that of the supplied ManagedString, and increase its reference count.

Parameters:
sThe ManagedString to copy.
 ManagedString s("abcd");
 ManagedString p("efgh");
 p = s   // p now points to s, s' ref is incremented

Definition at line 323 of file ManagedString.cpp.

bool operator== ( const ManagedString s )

Equality operation.

Called when one ManagedString is tested to be equal to another using the '==' operator.

Parameters:
sThe ManagedString to test ourselves against.
Returns:
true if this ManagedString is identical to the one supplied, false otherwise.
 MicroBitDisplay display;
 ManagedString s("abcd");
 ManagedString p("efgh");

 if(p == s)
     display.scroll("We are the same!");
 else
     display.scroll("We are different!"); //p is not equal to s - this will be called

Definition at line 355 of file ManagedString.cpp.

bool operator> ( const ManagedString s )

Inequality operation.

Called when one ManagedString is tested to be greater than another using the '>' operator.

Parameters:
sThe ManagedString to test ourselves against.
Returns:
true if this ManagedString is alphabetically greater than to the one supplied, false otherwise.
 MicroBitDisplay display;
 ManagedString s("a");
 ManagedString p("b");

 if(p>a)
     display.scroll("b is after a!"); //b is after a
 else
     display.scroll("a is after b!");

Definition at line 405 of file ManagedString.cpp.

ManagedString substring ( int16_t  start,
int16_t  length 
)

Extracts a ManagedString from this string, at the position provided.

Parameters:
startThe index of the first character to extract, indexed from zero.
lengthThe number of characters to extract from the start position
Returns:
a ManagedString representing the requested substring.
 MicroBitDisplay display;
 ManagedString s("abcdefg");

 display.scroll(s.substring(0,2)) // displays "ab"

Definition at line 426 of file ManagedString.cpp.

const char* toCharArray (  ) const

Provides an immutable 8 bit wide character buffer representing this string.

Returns:
a pointer to the character buffer.

Definition at line 342 of file ManagedString.h.


Friends And Related Function Documentation

ManagedString operator+ ( const ManagedString lhs,
const ManagedString rhs 
) [friend]

Concatenates two strings.

Parameters:
lhsThe first ManagedString to concatenate.
rhsThe second ManagedString to concatenate.
Returns:
a new ManagedString representing the joined strings.
 MicroBitDisplay display;
 ManagedString s("abcd");
 ManagedString p("efgh")

 display.scroll(s + p) // scrolls "abcdefgh"

Definition at line 455 of file ManagedString.cpp.


Field Documentation

Empty String constant.

Empty string constant literal.

Definition at line 367 of file ManagedString.h.