- /*
- * std_string_custom_new2.cpp - custom basic_string allocator demo
- *
- * Compile with:
- * clang++ -std=c++17 -g -Wall std_string_custom_new2.cpp
- * Written by Roland Mainy <roland.mainz@nrubsig.org>
- *
- */
- #include <iostream>
- #include <memory>
- #include <limits>
- /* required for |main()| test/demo code */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <inttypes.h>
- /* we need this for |std::numeric_limits<size_type>::max()| to work */
- #undef max
- template <typename T> class EvilStrAllocator {
- public:
- typedef T value_type;
- typedef value_type* pointer;
- typedef const value_type* const_pointer;
- typedef value_type& reference;
- typedef const value_type& const_reference;
- typedef std::size_t size_type;
- typedef std::ptrdiff_t difference_type;
- public:
- template<typename U>
- struct rebind {
- typedef EvilStrAllocator<U> other;
- };
- public:
- EvilStrAllocator() {}
- ~EvilStrAllocator() {}
- EvilStrAllocator(EvilStrAllocator const&) {}
- template<typename U>
- EvilStrAllocator(EvilStrAllocator<U> const&) {}
- pointer address(reference r) {
- return &r;
- }
- const_pointer address(const_reference r) {
- return &r;
- }
- pointer allocate(size_type cnt,
- typename std::allocator<void>::const_pointer = 0) {
- T *ptr = (T*)malloc(cnt * sizeof(T));
- (void)fprintf(stderr, "##allocate %p, cnt=%ld\n",
- (void*)ptr,
- (long)cnt);
- return ptr;
- }
- void deallocate(pointer ptr, size_type cnt) {
- (void)fprintf(stderr, "##free %p, cnt=%ld\n",
- (void*)ptr,
- (long)cnt);
- free((void *)ptr);
- }
- size_type max_size() const {
- return std::numeric_limits<size_type>::max() / sizeof(T);
- }
- void construct(pointer p, const T& t) {
- new(p) T(t);
- }
- void destroy(pointer p) {
- p->~T();
- }
- bool operator==(EvilStrAllocator const& a) { return this == &a; }
- bool operator!=(EvilStrAllocator const& a) { return !operator==(a); }
- };
- int main(int ac, char *av[])
- {
- typedef std::basic_string<char, std::char_traits<char>, EvilStrAllocator<char>> myString;
- /*
- * |basic_string| will not make use of an allocator if the
- * string is too small, so we use whitespace as padding,
- + as |strtol()|&co. allows leading whitespaces
- */
- myString str { " 72488" };
- myString copy = str;
- for(int i=0 ; i < 3 ; i++)
- {
- myString str2 = " " + copy;
- /*
- * |std::string s_str = str2;| does not work yet, clang++
- * says:
- * std_string_custom_new2.cpp: error: no viable
- * conversion from 'basic_string<[2 * ...],
- * EvilStrAllocator<char>>' to 'basic_string<[2 * ...],
- * (default) std::allocator<char>>'
- */
- std::string s_str(str2);
- copy = s_str;
- }
- const char* cstr = copy.c_str();
- intmax_t out = strtoimax(cstr, NULL, 10);
- (void)printf(("strlen(str=|%s|)=%" PRIdMAX ", result=%" PRIdMAX "\n"),
- cstr,
- (intmax_t)strlen(cstr),
- out);
- return EXIT_SUCCESS;
- }
std_string_custom_new2.cpp - custom basic_string allocator demo
Posted by Anonymous on Mon 27th Jun 2022 12:54
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.