I’m trying to make a class that behaves like MS CString (that is, you pass it to printf and it acts just like a pointer to C string, without additional ugly black magic like “.c_str()”).
This is very first implementation of this class, that just works and doesn’t yet provide anything useful:
#include <cstdlib>
#include <cstring>
class CString
{
protected:
struct CStringInfo
{
size_t Length;
size_t MaxLength;
};
public:
CString()
{
Buffer = NULL;
Assign(NULL);
}
CString(const char* chv)
{
Buffer = NULL;
Assign(chv, 0);
}
~CString()
{
if(Buffer) delete[] Buffer;
Buffer = NULL;
}
size_t GetLength()
{
if(!Buffer) Alloc(1);
return GetInfo()->Length;
}
size_t Resize(size_t size)
{
Alloc(size + 1); // + 0x00
Buffer[size] = 0;
return size;
}
bool Assign(const char* value, size_t size = 0)
{
size_t strl = ((size) ? size : strlen(value));
if(!value || !(strl = strlen(value)))
{
if(!Buffer) Alloc(1);
return false;
}
Alloc(strl + 1);
memcpy(Buffer, value, strl);
Buffer[strl] = 0;
return true;
}
CString& operator = (const char* what)
{
Assign(what);
return (*this);
}
CString& operator = (CString& string)
{
Assign(string.Buffer);
return (*this);
}
operator const char* ()
{
return Buffer;
}
protected:
char* Buffer;
void Alloc(size_t size)
{
if(!size) size = 1;
char* nb = new char[size + sizeof(CStringInfo)];
char* nbb = nb + sizeof(CStringInfo);
size_t cl = size - 1;
if(Buffer)
{
if(cl > GetInfo()->Length) cl = GetInfo()->Length;
if(cl) memcpy(nbb, Buffer, cl - 1);
nbb[cl] = 0;
*(CStringInfo*)(nb) = *(CStringInfo*)(Buffer);
delete[] (Buffer - sizeof(CStringInfo));
}
Buffer = nb;
GetInfo()->MaxLength = size;
GetInfo()->Length = cl;
}
void Free()
{
if(Buffer)
{
delete[] (Buffer - sizeof(CStringInfo));
}
}
CStringInfo* GetInfo()
{
return (CStringInfo*)(this->Buffer - sizeof(CStringInfo));
}
};
And code I test it on:
#include <cstdio>
#include "CString.hpp"
CString global_str = "global string!";
int main(int argc, char* argv[])
{
CString str = "string";
printf("Test: %s, %s\n", str, global_str);
return 0;
}
If I don’t have a destructor in the class, then I can pass it to printf and it will work just like it should (as a C string). But when I add destructor, GCC produces following error:
error: cannot pass objects of non-trivially-copyable type 'class CString' through '...'
And in addition to that prior versions of GCC will give a warning + ud2 opcode.
So… Question: can I actually make following construction work in GCC or is there any way, possibly not involving C varargs, to make something identical in use to above code?
You can trigger the conversion operator with a cast:
However, I don’t know if you will run into any problems with this, avoiding varargs in C++ code would probably be the best.
How about using the type-safe printf instead (Credit: Wikipedia):
I don’t think libstdc++ supports
std::runtime_errorandstd::logic_errorthough.