Created
March 16, 2016 19:03
-
-
Save digitalist/a48a9a7edd5a105bafe5 to your computer and use it in GitHub Desktop.
limited_size_allocator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class T> | |
class LimitedSizeAllocator { | |
//http://stackoverflow.com/questions/1591591/can-one-leverage-stdbasic-string-to-implement-a-string-having-a-length-limitat | |
public: | |
typedef T value_type; | |
typedef std::size_t size_type; | |
typedef std::ptrdiff_t difference_type; | |
typedef T *pointer; | |
typedef const T *const_pointer; | |
typedef T &reference; | |
typedef const T &const_reference; | |
pointer address(reference r) const { return &r; } | |
const_pointer address(const_reference r) const { return &r; } | |
LimitedSizeAllocator() throw() { } | |
template<class U> | |
LimitedSizeAllocator(const LimitedSizeAllocator<U> &) throw() { } | |
~LimitedSizeAllocator() throw() { } | |
pointer allocate(size_type n, void * = 0) { | |
// fail if we try to allocate too much | |
if ((n * sizeof(T)) > max_size()) { throw std::bad_alloc(); } | |
return static_cast<T *>(::operator new(n * sizeof(T))); | |
} | |
void deallocate(pointer p, size_type) { | |
return ::operator delete(p); | |
} | |
void construct(pointer p, const T &val) { new(p) T(val); } | |
void destroy(pointer p) { p->~T(); } | |
// max out at about 64k | |
size_type max_size() const throw() { return 0xff; } | |
template<class U> | |
struct rebind { | |
typedef LimitedSizeAllocator<U> other; | |
}; | |
template<class U> | |
LimitedSizeAllocator &operator=(const LimitedSizeAllocator<U> &rhs) { | |
(void) rhs; | |
return *this; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "LimitedSizeAllocator.h" | |
typedef typename std::basic_string<char, std::char_traits<char>, LimitedSizeAllocator<char> > limitedString; | |
//http://stackoverflow.com/questions/12805771/comparing-stl-strings-that-use-different-allocators/ | |
bool operator== (std::string const& s0, limitedString s1); | |
bool operator!= (std::string const& s0, limitedString s1); | |
//http://stackoverflow.com/a/10377340/5006740 | |
limitedString& operator+=(limitedString& lhs, const std::string& rhs); | |
bool operator== (std::string const& s0, limitedString s1) { | |
return s0.size() == s1.size() && std::equal(s0.begin(), s0.end(), s1.begin()); | |
} | |
bool operator!= (std::string const& s0, limitedString s1) { | |
//bad (no code reuse, prone to bugs on equality params change): | |
return s0.size() != s1.size() || !std::equal(s0.begin(), s0.end(), s1.begin()); | |
//good (we already have == operator): | |
//return !(s0==s1); | |
} | |
//http://stackoverflow.com/a/10377340/5006740 | |
limitedString& operator+=(limitedString& lhs, const std::string& rhs) { | |
char* new_buffer = new char[lhs.size() + rhs.size() +1]; | |
std::copy_n( lhs.begin(), lhs.size(), new_buffer ); | |
std::copy_n( rhs.begin(), rhs.size(), new_buffer+lhs.size() ); | |
lhs = new_buffer; | |
return lhs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment