Please help with testing & improving a StringValue class
I once suggested in [comp.std.c++] that SomeOne Else(TM) should propose
a string value class that accepted literals and char pointers and so on,
with possible custom deleter, and in case of literal strings just
carrying the original pointer.
In other words, for the simplest usage code:
* no overhead (just carrying a pointer or two), and
* no possibility of exceptions (for that case).
For example, in an exception it's not so good when the string carrier
can throw on construction due to dynamic allocation, and for another
example, passing std::string values around can involve dynamic
allocations and deallocations, and those are very inefficient.
So yesterday evening I fired up an editor and coded a little bit. /Not/
using test-driven development: this code changed significantly as it
clarified! Therefore, there aren't any tests, as yet, and
* it would be nice if some TDD person could device and run proper
tests (it would also be interesting to see them!), and/or
* if other persons could simply try to use this class and report on
bugs, compiler incompatibilities, needed functionality (I don't know
what functionality it should provide in addition to carrying
strings) etc., whatever's relevant.
The code uses boost::intrusive_ptr from the Boost library, which
therefore is required to compile. Strings with embedded zero characters
are not supported in the current code. I don't think the need is great.
Example usage code
StringValue foo()
{
return "No dynamic allocation, no possible exception, fast";
}
StringValue bar()
{
return std::string( "A dynamic" ) + " copy";
}
Example exercising all currently defined constructors, where malloc and
free is used just to demonstrate that also that is possible:
<code>
#include <alfs/StringValueClass.hpp>
#include <iostream>
#include <cstdlib> // std::malloc, std::free
#include <cstring> // std::strcpy, std::strlen
char const* mallocStr( char const s[] )
{
using namespace std;
return strcpy( static_cast<char*>( malloc( strlen( s ) + 1 ) ), s );
}
void myDeleter( void const* p ) { std::free( const_cast<void*>( p ) ); }
int main()
{
// A StringValue can be freely copied and assigned, but the value
// can not be modified.
using namespace alfs;
char const* const dynValue = "dynamic copy";
char const* const ptrValue = "pointer to persistent buffer";
char const* const customValue = "custom delete";
char const sizedValue[] = { 's', 'i', 'z', 'e', 'd' };
StringValue literal( "literal" ); // No alloc.
StringValue pointer( ptrValue, NoDelete() ); // No alloc.
StringValue custom( mallocStr( customValue ), myDeleter );
StringValue sized( sizedValue, sizeof( sizedValue ) );
StringValue dynamic( dynValue );
StringValue stdval( std::string( "std::string" ) );
std::cout << literal << std::endl;
std::cout << pointer << std::endl;
std::cout << custom << std::endl;
std::cout << sized << std::endl;
std::cout << dynamic << std::endl;
std::cout << stdval << std::endl;
}
</code>
Code currently available (especially if you want to help testing and or
discussing functionality or coding, whatever) at
<url: home.no.net/alfps/cpp/lib/alfs_v00.zip> (lawyers, if any: note
that I retain copyright etc., although usage is of course permitted).
Cheers, & hope this can be interesting,
- Alf