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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
|
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2019 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
/******************************************************************************
Copyright (C) 2017 by Sergey A Kryukov: derived work
http://www.SAKryukov.org
http://www.codeproject.com/Members/SAKryukov
Based on original work by Sergey Ryazanov:
"The Impossibly Fast C++ Delegates", 18 Jul 2005
https://www.codeproject.com/articles/11015/the-impossibly-fast-c-delegates
MIT license:
http://en.wikipedia.org/wiki/MIT_License
Original publication: https://www.codeproject.com/Articles/1170503/The-Impossibly-Fast-Cplusplus-Delegates-Fixed
******************************************************************************/
#ifndef ETL_DELEGATE_CPP11_INCLUDED
#define ETL_DELEGATE_CPP11_INCLUDED
#include "../platform.h"
#include "../error_handler.h"
#include "../exception.h"
#include "../type_traits.h"
#include "../utility.h"
#include "../optional.h"
namespace etl
{
//***************************************************************************
/// The base class for delegate exceptions.
//***************************************************************************
class delegate_exception : public exception
{
public:
delegate_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
/// The exception thrown when the delegate is uninitialised.
//***************************************************************************
class delegate_uninitialised : public delegate_exception
{
public:
delegate_uninitialised(string_type file_name_, numeric_type line_number_)
: delegate_exception(ETL_ERROR_TEXT("delegate:uninitialised", ETL_DELEGATE_FILE_ID"A"), file_name_, line_number_)
{
}
};
//*************************************************************************
/// Declaration.
//*************************************************************************
template <typename T> class delegate;
//*************************************************************************
/// Specialisation.
//*************************************************************************
template <typename TReturn, typename... TParams>
class delegate<TReturn(TParams...)> final
{
public:
//*************************************************************************
/// Default constructor.
//*************************************************************************
ETL_CONSTEXPR14 delegate()
{
}
//*************************************************************************
// Copy constructor.
//*************************************************************************
ETL_CONSTEXPR14 delegate(const delegate& other) = default;
//*************************************************************************
// Construct from lambda or functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
ETL_CONSTEXPR14 delegate(TLambda& instance)
{
assign((void*)(&instance), lambda_stub<TLambda>);
}
//*************************************************************************
// Construct from const lambda or functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
ETL_CONSTEXPR14 delegate(const TLambda& instance)
{
assign((void*)(&instance), const_lambda_stub<TLambda>);
}
//*************************************************************************
/// Create from function (Compile time).
//*************************************************************************
template <TReturn(*Method)(TParams...)>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create()
{
return delegate(ETL_NULLPTR, function_stub<Method>);
}
//*************************************************************************
/// Create from Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create(TLambda& instance)
{
return delegate((void*)(&instance), lambda_stub<TLambda>);
}
//*************************************************************************
/// Create from const Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create(const TLambda& instance)
{
return delegate((void*)(&instance), const_lambda_stub<TLambda>);
}
//*************************************************************************
/// Create from instance method (Run time).
//*************************************************************************
template <typename T, TReturn(T::*Method)(TParams...)>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create(T& instance)
{
return delegate((void*)(&instance), method_stub<T, Method>);
}
//*************************************************************************
/// Create from instance method (Run time).
/// Deleted for rvalue references.
//*************************************************************************
template <typename T, TReturn(T::*Method)(TParams...)>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create(T&& instance) = delete;
//*************************************************************************
/// Create from const instance method (Run time).
//*************************************************************************
template <typename T, TReturn(T::*Method)(TParams...) const>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create(const T& instance)
{
return delegate((void*)(&instance), const_method_stub<T, Method>);
}
//*************************************************************************
/// Disable create from rvalue instance method (Run time).
//*************************************************************************
template <typename T, TReturn(T::*Method)(TParams...) const>
static ETL_CONSTEXPR14 delegate create(T&& instance) = delete;
//*************************************************************************
/// Create from instance method (Compile time).
//*************************************************************************
template <typename T, T& Instance, TReturn(T::*Method)(TParams...)>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create()
{
return delegate(method_instance_stub<T, Method, Instance>);
}
//*************************************************************************
/// Create from instance method (Compile time).
/// New API
//*************************************************************************
template <typename T, TReturn(T::* Method)(TParams...), T& Instance>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create()
{
return delegate(method_instance_stub<T, Method, Instance>);
}
//*************************************************************************
/// Create from const instance method (Compile time).
//*************************************************************************
template <typename T, T const& Instance, TReturn(T::*Method)(TParams...) const>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create()
{
return delegate(const_method_instance_stub<T, Method, Instance>);
}
//*************************************************************************
/// Create from const instance method (Compile time).
/// New API
//*************************************************************************
template <typename T, TReturn(T::* Method)(TParams...) const, T const& Instance>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create()
{
return delegate(const_method_instance_stub<T, Method, Instance>);
}
#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
//*************************************************************************
/// Create from instance function operator (Compile time).
/// At the time of writing, GCC appears to have trouble with this.
//*************************************************************************
template <typename T, T& Instance>
ETL_NODISCARD
static ETL_CONSTEXPR14 delegate create()
{
return delegate(operator_instance_stub<T, Instance>);
}
#endif
//*************************************************************************
/// Set from function (Compile time).
//*************************************************************************
template <TReturn(*Method)(TParams...)>
ETL_CONSTEXPR14 void set()
{
assign(ETL_NULLPTR, function_stub<Method>);
}
//*************************************************************************
/// Set from Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
ETL_CONSTEXPR14 void set(TLambda& instance)
{
assign((void*)(&instance), lambda_stub<TLambda>);
}
//*************************************************************************
/// Set from const Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
ETL_CONSTEXPR14 void set(const TLambda& instance)
{
assign((void*)(&instance), const_lambda_stub<TLambda>);
}
//*************************************************************************
/// Set from instance method (Run time).
//*************************************************************************
template <typename T, TReturn(T::* Method)(TParams...)>
ETL_CONSTEXPR14 void set(T& instance)
{
assign((void*)(&instance), method_stub<T, Method>);
}
//*************************************************************************
/// Set from const instance method (Run time).
//*************************************************************************
template <typename T, TReturn(T::* Method)(TParams...) const>
ETL_CONSTEXPR14 void set(T& instance)
{
assign((void*)(&instance), const_method_stub<T, Method>);
}
//*************************************************************************
/// Set from instance method (Compile time).
//*************************************************************************
template <typename T, T& Instance, TReturn(T::* Method)(TParams...)>
ETL_CONSTEXPR14 void set()
{
assign(ETL_NULLPTR, method_instance_stub<T, Method, Instance>);
}
//*************************************************************************
/// Set from instance method (Compile time).
/// New API
//*************************************************************************
template <typename T, TReturn(T::* Method)(TParams...), T& Instance>
ETL_CONSTEXPR14 void set()
{
assign(ETL_NULLPTR, method_instance_stub<T, Method, Instance>);
}
//*************************************************************************
/// Set from const instance method (Compile time).
//*************************************************************************
template <typename T, T const& Instance, TReturn(T::* Method)(TParams...) const>
ETL_CONSTEXPR14 void set()
{
assign(ETL_NULLPTR, const_method_instance_stub<T, Method, Instance>);
}
//*************************************************************************
/// Set from const instance method (Compile time).
/// New API
//*************************************************************************
template <typename T, TReturn(T::* Method)(TParams...) const, T const& Instance>
ETL_CONSTEXPR14 void set()
{
assign(ETL_NULLPTR, const_method_instance_stub<T, Method, Instance>);
}
//*************************************************************************
/// Clear the delegate.
//*************************************************************************
ETL_CONSTEXPR14 void clear()
{
invocation.clear();
}
//*************************************************************************
/// Execute the delegate.
//*************************************************************************
TReturn operator()(TParams... args) const
{
ETL_ASSERT(is_valid(), ETL_ERROR(delegate_uninitialised));
return (*invocation.stub)(invocation.object, etl::forward<TParams>(args)...);
}
//*************************************************************************
/// Execute the delegate if valid.
/// 'void' return.
//*************************************************************************
template <typename TRet = TReturn>
ETL_CONSTEXPR14
typename etl::enable_if_t<etl::is_same<TRet, void>::value, bool>
call_if(TParams... args) const
{
if (is_valid())
{
(*invocation.stub)(invocation.object, etl::forward<TParams>(args)...);
return true;
}
else
{
return false;
}
}
//*************************************************************************
/// Execute the delegate if valid.
/// Non 'void' return.
//*************************************************************************
template <typename TRet = TReturn>
ETL_CONSTEXPR14
typename etl::enable_if_t<!etl::is_same<TRet, void>::value, etl::optional<TReturn>>
call_if(TParams... args) const
{
etl::optional<TReturn> result;
if (is_valid())
{
result = (*invocation.stub)(invocation.object, etl::forward<TParams>(args)...);
}
return result;
}
//*************************************************************************
/// Execute the delegate if valid or call alternative.
/// Run time alternative.
//*************************************************************************
template <typename TAlternative>
TReturn call_or(TAlternative alternative, TParams... args) const
{
if (is_valid())
{
return (*invocation.stub)(invocation.object, etl::forward<TParams>(args)...);
}
else
{
return alternative(etl::forward<TParams>(args)...);
}
}
//*************************************************************************
/// Execute the delegate if valid or call alternative.
/// Compile time alternative.
//*************************************************************************
template <TReturn(*Method)(TParams...)>
TReturn call_or(TParams... args) const
{
if (is_valid())
{
return (*invocation.stub)(invocation.object, etl::forward<TParams>(args)...);
}
else
{
return (Method)(etl::forward<TParams>(args)...);
}
}
//*************************************************************************
/// Assignment
//*************************************************************************
delegate& operator =(const delegate& rhs) = default;
//*************************************************************************
/// Create from Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
ETL_CONSTEXPR14 delegate& operator =(TLambda& instance)
{
assign((void*)(&instance), lambda_stub<TLambda>);
return *this;
}
//*************************************************************************
/// Create from const Lambda or Functor.
//*************************************************************************
template <typename TLambda, typename = etl::enable_if_t<etl::is_class<TLambda>::value && !etl::is_same<etl::delegate<TReturn(TParams...)>, TLambda>::value, void>>
ETL_CONSTEXPR14 delegate& operator =(const TLambda& instance)
{
assign((void*)(&instance), const_lambda_stub<TLambda>);
return *this;
}
//*************************************************************************
/// Checks equality.
//*************************************************************************
ETL_CONSTEXPR14 bool operator == (const delegate& rhs) const
{
return invocation == rhs.invocation;
}
//*************************************************************************
/// Returns <b>true</b> if the delegate is valid.
//*************************************************************************
ETL_CONSTEXPR14 bool operator != (const delegate& rhs) const
{
return invocation != rhs.invocation;
}
//*************************************************************************
/// Returns <b>true</b> if the delegate is valid.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR14 bool is_valid() const
{
return invocation.stub != ETL_NULLPTR;
}
//*************************************************************************
/// Returns <b>true</b> if the delegate is valid.
//*************************************************************************
ETL_CONSTEXPR14 operator bool() const
{
return is_valid();
}
private:
using stub_type = TReturn(*)(void* object, TParams...);
//*************************************************************************
/// The internal invocation object.
//*************************************************************************
struct invocation_element
{
invocation_element() = default;
//***********************************************************************
ETL_CONSTEXPR14 invocation_element(void* object_, stub_type stub_)
: object(object_)
, stub(stub_)
{
}
//***********************************************************************
ETL_CONSTEXPR14 bool operator ==(const invocation_element& rhs) const
{
return (rhs.stub == stub) && (rhs.object == object);
}
//***********************************************************************
ETL_CONSTEXPR14 bool operator !=(const invocation_element& rhs) const
{
return (rhs.stub != stub) || (rhs.object != object);
}
//***********************************************************************
ETL_CONSTEXPR14 void clear()
{
object = ETL_NULLPTR;
stub = ETL_NULLPTR;
}
//***********************************************************************
void* object = ETL_NULLPTR;
stub_type stub = ETL_NULLPTR;
};
//*************************************************************************
/// Constructs a delegate from an object and stub.
//*************************************************************************
ETL_CONSTEXPR14 delegate(void* object, stub_type stub)
: invocation(object, stub)
{
}
//*************************************************************************
/// Constructs a delegate from a stub.
//*************************************************************************
ETL_CONSTEXPR14 delegate(stub_type stub)
: invocation(ETL_NULLPTR, stub)
{
}
//*************************************************************************
/// Assign from an object and stub.
//*************************************************************************
ETL_CONSTEXPR14 void assign(void* object, stub_type stub)
{
invocation.object = object;
invocation.stub = stub;
}
//*************************************************************************
/// Stub call for a member function. Run time instance.
//*************************************************************************
template <typename T, TReturn(T::*Method)(TParams...)>
static ETL_CONSTEXPR14 TReturn method_stub(void* object, TParams... params)
{
T* p = static_cast<T*>(object);
return (p->*Method)(etl::forward<TParams>(params)...);
}
//*************************************************************************
/// Stub call for a const member function. Run time instance.
//*************************************************************************
template <typename T, TReturn(T::*Method)(TParams...) const>
static ETL_CONSTEXPR14 TReturn const_method_stub(void* object, TParams... params)
{
T* const p = static_cast<T*>(object);
return (p->*Method)(etl::forward<TParams>(params)...);
}
//*************************************************************************
/// Stub call for a member function. Compile time instance.
//*************************************************************************
template <typename T, TReturn(T::*Method)(TParams...), T& Instance>
static ETL_CONSTEXPR14 TReturn method_instance_stub(void*, TParams... params)
{
return (Instance.*Method)(etl::forward<TParams>(params)...);
}
//*************************************************************************
/// Stub call for a const member function. Compile time instance.
//*************************************************************************
template <typename T, TReturn(T::*Method)(TParams...) const, const T& Instance>
static ETL_CONSTEXPR14 TReturn const_method_instance_stub(void*, TParams... params)
{
return (Instance.*Method)(etl::forward<TParams>(params)...);
}
#if !(defined(ETL_COMPILER_GCC) && (__GNUC__ <= 8))
//*************************************************************************
/// Stub call for a function operator. Compile time instance.
//*************************************************************************
template <typename T, T& Instance>
static ETL_CONSTEXPR14 TReturn operator_instance_stub(void*, TParams... params)
{
return Instance.operator()(etl::forward<TParams>(params)...);
}
#endif
//*************************************************************************
/// Stub call for a free function.
//*************************************************************************
template <TReturn(*Method)(TParams...)>
static ETL_CONSTEXPR14 TReturn function_stub(void*, TParams... params)
{
return (Method)(etl::forward<TParams>(params)...);
}
//*************************************************************************
/// Stub call for a lambda or functor function.
//*************************************************************************
template <typename TLambda>
static ETL_CONSTEXPR14 TReturn lambda_stub(void* object, TParams... arg)
{
TLambda* p = static_cast<TLambda*>(object);
return (p->operator())(etl::forward<TParams>(arg)...);
}
//*************************************************************************
/// Stub call for a const lambda or functor function.
//*************************************************************************
template <typename TLambda>
static ETL_CONSTEXPR14 TReturn const_lambda_stub(void* object, TParams... arg)
{
const TLambda* p = static_cast<const TLambda*>(object);
return (p->operator())(etl::forward<TParams>(arg)...);
}
//*************************************************************************
/// The invocation object.
//*************************************************************************
invocation_element invocation;
};
}
#endif
|