| 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 |
#ifndef XSENS_JANITORS_H |
| 34 |
#define XSENS_JANITORS_H |
| 35 |
|
| 36 |
#ifndef __AVR32__ |
| 37 |
#include <functional> |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
#if defined(__AVR32__) |
| 42 |
# define __cdecl |
| 43 |
# define __stdcall |
| 44 |
#elif defined(_ADI_COMPILER) |
| 45 |
# define __cdecl |
| 46 |
# define __stdcall |
| 47 |
#elif defined(__GNUC__) && !defined(HAVE_CDECL) |
| 48 |
# if !defined(__cdecl) |
| 49 |
# if defined(__x86_64__) |
| 50 |
# define __cdecl |
| 51 |
# else |
| 52 |
# define __cdecl __attribute__((cdecl)) |
| 53 |
# endif |
| 54 |
# endif |
| 55 |
# if !defined(__stdcall) |
| 56 |
# if defined(__x86_64__) |
| 57 |
# define __stdcall |
| 58 |
# else |
| 59 |
# define __stdcall __attribute__((stdcall)) |
| 60 |
#endif |
| 61 |
# endif |
| 62 |
#endif |
| 63 |
|
| 64 |
namespace xsens { |
| 65 |
|
| 66 |
#ifndef NOT_FOR_PUBLIC_RELEASE |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
template <class T> |
| 73 |
class JanitorRestore { |
| 74 |
private: |
| 75 |
T& m_control; |
| 76 |
T m_value; |
| 77 |
bool m_enabled; |
| 78 |
public: |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
JanitorRestore<T>(T& control, bool enabl = true) : |
| 83 |
m_control(control), m_value(control), m_enabled(enabl) {} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
~JanitorRestore() |
| 88 |
{ |
| 89 |
if (m_enabled) |
| 90 |
m_control = m_value; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void disable(void) |
| 96 |
{ m_enabled = false; } |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void enable(void) |
| 101 |
{ m_enabled = true; } |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
template <class T> |
| 110 |
class JanitorFree { |
| 111 |
private: |
| 112 |
T* m_control; |
| 113 |
bool m_enabled; |
| 114 |
public: |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
JanitorFree<T>(T* control, bool enabl = true) : |
| 119 |
m_control(control), m_enabled(enabl) {} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
~JanitorFree() |
| 124 |
{ |
| 125 |
if (m_enabled) |
| 126 |
free(m_control); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
void disable(void) |
| 132 |
{ m_enabled = false; } |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
void enable(void) |
| 137 |
{ m_enabled = true; } |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
template <class T> |
| 146 |
class JanitorDelete { |
| 147 |
private: |
| 148 |
T* m_control; |
| 149 |
bool m_enabled; |
| 150 |
public: |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
JanitorDelete<T>() : |
| 155 |
m_control(NULL), |
| 156 |
m_enabled(true) |
| 157 |
{ |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
JanitorDelete<T>(T* control, bool enabl = true) : |
| 164 |
m_control(control), |
| 165 |
m_enabled(enabl) |
| 166 |
{ |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
~JanitorDelete() |
| 173 |
{ |
| 174 |
if (m_enabled) |
| 175 |
delete m_control; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
void setControl(T* control) |
| 182 |
{ m_control = control; } |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
void disable(void) |
| 187 |
{ m_enabled = false; } |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
void enable(void) |
| 192 |
{ m_enabled = true; } |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
const T *operator->() const { return m_control; } |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
T *operator->() { return m_control; } |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
template <class T> |
| 209 |
class JanitorDeleteNull { |
| 210 |
private: |
| 211 |
T*& m_control; |
| 212 |
bool m_enabled; |
| 213 |
public: |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
JanitorDeleteNull<T>(T*& control, bool enabl = true) : |
| 218 |
m_control(control), |
| 219 |
m_enabled(enabl) |
| 220 |
{ |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
~JanitorDeleteNull() |
| 227 |
{ |
| 228 |
if (m_enabled) |
| 229 |
{ |
| 230 |
delete m_control; |
| 231 |
m_control = 0; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
void disable(void) |
| 238 |
{ m_enabled = false; } |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
void enable(void) |
| 243 |
{ m_enabled = true; } |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
const T *operator->() const { return m_control; } |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
T *operator->() { return m_control; } |
| 252 |
}; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
template <class T> |
| 260 |
class JanitorDeleteArray { |
| 261 |
private: |
| 262 |
T* m_control; |
| 263 |
bool m_enabled; |
| 264 |
public: |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
JanitorDeleteArray<T>(T* control, bool enabl = true) : |
| 269 |
m_control(control), m_enabled(enabl) {} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
~JanitorDeleteArray() |
| 274 |
{ |
| 275 |
if (m_enabled) |
| 276 |
delete[] m_control; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
void disable(void) |
| 282 |
{ m_enabled = false; } |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
void enable(void) |
| 287 |
{ m_enabled = true; } |
| 288 |
}; |
| 289 |
#endif |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
template <class T, typename R = void> |
| 297 |
class JanitorClassFunc { |
| 298 |
public: |
| 299 |
typedef R (T::*t_func_JanitorClassFunc)(void); |
| 300 |
private: |
| 301 |
const JanitorClassFunc& operator = (const JanitorClassFunc&); |
| 302 |
|
| 303 |
T& m_control; |
| 304 |
t_func_JanitorClassFunc m_funcJCF; |
| 305 |
bool m_enabled; |
| 306 |
public: |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
JanitorClassFunc<T,R>(T& control, t_func_JanitorClassFunc func, bool enabl = true) : |
| 311 |
m_control(control), m_funcJCF(func), m_enabled(enabl) |
| 312 |
{ |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
~JanitorClassFunc() |
| 318 |
{ |
| 319 |
if (m_enabled) |
| 320 |
(m_control.*m_funcJCF)(); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
void disable(void) |
| 326 |
{ m_enabled = false; } |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
void enable(void) |
| 331 |
{ m_enabled = true; } |
| 332 |
}; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
template <class T, typename P1, typename R = void> |
| 340 |
class JanitorClassFuncP1 { |
| 341 |
public: |
| 342 |
typedef R (T::*t_func_JanitorClassFunc)(P1); |
| 343 |
private: |
| 344 |
const JanitorClassFuncP1& operator = (const JanitorClassFuncP1&); |
| 345 |
|
| 346 |
T& m_control; |
| 347 |
P1 m_param1; |
| 348 |
t_func_JanitorClassFunc m_funcJCF; |
| 349 |
bool m_enabled; |
| 350 |
public: |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
JanitorClassFuncP1<T,P1,R>(T& control, P1 p1, t_func_JanitorClassFunc func, bool enabl = true) : |
| 355 |
m_control(control), m_param1(p1), m_funcJCF(func), m_enabled(enabl) |
| 356 |
{ |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
~JanitorClassFuncP1() |
| 362 |
{ |
| 363 |
if (m_enabled) |
| 364 |
(m_control.*m_funcJCF)(m_param1); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
void disable(void) |
| 370 |
{ m_enabled = false; } |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
void enable(void) |
| 375 |
{ m_enabled = true; } |
| 376 |
}; |
| 377 |
|
| 378 |
#ifndef NOT_FOR_PUBLIC_RELEASE |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
template <typename ResultType = void> |
| 386 |
class JanitorFunc0 { |
| 387 |
public: |
| 388 |
typedef ResultType (__cdecl * t_func_JanitorFunc)(void); |
| 389 |
private: |
| 390 |
const JanitorFunc0& operator = (const JanitorFunc0&); |
| 391 |
t_func_JanitorFunc m_funcJF; |
| 392 |
bool m_enabled; |
| 393 |
public: |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
JanitorFunc0<ResultType>(t_func_JanitorFunc func, bool enabl = true) : |
| 398 |
m_funcJF(func), m_enabled(enabl) {} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
~JanitorFunc0() |
| 403 |
{ |
| 404 |
if (m_enabled) |
| 405 |
(*m_funcJF)(); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
void disable(void) |
| 411 |
{ m_enabled = false; } |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
void enable(void) |
| 416 |
{ m_enabled = true; } |
| 417 |
}; |
| 418 |
|
| 419 |
#ifndef __AVR32__ |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
template <typename ResultType = void> |
| 427 |
class JanitorStdFunc0 { |
| 428 |
public: |
| 429 |
typedef std::function<ResultType (void)> t_func_JanitorFunc; |
| 430 |
private: |
| 431 |
const JanitorStdFunc0& operator = (const JanitorStdFunc0&); |
| 432 |
t_func_JanitorFunc m_funcJF; |
| 433 |
bool m_enabled; |
| 434 |
public: |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
JanitorStdFunc0<ResultType>(t_func_JanitorFunc func, bool enabl = true) : |
| 439 |
m_funcJF(func), m_enabled(enabl) {} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
~JanitorStdFunc0() |
| 444 |
{ |
| 445 |
if (m_enabled) |
| 446 |
m_funcJF(); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
void disable(void) |
| 452 |
{ m_enabled = false; } |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
void enable(void) |
| 457 |
{ m_enabled = true; } |
| 458 |
}; |
| 459 |
#endif |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
template <class ParamType, typename ResultType = void> |
| 468 |
class JanitorFunc1 { |
| 469 |
public: |
| 470 |
typedef ResultType (__cdecl * t_func_JanitorFunc)(ParamType); |
| 471 |
private: |
| 472 |
const JanitorFunc1& operator = (const JanitorFunc1&); |
| 473 |
ParamType m_control; |
| 474 |
t_func_JanitorFunc m_funcJF; |
| 475 |
bool m_enabled; |
| 476 |
public: |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
JanitorFunc1<ParamType,ResultType>(t_func_JanitorFunc func, ParamType control, bool enabl = true) : |
| 481 |
m_control(control), m_funcJF(func), m_enabled(enabl) {} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
~JanitorFunc1() |
| 486 |
{ |
| 487 |
if (m_enabled) |
| 488 |
(*m_funcJF)(m_control); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
void disable(void) |
| 494 |
{ m_enabled = false; } |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
void enable(void) |
| 499 |
{ m_enabled = true; } |
| 500 |
}; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
template <class ParamType, typename ResultType = void> |
| 509 |
class JanitorFunc1R { |
| 510 |
public: |
| 511 |
typedef ResultType (__cdecl * t_func_JanitorFunc)(ParamType); |
| 512 |
private: |
| 513 |
const JanitorFunc1R& operator = (const JanitorFunc1R&); |
| 514 |
ParamType& m_control; |
| 515 |
t_func_JanitorFunc m_funcJF; |
| 516 |
bool m_enabled; |
| 517 |
public: |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
JanitorFunc1R<ParamType,ResultType>(t_func_JanitorFunc func, ParamType& control, bool enabl = true) : |
| 522 |
m_control(control), m_funcJF(func), m_enabled(enabl) {} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
~JanitorFunc1R() |
| 527 |
{ |
| 528 |
if (m_enabled) |
| 529 |
(*m_funcJF)(m_control); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
void disable(void) |
| 535 |
{ m_enabled = false; } |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
void enable(void) |
| 540 |
{ m_enabled = true; } |
| 541 |
}; |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
template <class Param1Type, class Param2Type, typename ResultType = void> |
| 550 |
class JanitorFunc2 { |
| 551 |
public: |
| 552 |
typedef ResultType (__cdecl * t_func_JanitorFunc)(Param1Type,Param2Type); |
| 553 |
private: |
| 554 |
const JanitorFunc2& operator = (const JanitorFunc2&); |
| 555 |
Param1Type m_control1; |
| 556 |
Param2Type m_control2; |
| 557 |
t_func_JanitorFunc m_funcJF; |
| 558 |
bool m_enabled; |
| 559 |
public: |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
JanitorFunc2<Param1Type,Param2Type,ResultType>(t_func_JanitorFunc func, Param1Type control1, Param2Type control2, bool enabl = true) : |
| 564 |
m_funcJF(func), m_control1(control1), m_control2(control2), m_enabled(enabl) {} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
~JanitorFunc2() |
| 569 |
{ |
| 570 |
if (m_enabled) |
| 571 |
(*m_funcJF)(m_control1,m_control2); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
void disable(void) |
| 577 |
{ m_enabled = false; } |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
void enable(void) |
| 582 |
{ m_enabled = true; } |
| 583 |
}; |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
template <class Param1Type, class Param2Type, typename ResultType = void> |
| 592 |
class JanitorFunc2R { |
| 593 |
public: |
| 594 |
typedef ResultType (__cdecl * t_func_JanitorFunc)(Param1Type,Param2Type); |
| 595 |
private: |
| 596 |
const JanitorFunc2R& operator = (const JanitorFunc2R&); |
| 597 |
Param1Type& m_control1; |
| 598 |
Param2Type& m_control2; |
| 599 |
t_func_JanitorFunc m_funcJF; |
| 600 |
bool m_enabled; |
| 601 |
public: |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
JanitorFunc2R<Param1Type,Param2Type,ResultType>(t_func_JanitorFunc func, Param1Type& control1, Param2Type& control2, bool enabl = true) : |
| 606 |
m_funcJF(func), m_control1(control1), m_control2(control2), m_enabled(enabl) {} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
~JanitorFunc2R() |
| 611 |
{ |
| 612 |
if (m_enabled) |
| 613 |
(*m_funcJF)(m_control1,m_control2); |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
void disable(void) |
| 619 |
{ m_enabled = false; } |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
void enable(void) |
| 624 |
{ m_enabled = true; } |
| 625 |
}; |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
template <class T, class C, typename R = void> |
| 634 |
class JanitorLogFunc { |
| 635 |
public: |
| 636 |
typedef R (__cdecl * t_func_JanitorLogFunc)(const char*, const char*, ...); |
| 637 |
private: |
| 638 |
const JanitorLogFunc& operator = (const JanitorLogFunc&); |
| 639 |
t_func_JanitorLogFunc m_funcJF; |
| 640 |
const char* m_filter; |
| 641 |
const char* m_str; |
| 642 |
const char* m_functionName; |
| 643 |
T& m_control; |
| 644 |
bool m_enabled; |
| 645 |
public: |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
JanitorLogFunc<T,C,R>(t_func_JanitorLogFunc func, const char *filter, const char *str, const char *functionName, T& control, bool enable = true) : |
| 650 |
m_funcJF(func), m_filter(filter), m_str(str), m_functionName(functionName), m_control(control), m_enabled(enable) |
| 651 |
{} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
JanitorLogFunc<T,C,R>(t_func_JanitorLogFunc func, const char *filter, const char* str, T& control, bool enable = true) : |
| 656 |
m_funcJF(func), m_filter(filter), m_str(str), m_functionName(0), m_control(control), m_enabled(enable) {} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
~JanitorLogFunc() |
| 661 |
{ |
| 662 |
if (m_enabled) |
| 663 |
{ |
| 664 |
if (m_functionName) |
| 665 |
(*m_funcJF)(m_filter, m_str, m_functionName, (C) m_control); |
| 666 |
else |
| 667 |
(*m_funcJF)(m_filter, m_str, (C) m_control); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
void disable(void) |
| 674 |
{ m_enabled = false; } |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
void enable(void) |
| 679 |
{ m_enabled = true; } |
| 680 |
}; |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
template <typename R = void> |
| 688 |
class JanitorSimpleLogFunc { |
| 689 |
public: |
| 690 |
typedef R (__cdecl * t_func_JanitorSimpleLogFunc)(const char*, const char*, ...); |
| 691 |
private: |
| 692 |
const JanitorSimpleLogFunc& operator = (const JanitorSimpleLogFunc&); |
| 693 |
t_func_JanitorSimpleLogFunc m_funcJF; |
| 694 |
const char* m_filter; |
| 695 |
const char* m_str; |
| 696 |
const char* m_functionName; |
| 697 |
bool m_enabled; |
| 698 |
public: |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
JanitorSimpleLogFunc<R>(t_func_JanitorSimpleLogFunc func, const char *filter, const char *str, const char *functionName, bool enable = true) : |
| 703 |
m_funcJF(func), m_filter(filter), m_str(str), m_functionName(functionName), m_enabled(enable) |
| 704 |
{} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
JanitorSimpleLogFunc<R>(t_func_JanitorSimpleLogFunc func, const char *filter, const char* str, bool enable = true) : |
| 709 |
m_funcJF(func), m_filter(filter), m_str(str), m_functionName(0), m_enabled(enable) {} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
~JanitorSimpleLogFunc() |
| 714 |
{ |
| 715 |
if (m_enabled) |
| 716 |
{ |
| 717 |
if (m_functionName) |
| 718 |
(*m_funcJF)(m_filter, m_str, m_functionName); |
| 719 |
else |
| 720 |
(*m_funcJF)(m_filter, m_str); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
void disable(void) |
| 727 |
{ m_enabled = false; } |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
void enable(void) |
| 732 |
{ m_enabled = true; } |
| 733 |
}; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
template <class ParamType, typename ResultType = void> |
| 742 |
class JanitorFuncStdCall { |
| 743 |
public: |
| 744 |
typedef ResultType (__stdcall * t_func_JanitorFuncStdCall)(ParamType); |
| 745 |
private: |
| 746 |
const JanitorFuncStdCall& operator = (const JanitorFuncStdCall&); |
| 747 |
ParamType& m_control; |
| 748 |
t_func_JanitorFuncStdCall m_funcJFSC; |
| 749 |
bool m_enabled; |
| 750 |
public: |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
JanitorFuncStdCall<ParamType,ResultType>(t_func_JanitorFuncStdCall func, ParamType& control, bool enabl = true) : |
| 755 |
m_funcJFSC(func), m_control(control), m_enabled(enabl) {} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
~JanitorFuncStdCall() |
| 760 |
{ |
| 761 |
if (m_enabled) |
| 762 |
(*m_funcJFSC)(m_control); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
void disable(void) |
| 768 |
{ m_enabled = false; } |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
void enable(void) |
| 773 |
{ m_enabled = true; } |
| 774 |
}; |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
template <class T> |
| 782 |
class JanitorSet { |
| 783 |
private: |
| 784 |
const JanitorSet& operator = (const JanitorSet&); |
| 785 |
T& m_control; |
| 786 |
T m_value; |
| 787 |
bool m_enabled; |
| 788 |
public: |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
JanitorSet<T>(T& control, const T& val, bool enabl = true) : |
| 793 |
m_control(control), m_value(val), m_enabled(enabl) {} |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
~JanitorSet() |
| 798 |
{ |
| 799 |
if (m_enabled) |
| 800 |
m_control = m_value; |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
void disable(void) |
| 806 |
{ m_enabled = false; } |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
void enable(void) |
| 811 |
{ m_enabled = true; } |
| 812 |
}; |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
template <typename T> |
| 821 |
class JanitorRCDeallocationPolicy_Delete |
| 822 |
{ |
| 823 |
public: |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
static void Deallocate(T* p) {delete p;} |
| 828 |
protected: |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
~JanitorRCDeallocationPolicy_Delete() {} |
| 833 |
}; |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
template <typename T> |
| 840 |
class JanitorRCDeallocationPolicy_Free |
| 841 |
{ |
| 842 |
public: |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
static void Deallocate(T* p) {free(p);} |
| 847 |
protected: |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
~JanitorRCDeallocationPolicy_Free() {} |
| 852 |
}; |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
template <class T, template <class> class DeallocationPolicy> |
| 859 |
class JanitorRCDeallocation { |
| 860 |
public: |
| 861 |
typedef JanitorRCDeallocation<T, DeallocationPolicy> ThisType; |
| 862 |
typedef T ElementType; |
| 863 |
typedef void (*PostDeallocateFunc)(); |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
JanitorRCDeallocation(ElementType *target = 0, bool enabled=true) |
| 868 |
: m_target(target) |
| 869 |
, m_refCounter(0) |
| 870 |
, m_enabled(enabled) |
| 871 |
{ |
| 872 |
addRef(); |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
JanitorRCDeallocation(const ThisType &j, bool enabled=true) |
| 878 |
: m_target(j.m_target) |
| 879 |
, m_refCounter(j.m_refCounter) |
| 880 |
, m_enabled(enabled) |
| 881 |
{ |
| 882 |
addRef(); |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
virtual ~JanitorRCDeallocation() { removeRef(); } |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
JanitorRCDeallocation& operator=(const JanitorRCDeallocation &rhs) |
| 894 |
{ |
| 895 |
if (m_target != rhs.m_target) { |
| 896 |
removeRef(); |
| 897 |
m_target = rhs.m_target; |
| 898 |
m_refCounter = rhs.m_refCounter; |
| 899 |
addRef(); |
| 900 |
} |
| 901 |
return *this; |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
ElementType* operator->() const |
| 906 |
{ |
| 907 |
return m_target; |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
ElementType& operator*() const |
| 913 |
{ |
| 914 |
return *m_target; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
void disable(void) |
| 920 |
{ |
| 921 |
m_enabled = false; |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
void enable(void) |
| 927 |
{ |
| 928 |
m_enabled = true; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
ElementType* get() const throw() |
| 934 |
{ |
| 935 |
return m_target; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
void swap(JanitorRCDeallocation& other) throw() |
| 942 |
{ |
| 943 |
std::swap(m_target, other.m_target); |
| 944 |
std::swap(m_refCounter, other.m_refCounter); |
| 945 |
std::swap(m_enabled, other.m_enabled); |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
void reset(ElementType* p = 0) throw() |
| 953 |
{ |
| 954 |
if (m_target != p) { |
| 955 |
ThisType(p).swap(*this); |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
private: |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
void addRef(void) |
| 965 |
{ |
| 966 |
if (m_target) { |
| 967 |
if (!m_refCounter) |
| 968 |
m_refCounter = new int(0); |
| 969 |
(*m_refCounter)++; |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
void removeRef(void) |
| 977 |
{ |
| 978 |
if (m_refCounter) { |
| 979 |
if (*m_refCounter > 1) |
| 980 |
(*m_refCounter)--; |
| 981 |
else { |
| 982 |
delete m_refCounter; |
| 983 |
DeallocationPolicy<ElementType>::Deallocate(m_target); |
| 984 |
} |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
ElementType *m_target; |
| 989 |
int *m_refCounter; |
| 990 |
bool m_enabled; |
| 991 |
}; |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
template <class T, template <class> class DeallocationPolicy = JanitorRCDeallocationPolicy_Free> |
| 996 |
class JanitorRCFree : public JanitorRCDeallocation<T, DeallocationPolicy> |
| 997 |
{ |
| 998 |
public: |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
JanitorRCFree(T *target = 0, bool enabled=true): JanitorRCDeallocation<T, DeallocationPolicy>(target, enabled) {} |
| 1003 |
}; |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
template <class T, template <class> class DeallocationPolicy = JanitorRCDeallocationPolicy_Delete> |
| 1008 |
class JanitorRCDelete : public JanitorRCDeallocation<T, DeallocationPolicy> |
| 1009 |
{ |
| 1010 |
public: |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
JanitorRCDelete(T *target = 0, bool enabled=true): JanitorRCDeallocation<T, DeallocationPolicy>(target, enabled) {} |
| 1015 |
}; |
| 1016 |
|
| 1017 |
#endif |
| 1018 |
|
| 1019 |
} |
| 1020 |
|
| 1021 |
#endif |