如何获取C std :: string char数据的所有权,而不复制并保留std :: string对象?

前端之家收集整理的这篇文章主要介绍了如何获取C std :: string char数据的所有权,而不复制并保留std :: string对象?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何获取std :: string char数据的所有权,而不复制并保留源std :: string对象? (我想使用移动语义,但不同类型之间.)

我使用C 11 Clang编译器和Boost.

基本上我想做一些相当于这样的事情:

  1. {
  2. std::string s(“Possibly very long user string”);
  3. const char* mine = s.c_str();
  4.  
  5. // 'mine' will be passed along,pass(mine);
  6.  
  7. //Made-up call
  8. s.release_data();
  9.  
  10. // 's' should not release data,but it should properly destroy itself otherwise.
  11. }

要澄清,我确实需要摆脱std :: string:进一步走下坡路.该代码处理字符串和二进制数据,并应以相同的格式处理它.而且我想要std :: string的数据,因为它来自另一个与std :: string一起工作的代码层.

为了给我更多的观点,我想要这样做:例如我有一个异步的套接字封装,应该能够使用std :: string和二进制数据从用户的写作.两个“API”写入版本(采用std :: string或行二进制数据)内部解析为相同的(二进制)写入.我需要避免任何复制,因为字符串可能很长.

  1. WriteId write( std::unique_ptr< std::string > strToWrite )
  2. {
  3.  
  4. // Convert std::string data to contiguous byte storage
  5. // that will be further passed along to other
  6. // functions (also with the moving semantics).
  7. // strToWrite.c_str() would be a solution to my problem
  8. // if I could tell strToWrite to simply give up its
  9. // ownership. Is there a way?
  10.  
  11. unique_ptr<std::vector<char> > dataToWrite= ??
  12.  
  13. //
  14. scheduleWrite( dataToWrite );
  15. }
  16.  
  17. void scheduledWrite( std::unique_ptr< std::vecor<char> > data)
  18. {
  19. }

std :: unique_ptr在这个例子中说明所有权转让:任何其他具有相同语义的方法对我来说都是好的.

我想知道这个具体情况的解决方案(使用std :: string char缓冲区)和字符串,流和类似的一般问题:在string,stream,std容器和缓冲区类型之间移动缓冲区的提示.

我也会赞赏提示链接C设计方法和具体技术,当涉及传递缓冲区数据之间不同的API /类型而不复制.我提到但不使用流,因为我在这个问题上摇摇欲坠.

解决方法

How can I take ownership of std::string char data without copying and withoug keeping source std::string object ? (I want to use moving semantics but between different types)

你不能安全地做到这一点.

对于具体的实现,在某些情况下,您可以使用别名来修改字符串中的私有成员变量来欺骗字符串,认为它不再拥有缓冲区.但是即使你愿意尝试这样做也不会总是奏效.例如.考虑小字符串优化,其中字符串没有指向一些保存数据的外部缓冲区的指针,数据在字符串对象本身内.

如果您想避免复制,可以考虑将界面更改为scheduledWrite.一种可能性是:

  1. template<typename Container>
  2. void scheduledWrite(Container data)
  3. {
  4. // requires data[i],data.size(),and &data[n] == &data[0] + n for n [0,size)
  5. }
  6.  
  7. // move resources from object owned by a unique_ptr
  8. WriteId write( std::unique_ptr< std::vector<char> > vecToWrite)
  9. {
  10. scheduleWrite(std::move(*vecToWrite));
  11. }
  12.  
  13. WriteId write( std::unique_ptr< std::string > strToWrite)
  14. {
  15. scheduleWrite(std::move(*strToWrite));
  16. }
  17.  
  18. // move resources from object passed by value (callers also have to take care to avoid copies)
  19. WriteId write(std::string strToWrite)
  20. {
  21. scheduleWrite(std::move(strToWrite));
  22. }
  23.  
  24. // assume ownership of raw pointer
  25. // requires data to have been allocated with new char[]
  26. WriteId write(char const *data,size_t size) // you could also accept an allocator or deallocation function and make ptr_adapter deal with it
  27. {
  28. struct ptr_adapter {
  29. std::unique_ptr<char const []> ptr;
  30. size_t m_size;
  31. char const &operator[] (size_t i) { return ptr[i]; }
  32. size_t size() { return m_size; }
  33. };
  34.  
  35. scheduleWrite(ptr_adapter{data,size});
  36. }

猜你在找的C&C++相关文章