Evo C++ Library v0.5.1
Sharing

Sharing is a form of Copy-On-Write (COW) optimization with Reference Counting.

Containers like List and String use sharing:

Containers that support Sharing: List, String, PtrList, MapList, MapHash

Example
#include <evo/string.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
// Reference a string literal -- no memory allocated here
String str("test");
// Append a space and number
// - This is a write operation (modifier) so will allocate a buffer to hold "test 123"
str << " 123";
// Split into two tokens separated by space -- data is shared, no memory allocated here
String left, right;
str.split(' ', left, right);
// Append "ing" to first token
// - This is a write operation (modifier) so will allocate a buffer to hold "testing"
left << "ing";
// Prints: testing,123
c.out << left << ',' << right << NL;
return 0;
}
Example with raw pointer
#include <evo/string.h>
#include <evo/ptr.h>
using namespace evo;
int main() {
// Fixed buffers like this are common in C
char buf[10];
strcpy(buf, "testing");
String str;
str = "foo"; // Ok, string literal (static and immutable)
str = buf; // DANGER: assigning raw pointer to Evo container, which may reference that pointer instead of copy from it
str = Ptr<char>(buf); // Ok, assign as dumb pointer so the assignment will make a copy
return 0;
}
Example with sharing and slicing
#include <evo/string.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
// Reference a string literal -- no memory allocated here
String str("_test_");
// Remove (slice out) underscore characters, same literal is still referenced
str.slice(1, 4);
// Append a dash, space, and number
// - This is a write operation (modifier) so will allocate a buffer to hold "test- 123" -- sliced out data is discarded
str << "- 123";
// Split into two tokens separated by dash -- data is shared, no memory allocated here
String left, right;
str.split('-', left, right);
// Strip (slice out) leading space from right substring
right.stripl(' ');
// Append "ing" to first token
// - This is a write operation (modifier) so will allocate a buffer to hold "testing"
left << "ing";
// Prints: testing,123
c.out << left << ',' << right << NL;
}