Skip to content

Instantly share code, notes, and snippets.

@lighta
Created November 18, 2017 11:16
Show Gist options
  • Save lighta/90d8814c63b5aab3515d87827f2f845e to your computer and use it in GitHub Desktop.
Save lighta/90d8814c63b5aab3515d87827f2f845e to your computer and use it in GitHub Desktop.
testing string with maxlen
/*
* File: main.cpp
* Author: lighta
*
* Created on November 18, 2017, 4:17 AM
*/
#include <cstdlib>
#include <string>
#include <iostream>
#include <type_traits>
using namespace std;
//simple allocator with maxN accepted arg
template <class T>
class ra_allocator
{
protected:
const int maxN;
public:
using value_type = T;
using pointer = value_type*;
using const_pointer = typename std::pointer_traits<pointer>::template
rebind<value_type const>;
using void_pointer = typename std::pointer_traits<pointer>::template
rebind<void>;
using const_void_pointer = typename std::pointer_traits<pointer>::template
rebind<const void>;
using difference_type = typename std::pointer_traits<pointer>::difference_type;
using size_type = std::make_unsigned_t<difference_type>;
template <class U> struct rebind {typedef ra_allocator<U> other;};
ra_allocator(int N=250) noexcept
: maxN(N)
{} // not required, unless used
template <class U> ra_allocator(ra_allocator<U> const& other) noexcept
: maxN(other.maxN)
{}
value_type* // Use pointer if pointer is not a value_type*
allocate(std::size_t n)
{
return static_cast<value_type*>(::operator new (n*sizeof(value_type)));
}
void
deallocate(value_type* p, std::size_t) noexcept // Use pointer if pointer is not a value_type*
{
::operator delete(p);
}
value_type*
allocate(std::size_t n, const_void_pointer)
{
return allocate(n);
}
template <class U, class ...Args>
void
construct(U* p, Args&& ...args)
{
::new(p) U(std::forward<Args>(args)...);
}
template <class U>
void
destroy(U* p) noexcept
{
p->~U();
}
std::size_t
max_size() const noexcept
{
return (maxN+1);
// return std::numeric_limits<size_type>::max();
// return 250;
}
ra_allocator
select_on_container_copy_construction() const
{
return *this;
}
using propagate_on_container_copy_assignment = std::false_type;
using propagate_on_container_move_assignment = std::false_type;
using propagate_on_container_swap = std::false_type;
using is_always_equal = std::is_empty<ra_allocator>;
};
template <class T, class U>
bool operator==(ra_allocator<T> const&, ra_allocator<U> const&) noexcept
{
return true;
}
template <class T, class U>
bool operator!=(ra_allocator<T> const& x, ra_allocator<U> const& y) noexcept
{
return !(x == y);
}
template <class T, int N=40>
class ra_allocatorMaxN : public ra_allocator<T> {
public:
ra_allocatorMaxN() noexcept
: ra_allocator<T>(N)
{}
};
using ratest1 = ra_allocatorMaxN<char,30>;
#define raStringN(N) std::basic_string<char, std::char_traits<char>, ra_allocator<char>>
//#define raStringN(N) std::basic_string<char, std::char_traits<char>, ratest1>
typedef std::basic_string<char, std::char_traits<char>, std::allocator<char>> raStringL; //standard same as std
/*
*
*/
int main(int argc, char** argv) {
raStringN(40) ltest = "hello world";
raStringN(40) ltest2 = "_world_";
raStringN(40) ltest3 = ltest + ltest2;
std::cout << ltest << " size=" << ltest.size() << " maxsize=" << ltest.max_size() << "\n";
std::cout << ltest2 << " size=" << ltest2.size() << " maxsize=" << ltest2.max_size() << "\n";
std::cout << ltest3 << " size=" << ltest3.size() << " maxsize=" << ltest3.max_size() << "\n";
raStringN(200) ltest4 = "0123456789";
raStringN(200) ltest5, ltest6;
for(size_t i=0; i<10; i++) //should be ok
ltest5+= ltest4;
try {
for(size_t i=0; i<30; i++)
ltest6+= ltest4;
}catch(...){
//ltest6 = "Success, expected std::length_error";
}
std::cout << ltest5 << " size=" << ltest5.size() << " maxsize=" << ltest5.max_size() << "\n";
std::cout << ltest6 << " size=" << ltest6.size() << " maxsize=" << ltest6.max_size() << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment