Specialization of @c ats_scoped_resource for strings. This contains an allocated string that is cleaned up if not explicitly released. */
| 491 | This contains an allocated string that is cleaned up if not explicitly released. |
| 492 | */ |
| 493 | class ats_scoped_str : public ats_scoped_resource<detail::SCOPED_MALLOC_TRAITS<char>> |
| 494 | { |
| 495 | public: |
| 496 | using super = ats_scoped_resource<detail::SCOPED_MALLOC_TRAITS<char>>; ///< Super type. |
| 497 | using self_type = ats_scoped_str; ///< Self reference type. |
| 498 | |
| 499 | /** Default constructor. |
| 500 | * Constructs an empty container. |
| 501 | */ |
| 502 | ats_scoped_str() = default; |
| 503 | |
| 504 | /** Construct an empty buffer of @a n bytes. |
| 505 | * |
| 506 | * @param n Size of the contained buffer. |
| 507 | * |
| 508 | * The memory is left uninitialized, presumably ready for a @c memcpy. |
| 509 | */ |
| 510 | explicit ats_scoped_str(size_t n) : super(static_cast<char *>(ats_malloc(n))) {} |
| 511 | |
| 512 | /** Construct with an existing buffer. |
| 513 | * |
| 514 | * @param s The string. |
| 515 | * |
| 516 | * This container takes ownership of the string memory. For this reason @a s must be independently |
| 517 | * allocated, not part of a larger memory allocation. |
| 518 | */ |
| 519 | explicit ats_scoped_str(char *s) : super(s) {} |
| 520 | |
| 521 | /** Construct from a @c string_view. |
| 522 | * |
| 523 | * @param s The string to copy. |
| 524 | * |
| 525 | * The string in @a s is duplicated into this container. |
| 526 | */ |
| 527 | explicit ats_scoped_str(std::string_view s); |
| 528 | |
| 529 | /** Put an existing buffer in this container. |
| 530 | * |
| 531 | * @param s The string buffer. |
| 532 | * @return @a this |
| 533 | * |
| 534 | * The container takes ownership of @a s. For this reason @a s must be independently allocated, |
| 535 | * not part of a larger memory allocation. Any currently contained resource is destroyed. |
| 536 | */ |
| 537 | self_type &operator=(char *s); |
| 538 | |
| 539 | /** Assign from a @c string_view |
| 540 | * |
| 541 | * @param s The source string. |
| 542 | * @return @a this |
| 543 | * |
| 544 | * The string data is duplicated into this object and guaranteed to be null terminated. Any currently |
| 545 | * contained resource is destroyed. |
| 546 | */ |
| 547 | self_type &operator=(std::string_view s); |
| 548 | }; |
| 549 | |
| 550 | inline ats_scoped_str::ats_scoped_str(std::string_view s) |