请帮助测试&改进StringValue类 [英] Please help with testing & improving a StringValue class

查看:32
本文介绍了请帮助测试&改进StringValue类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾在[comp.std.c ++]中建议SomeOne Else(TM)应该提出

a字符串值类,它接受文字和字符串指针等等,

带有可能的自定义删除器,如果是文字字符串,只需带有原始指针的



换句话说,对于最简单的使用代码:


*没有开销(只带一两个指针),并且


*没有异常的可能性(对于那种情况)。


例如,在一个例外情况下,当字符串载体

由于动态分配而投入施工时,并不是很好,而另一个

例子,传递std :: string值可能涉及动态

分配和解除分配,而且效率非常低。


所以昨天晚上我解雇了一个编辑并编码了一点。 /不是/

使用测试驱动的开发:这个代码发生了很大变化,因为它澄清了b $ b!因此,目前还没有任何测试,而且如果有一些TDD人可以设备和运行正确的
测试,那将是很好的。看到它们也很有趣!),和/或


*如果其他人可以简单地尝试使用这个课程并报告

错误,编译器不兼容,需要的功能(我不知道

除了携带

字符串之外它应该提供什么功能)等等,无论相关的是什么。 />

代码使用Boost库中的boost :: intrusive_ptr,因此需要编译
。当前代码不支持嵌入零字符的字符串

。我不认为需要很好。


示例使用代码

StringValue foo()

{

返回没有动态分配,没有可能的例外,快速;

}


StringValue bar()

{

return std :: string(" A dynamic")+"复制;

}

运行所有当前定义的构造函数的示例,其中malloc和

free仅用于演示也是可能的:


< 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()

{

//可以自由复制和分配StringValue,但值

//无法修改。


使用命名空间alfs;


char const * const dynValue =" dynamic copy" ;;

char const * const ptrValue ="指向持久缓冲区的指针" ;;

char const * const customValue =" custom delete";

char const sizedValue [] = {'s'',''我',''z'',''e'','d''};


StringValue literal(" literal"); //没有分配。

StringValue指针(ptrValue,NoDelete()); //没有分配。

StringValue自定义(mallocStr(customValue),myDeleter);

StringValue size(sizedValue,sizeof(sizedValue));

StringValue dynamic(dynValue);

StringValue stdval(std :: string(" std :: string"));


std :: cout< ;<字面数据<< std :: endl;

std :: cout<<指针<< std :: endl;

std :: cout<<自定义<< std :: endl;

std :: cout<<大小<< std :: endl;

std :: cout<<动态<< std :: endl;

std :: cout<< stdval<< std :: endl;

}

< / code>


代码目前可用(特别是如果你想帮助测试和/或

讨论功能或编码,无论如何)

< url:home.no.net/alfps/cpp/lib/alfs_v00.zip(lawyers,如果有的话) :注意

我保留版权等,但当然允许使用。)


干杯,&希望这会很有趣,


- Alf

解决方案

Alf P. Steinbach写道:

....


StringValue literal(" literal"); //没有分配



这样做是否合法:


literal [0] =''L''; ?


Gianni Mariani写道:


Alf P. Steinbach写道:

...


> StringValue literal(" literal"); //没有分配



这样做是否合法:


literal [0] =''L''; ?



int main()

{

//可以自由复制和分配StringValue,但是价值

//无法修改。


-

谢谢

Barry


Alf P. Steinbach写道:


我曾在[comp.std.c ++]中建议SomeOne Else( TM)应该建议

a字符串值类接受文字和字符串指针等等,

可能有自定义删除器,如果是文字字符串只是

携带原始指针。


换句话说,对于最简单的使用代码:


*没有开销(只是携带一个指针)或者两个),并且


*没有异常的可能性(对于那种情况)。


例如,在一个例外中,它是'当字符串载体

由于动态分配而投入施工时会不太好,而对于另一个

示例,pa ssing std :: string值可能涉及动态

分配和解除分配,而且效率非常低。


所以昨天晚上我启动了一个编辑器和编码一点点。 /不是/

使用测试驱动的开发:这个代码发生了很大变化,因为它澄清了b $ b!因此,目前还没有任何测试,而且如果有一些TDD人可以设备和运行正确的
测试,那将是很好的。看到它们也很有趣!),和/或


*如果其他人可以简单地尝试使用这个课程并报告

错误,编译器不兼容,需要的功能(我不知道

除了携带

字符串之外它应该提供什么功能)等等,无论相关的是什么。 />

代码使用Boost库中的boost :: intrusive_ptr,因此需要编译
。当前代码不支持嵌入零字符的字符串

。我不认为需要很好。


示例使用代码


StringValue foo()

{

返回没有动态分配,没有可能的例外,快速;

}


StringValue bar()

{

返回std :: string(" A dynamic")+"复制;

}


运行所有当前定义的构造函数的示例,其中malloc和

free仅用于演示这是可能的:


< 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;

返回strcpy(static_cast< char *>(malloc(strlen(s)+ 1)),s);

}


void myDeleter( void const * p){std :: free(const_cast< void *>(p)); }


int main()

{

//可以自由复制和分配StringValue,但值

//无法修改。


使用命名空间alfs;


char const * const dynValue =" dynamic copy" ;;

char const * const ptrValue ="指向持久缓冲区的指针" ;;

char const * const customValue =" custom delete";

char const sizedValue [] = {'s'',''我',''z'',''e'','d''};


StringValue literal(" literal"); //没有分配。

StringValue指针(ptrValue,NoDelete()); //没有分配。

StringValue自定义(mallocStr(customValue),myDeleter);

StringValue size(sizedValue,sizeof(sizedValue));

StringValue dynamic(dynValue);

StringValue stdval(std :: string(" std :: string"));


std :: cout< ;<字面数据<< std :: endl;

std :: cout<<指针<< std :: endl;

std :: cout<<自定义<< std :: endl;

std :: cout<<大小<< std :: endl;

std :: cout<<动态<< std :: endl;

std :: cout<< stdval<< std :: endl;

}

< / code>


代码目前可用(特别是如果你想帮助测试和/或

讨论功能或编码,无论如何)

< url:home.no.net/alfps/cpp/lib/alfs_v00.zip(lawyers,如果有的话) :注意

我保留版权等,但当然允许使用。)


干杯,&希望这很有意思,



std :: cout<< (dynamic == dynValue);


打印0

因为它调用

StringValue :: operator char const *()


所以==是比较字典比较以外的指针


我认为提供
operator ==进行词典比较。


-

谢谢

Barry


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

解决方案

Alf P. Steinbach wrote:
....

StringValue literal( "literal" ); // No alloc.

Is it legal to do:

literal[0] = ''L''; ?


Gianni Mariani wrote:

Alf P. Steinbach wrote:
...

> StringValue literal( "literal" ); // No alloc.


Is it legal to do:

literal[0] = ''L''; ?

int main()
{
// A StringValue can be freely copied and assigned, but the value
// can not be modified.

--
Thanks
Barry


Alf P. Steinbach wrote:

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,

std:: cout << (dynamic == dynValue);

prints 0

as it calls
StringValue::operator char const* ()

so == is to compare pointer other than lexicographical compare

I think it would be a good idea to provide
operator== to make lexicographical compare.

--
Thanks
Barry


这篇关于请帮助测试&amp;改进StringValue类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆