UPDATE: This post is seriously old, and Copy-On-Write strings are not recommended, especially nowadays with the widespread use of multiple core machines.
In my last contract, we used CEGUI for the User Interface. The String class provided and used by CEGUI uses an internal array as quick buffer for small strings, and reverts to the heap when the data doesn’t fit that quick buffer.
That approach increases performance if the quick buffer is big enough to hold most of your string operations.
However it lacks some features:
- The quick buffer size is hard-coded in the class, instead of template-based.
- When reverting to heap based, it doesn’t use any mechanisms to minimize allocations/ deallocations, like for example (Reference counting and Copy-On-Write)
Since I’m starting a new hobby project (my own small game engine), to test ideas and learn some new things, the first thing I’m coding is my own String class, which I’m also using as an exercise to test some more advanced templates stuff, instead of the classical “container of values of type T”.
So… about my String class.
- Completely template based, using policies like described in the book Modern C++. This allows for a great deal of customization.
- Using policies, it supports multiple types of string storage. Currently it support 2 storage types:
- Heap based with reference counting and Copy-On-Write, to delay memory allocations/deallocations as much as possible.
- Quick buffer based (like CEGUI), but with fallback to Heap based like described above when the data doesn’t fit the quick buffer. Also, the quick buffer size itself is a template parameter, so we set the size based on the use we have in mind.
- We can mix strings declared with different policies (the ones provided), and internally it will still try to minimize memory allocations/deallocations, by using reference counting and Copy-On-Write when possible.
- We can specify our own Allocator for heap based strings
- Basic methods for manipulations. I need to implement a lot more.
- More methods for manipulations (make it more compatible with the std::string)
- Unicode support. I’ve laid down the bases for that, but currently only works with char type
When coding it, I’ve used CppUnite2 for the tests. See this link for more information (Games from Within: CppUnitLite2 1.1).
I haven’t used this String class in a complex product yet, only in the tests during development, so I can’t talk about performance or bugs yet.
If you want to check it out, download it below:
NOTE: I’ve only tested it with Visual Studio 2005 (with sp1), so the provided project files are for that same compiler.