1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
// Copyright 2006-2009 Daniel James.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#if !defined(BOOST_UNORDERED_EXCEPTION_TEST_OBJECTS_HEADER)
#define BOOST_UNORDERED_EXCEPTION_TEST_OBJECTS_HEADER
#include "../helpers/exception_test.hpp"
#include <cstddef>
#include <iostream>
#include <boost/limits.hpp>
#include <new>
#include "../helpers/fwd.hpp"
#include "../helpers/allocator.hpp"
#include "../helpers/memory.hpp"
namespace test
{
namespace exception
{
namespace detail
{
namespace
{
test::detail::memory_tracker<test::malloc_allocator<int> > tracker;
}
}
class object;
class hash;
class equal_to;
template <class T> class allocator;
object generate(object const*);
class object
{
public:
int tag1_, tag2_;
explicit object() : tag1_(0), tag2_(0)
{
UNORDERED_SCOPE(object::object()) {
UNORDERED_EPOINT("Mock object default constructor.");
}
}
explicit object(int t1, int t2 = 0) : tag1_(t1), tag2_(t2)
{
UNORDERED_SCOPE(object::object(int)) {
UNORDERED_EPOINT("Mock object constructor by value.");
}
}
object(object const& x)
: tag1_(x.tag1_), tag2_(x.tag2_)
{
UNORDERED_SCOPE(object::object(object)) {
UNORDERED_EPOINT("Mock object copy constructor.");
}
}
~object() {
tag1_ = -1;
tag2_ = -1;
}
object& operator=(object const& x)
{
UNORDERED_SCOPE(object::operator=(object)) {
tag1_ = x.tag1_;
UNORDERED_EPOINT("Mock object assign operator 1.");
tag2_ = x.tag2_;
//UNORDERED_EPOINT("Mock object assign operator 2.");
}
return *this;
}
friend bool operator==(object const& x1, object const& x2) {
UNORDERED_SCOPE(operator==(object, object)) {
UNORDERED_EPOINT("Mock object equality operator.");
}
return x1.tag1_ == x2.tag1_ && x1.tag2_ == x2.tag2_;
}
friend bool operator!=(object const& x1, object const& x2) {
UNORDERED_SCOPE(operator!=(object, object)) {
UNORDERED_EPOINT("Mock object inequality operator.");
}
return !(x1.tag1_ == x2.tag1_ && x1.tag2_ == x2.tag2_);
}
// None of the last few functions are used by the unordered associative
// containers - so there aren't any exception points.
friend bool operator<(object const& x1, object const& x2) {
return x1.tag1_ < x2.tag1_ ||
(x1.tag1_ == x2.tag1_ && x1.tag2_ < x2.tag2_);
}
friend object generate(object const*) {
int* x = 0;
return object(::test::generate(x), ::test::generate(x));
}
friend std::ostream& operator<<(std::ostream& out, object const& o)
{
return out<<"("<<o.tag1_<<","<<o.tag2_<<")";
}
};
class hash
{
int tag_;
public:
hash(int t = 0) : tag_(t)
{
UNORDERED_SCOPE(hash::object()) {
UNORDERED_EPOINT("Mock hash default constructor.");
}
}
hash(hash const& x)
: tag_(x.tag_)
{
UNORDERED_SCOPE(hash::hash(hash)) {
UNORDERED_EPOINT("Mock hash copy constructor.");
}
}
hash& operator=(hash const& x)
{
UNORDERED_SCOPE(hash::operator=(hash)) {
UNORDERED_EPOINT("Mock hash assign operator 1.");
tag_ = x.tag_;
UNORDERED_EPOINT("Mock hash assign operator 2.");
}
return *this;
}
std::size_t operator()(object const& x) const {
UNORDERED_SCOPE(hash::operator()(object)) {
UNORDERED_EPOINT("Mock hash function.");
}
switch(tag_) {
case 1:
return x.tag1_;
case 2:
return x.tag2_;
default:
return x.tag1_ + x.tag2_;
}
}
friend bool operator==(hash const& x1, hash const& x2) {
UNORDERED_SCOPE(operator==(hash, hash)) {
UNORDERED_EPOINT("Mock hash equality function.");
}
return x1.tag_ == x2.tag_;
}
friend bool operator!=(hash const& x1, hash const& x2) {
UNORDERED_SCOPE(hash::operator!=(hash, hash)) {
UNORDERED_EPOINT("Mock hash inequality function.");
}
return x1.tag_ != x2.tag_;
}
};
class equal_to
{
int tag_;
public:
equal_to(int t = 0) : tag_(t)
{
UNORDERED_SCOPE(equal_to::equal_to()) {
UNORDERED_EPOINT("Mock equal_to default constructor.");
}
}
equal_to(equal_to const& x)
: tag_(x.tag_)
{
UNORDERED_SCOPE(equal_to::equal_to(equal_to)) {
UNORDERED_EPOINT("Mock equal_to copy constructor.");
}
}
equal_to& operator=(equal_to const& x)
{
UNORDERED_SCOPE(equal_to::operator=(equal_to)) {
UNORDERED_EPOINT("Mock equal_to assign operator 1.");
tag_ = x.tag_;
UNORDERED_EPOINT("Mock equal_to assign operator 2.");
}
return *this;
}
bool operator()(object const& x1, object const& x2) const {
UNORDERED_SCOPE(equal_to::operator()(object, object)) {
UNORDERED_EPOINT("Mock equal_to function.");
}
switch(tag_) {
case 1:
return x1.tag1_ == x2.tag1_;
case 2:
return x1.tag2_ == x2.tag2_;
default:
return x1 == x2;
}
}
friend bool operator==(equal_to const& x1, equal_to const& x2) {
UNORDERED_SCOPE(operator==(equal_to, equal_to)) {
UNORDERED_EPOINT("Mock equal_to equality function.");
}
return x1.tag_ == x2.tag_;
}
friend bool operator!=(equal_to const& x1, equal_to const& x2) {
UNORDERED_SCOPE(operator!=(equal_to, equal_to)) {
UNORDERED_EPOINT("Mock equal_to inequality function.");
}
return x1.tag_ != x2.tag_;
}
};
template <class T>
class allocator
{
public:
int tag_;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef T const* const_pointer;
typedef T& reference;
typedef T const& const_reference;
typedef T value_type;
template <class U> struct rebind { typedef allocator<U> other; };
explicit allocator(int t = 0) : tag_(t)
{
UNORDERED_SCOPE(allocator::allocator()) {
UNORDERED_EPOINT("Mock allocator default constructor.");
}
detail::tracker.allocator_ref();
}
template <class Y> allocator(allocator<Y> const& x) : tag_(x.tag_)
{
UNORDERED_SCOPE(allocator::allocator()) {
UNORDERED_EPOINT("Mock allocator template copy constructor.");
}
detail::tracker.allocator_ref();
}
allocator(allocator const& x) : tag_(x.tag_)
{
UNORDERED_SCOPE(allocator::allocator()) {
UNORDERED_EPOINT("Mock allocator copy constructor.");
}
detail::tracker.allocator_ref();
}
~allocator() {
detail::tracker.allocator_unref();
}
allocator& operator=(allocator const& x) {
UNORDERED_SCOPE(allocator::allocator()) {
UNORDERED_EPOINT("Mock allocator assignment operator.");
tag_ = x.tag_;
}
return *this;
}
// If address throws, then it can't be used in erase or the
// destructor, which is very limiting. I need to check up on
// this.
pointer address(reference r) {
//UNORDERED_SCOPE(allocator::address(reference)) {
// UNORDERED_EPOINT("Mock allocator address function.");
//}
return pointer(&r);
}
const_pointer address(const_reference r) {
//UNORDERED_SCOPE(allocator::address(const_reference)) {
// UNORDERED_EPOINT("Mock allocator const address function.");
//}
return const_pointer(&r);
}
pointer allocate(size_type n) {
T* ptr = 0;
UNORDERED_SCOPE(allocator::allocate(size_type)) {
UNORDERED_EPOINT("Mock allocator allocate function.");
using namespace std;
ptr = (T*) malloc(n * sizeof(T));
if(!ptr) throw std::bad_alloc();
}
detail::tracker.track_allocate((void*) ptr, n, sizeof(T), tag_);
return pointer(ptr);
//return pointer(static_cast<T*>(::operator new(n * sizeof(T))));
}
pointer allocate(size_type n, void const* u)
{
T* ptr = 0;
UNORDERED_SCOPE(allocator::allocate(size_type, const_pointer)) {
UNORDERED_EPOINT("Mock allocator allocate function.");
using namespace std;
ptr = (T*) malloc(n * sizeof(T));
if(!ptr) throw std::bad_alloc();
}
detail::tracker.track_allocate((void*) ptr, n, sizeof(T), tag_);
return pointer(ptr);
//return pointer(static_cast<T*>(::operator new(n * sizeof(T))));
}
void deallocate(pointer p, size_type n)
{
//::operator delete((void*) p);
if(p) {
detail::tracker.track_deallocate((void*) p, n, sizeof(T), tag_);
using namespace std;
free(p);
}
}
void construct(pointer p, T const& t) {
UNORDERED_SCOPE(allocator::construct(pointer, T)) {
UNORDERED_EPOINT("Mock allocator construct function.");
new(p) T(t);
}
detail::tracker.track_construct((void*) p, sizeof(T), tag_);
}
#if defined(BOOST_UNORDERED_STD_FORWARD)
template<class... Args> void construct(pointer p, Args&&... args) {
UNORDERED_SCOPE(allocator::construct(pointer, Args&&...)) {
UNORDERED_EPOINT("Mock allocator construct function.");
new(p) T(std::forward<Args>(args)...);
}
detail::tracker.track_construct((void*) p, sizeof(T), tag_);
}
#endif
void destroy(pointer p) {
detail::tracker.track_destroy((void*) p, sizeof(T), tag_);
p->~T();
}
size_type max_size() const {
UNORDERED_SCOPE(allocator::construct(pointer, T)) {
UNORDERED_EPOINT("Mock allocator max_size function.");
}
return (std::numeric_limits<std::size_t>::max)();
}
};
template <class T>
void swap(allocator<T>& x, allocator<T>& y)
{
std::swap(x.tag_, y.tag_);
}
// It's pretty much impossible to write a compliant swap when these
// two can throw. So they don't.
template <class T>
inline bool operator==(allocator<T> const& x, allocator<T> const& y)
{
//UNORDERED_SCOPE(operator==(allocator, allocator)) {
// UNORDERED_EPOINT("Mock allocator equality operator.");
//}
return x.tag_ == y.tag_;
}
template <class T>
inline bool operator!=(allocator<T> const& x, allocator<T> const& y)
{
//UNORDERED_SCOPE(operator!=(allocator, allocator)) {
// UNORDERED_EPOINT("Mock allocator inequality operator.");
//}
return x.tag_ != y.tag_;
}
}
}
// Workaround for ADL deficient compilers
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
namespace test
{
test::exception::object generate(test::exception::object const* x) {
return test::exception::generate(x);
}
}
#endif
#endif
|