File size: 17,219 Bytes
9375c9a |
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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
// Copyright (C) 2011 Davis E. King ([email protected])
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_ANY_FUNCTION_RETURN
#error "You aren't supposed to directly #include this file. #include <dlib/any.h> instead."
#endif
#ifdef _MSC_VER
// When using visual studio 2012, disable the warning "warning C4180: qualifier applied to function type has no meaning; ignored"
// that you get about some template expansions applying & to function types.
#pragma warning(disable : 4180)
#endif
#ifdef DLIB_ANY_FUNCTION_RETURN
// This file contains the body of the any_function class. We use the
// preprocessor to generate many different versions. There are
// versions which return a value and those which return void. For
// each of these types there are versions with differing numbers
// of arguments.
public:
typedef typename sig_traits<function_type>::result_type result_type;
typedef typename sig_traits<function_type>::arg1_type arg1_type;
typedef typename sig_traits<function_type>::arg2_type arg2_type;
typedef typename sig_traits<function_type>::arg3_type arg3_type;
typedef typename sig_traits<function_type>::arg4_type arg4_type;
typedef typename sig_traits<function_type>::arg5_type arg5_type;
typedef typename sig_traits<function_type>::arg6_type arg6_type;
typedef typename sig_traits<function_type>::arg7_type arg7_type;
typedef typename sig_traits<function_type>::arg8_type arg8_type;
typedef typename sig_traits<function_type>::arg9_type arg9_type;
typedef typename sig_traits<function_type>::arg10_type arg10_type;
const static unsigned long num_args = sig_traits<function_type>::num_args;
any_function()
{
}
any_function (
const any_function& item
)
{
if (item.data)
{
item.data->copy_to(data);
}
}
template <typename T>
any_function (
const T& item
)
{
typedef typename basic_type<T>::type U;
data.reset(new derived<U,function_type>(item));
}
void clear (
)
{
data.reset();
}
template <typename T>
bool contains (
) const
{
typedef typename basic_type<T>::type U;
return dynamic_cast<derived<U,function_type>*>(data.get()) != 0;
}
bool is_empty(
) const
{
return data.get() == 0;
}
bool is_set(
) const
{
return !is_empty();
}
template <typename T>
T& cast_to(
)
{
typedef typename basic_type<T>::type U;
derived<U,function_type>* d = dynamic_cast<derived<U,function_type>*>(data.get());
if (d == 0)
{
throw bad_any_cast();
}
return d->item;
}
template <typename T>
const T& cast_to(
) const
{
typedef typename basic_type<T>::type U;
derived<U,function_type>* d = dynamic_cast<derived<U,function_type>*>(data.get());
if (d == 0)
{
throw bad_any_cast();
}
return d->item;
}
template <typename T>
T& get(
)
{
typedef typename basic_type<T>::type U;
derived<U,function_type>* d = dynamic_cast<derived<U,function_type>*>(data.get());
if (d == 0)
{
d = new derived<U,function_type>();
data.reset(d);
}
return d->item;
}
any_function& operator= (
const any_function& item
)
{
any_function(item).swap(*this);
return *this;
}
void swap (
any_function& item
)
{
data.swap(item.data);
}
result_type operator()(DLIB_ANY_FUNCTION_ARG_LIST) const
{ validate(); DLIB_ANY_FUNCTION_RETURN data->evaluate(DLIB_ANY_FUNCTION_ARGS); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to call a dlib::any_function but you have supplied
arguments which don't match the function signature used by the
dlib::any_function.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
private:
void validate () const
{
// make sure requires clause is not broken
DLIB_ASSERT(is_empty() == false,
"\t result_type any_function::operator()"
<< "\n\t You can't call operator() on an empty any_function"
<< "\n\t this: " << this
);
}
template <typename FT>
struct Tbase
{
virtual ~Tbase() {}
virtual result_type evaluate () const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1
>
struct Tbase<T (A1)>
{
virtual ~Tbase() {}
virtual T evaluate ( A1) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2
>
struct Tbase<T (A1,A2)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2, typename A3
>
struct Tbase<T (A1,A2,A3)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2, typename A3,
typename A4
>
struct Tbase<T (A1,A2,A3,A4)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2, typename A3,
typename A4, typename A5
>
struct Tbase<T (A1,A2,A3,A4,A5)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6
>
struct Tbase<T (A1,A2,A3,A4,A5,A6)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6,
typename A7
>
struct Tbase<T (A1,A2,A3,A4,A5,A6,A7)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6,A7) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6,
typename A7, typename A8
>
struct Tbase<T (A1,A2,A3,A4,A5,A6,A7,A8)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6,A7,A8) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6,
typename A7, typename A8, typename A9
>
struct Tbase<T (A1,A2,A3,A4,A5,A6,A7,A8,A9)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6,A7,A8,A9) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
template <
typename T,
typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6,
typename A7, typename A8, typename A9,
typename A10
>
struct Tbase<T (A1,A2,A3,A4,A5,A6,A7,A8,A9,A10)>
{
virtual ~Tbase() {}
virtual T evaluate (A1,A2,A3,A4,A5,A6,A7,A8,A9,A10) const = 0;
virtual void copy_to ( std::unique_ptr<Tbase>& dest) const = 0;
};
typedef Tbase<function_type> base;
// -----------------------------------------------
// Some templates to help deal with the weirdness of storing C function types (rather than pointer to functions).
// Basically, we make sure things always get turned into function pointers even if the user gives a function reference.
template <typename T, typename enabled = void>
struct funct_type { typedef T type; };
template <typename T>
struct funct_type<T, typename enable_if<is_function<T> >::type> { typedef T* type; };
template <typename T>
static typename enable_if<is_function<T>,const T*>::type copy (const T& item) { return &item; }
template <typename T>
static typename disable_if<is_function<T>,const T&>::type copy (const T& item) { return item; }
template <typename T, typename U>
static typename enable_if<is_function<T>,const T&>::type deref (const U& item) { return *item; }
template <typename T, typename U>
static typename disable_if<is_function<T>,const T&>::type deref (const U& item) { return item; }
// -----------------------------------------------
#define DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE \
typename funct_type<T>::type item; \
derived() {} \
derived(const T& val) : item(copy(val)) {} \
virtual void copy_to ( std::unique_ptr<base>& dest) const \
{ dest.reset(new derived(deref<T>(item))); }
template <typename T, typename FT>
struct derived : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
) const { DLIB_ANY_FUNCTION_RETURN item(); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1>
struct derived<T,result_type (A1)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1
) const { DLIB_ANY_FUNCTION_RETURN item(a1); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2>
struct derived<T,result_type (A1,A2)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2, typename A3>
struct derived<T,result_type (A1,A2,A3)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2, A3 a3
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2,a3); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2, typename A3,
typename A4>
struct derived<T,result_type (A1,A2,A3,A4)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2, A3 a3, A4 a4
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2,a3,a4); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2, typename A3,
typename A4, typename A5>
struct derived<T,result_type (A1,A2,A3,A4,A5)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2, A3 a3, A4 a4, A5 a5
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2,a3,a4,a5); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6>
struct derived<T,result_type (A1,A2,A3,A4,A5,A6)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2,a3,a4,a5,a6); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6,
typename A7>
struct derived<T,result_type (A1,A2,A3,A4,A5,A6,A7)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2,a3,a4,a5,a6,a7); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6,
typename A7, typename A8>
struct derived<T,result_type (A1,A2,A3,A4,A5,A6,A7,A8)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2,a3,a4,a5,a6,a7,a8); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6,
typename A7, typename A8, typename A9>
struct derived<T,result_type (A1,A2,A3,A4,A5,A6,A7,A8,A9)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2,a3,a4,a5,a6,a7,a8,a9); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
template <typename T, typename A1, typename A2, typename A3,
typename A4, typename A5, typename A6,
typename A7, typename A8, typename A9,
typename A10>
struct derived<T,result_type (A1,A2,A3,A4,A5,A6,A7,A8,A9,A10)> : public base
{
DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
virtual result_type evaluate (
A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10
) const { DLIB_ANY_FUNCTION_RETURN item(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10); }
/* !!!!!!!! ERRORS ON THE ABOVE LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you are getting an error on the above line then it means you
have attempted to assign a function or function object to a
dlib::any_function but the signatures of the source and
destination functions don't match.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
std::unique_ptr<base> data;
#undef DLIB_ANY_FUNCTION_DERIVED_BOILERPLATE
#endif // DLIB_ANY_FUNCTION_RETURN
|