attrcb.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef _ATTRCB_H
  2. #define _ATTRCB_H
  3. #include "attribute.h"
  4. /**
  5. Enables you to register callbacks on
  6. a specific attribute to monitor if it's
  7. value has been changed by the user or other.
  8. This class is not meant to be used on it's own.
  9. Please derive from it instead.
  10. @short Attribute callback
  11. @ver 1.0
  12. @author Nullsoft
  13. @see Attribute
  14. @see int_attrCB
  15. @see _int
  16. @see _float
  17. @see _string
  18. @see _bool
  19. */
  20. class AttrCallback {
  21. public:
  22. /**
  23. Does nothing.
  24. */
  25. virtual ~AttrCallback() {}
  26. /**
  27. Event triggered when the value of the attribute,
  28. for which this callback has been registered, changes.
  29. This is a pure virtual, please override to implement
  30. your custom behavior.
  31. @param attr Attribute for which the value has changed.
  32. */
  33. virtual void onValueChange(Attribute *attr)=0;
  34. };
  35. /**
  36. Enables you to register callbacks on a specific
  37. integer or boolean attribute to monitor if the
  38. value has been changed by the user or other.
  39. @short Integer or Boolean attribute Callback.
  40. @ver 1.0
  41. @author Nullsoft
  42. @see Attribute
  43. @see _int
  44. @see _bool
  45. */
  46. class int_attrCB : public AttrCallback {
  47. typedef void (*fnPtrType)(int);
  48. public:
  49. /**
  50. Upon construction, you must specify which
  51. function will be called when the value of
  52. the attribute has indeed changed.
  53. This is done using a pointer to the function.
  54. The function must accept one parameter of type
  55. int, like so: void myfunc(int val);
  56. @param _fn Pointer to the function to use on value change.
  57. */
  58. int_attrCB(fnPtrType _fn) { fnptr = _fn; }
  59. /**
  60. Event triggered when the value of the attribute,
  61. for which this callback has been registered, changes.
  62. Override this to implement your own behavior.
  63. The default is to send the new value of the attribute
  64. to a function which you specify upon construction
  65. of this object.
  66. @param attr Attribute for which the value has changed.
  67. */
  68. virtual void onValueChange(Attribute *attr) {
  69. ASSERT(attr->getAttributeType() == AttributeType::INT ||
  70. attr->getAttributeType() == AttributeType::BOOL);
  71. (*fnptr)(attr->getValueAsInt());
  72. }
  73. private:
  74. fnPtrType fnptr;
  75. };
  76. class string_attrCB : public AttrCallback {
  77. typedef void (*fnPtrType)(const wchar_t *);
  78. public:
  79. /**
  80. Upon construction, you must specify which
  81. function will be called when the value of
  82. the attribute has indeed changed.
  83. This is done using a pointer to the function.
  84. The function must accept one parameter of type
  85. int, like so: void myfunc(const char *val);
  86. @param _fn Pointer to the function to use on value change.
  87. */
  88. string_attrCB(fnPtrType _fn) { fnptr = _fn; }
  89. /**
  90. Event triggered when the value of the attribute,
  91. for which this callback has been registered, changes.
  92. Override this to implement your own behavior.
  93. The default is to send the name of the attribute
  94. to a function which you specify upon construction
  95. of this object.
  96. @param attr Attribute for which the value has changed.
  97. */
  98. virtual void onValueChange(Attribute *attr)
  99. {
  100. ASSERT(attr->getAttributeType() == AttributeType::STRING);
  101. (*fnptr)(attr->getAttributeName());
  102. }
  103. private:
  104. fnPtrType fnptr;
  105. };
  106. #endif