1
0

attrhandler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //!## An object to multiplex the callbacks of multiple attributes.
  2. #ifndef _ATTRHANDLER_H
  3. #define _ATTRHANDLER_H
  4. // This class is meant to be subclassed. The methods you must provide
  5. // are given as pure virtuals. See ExampleAttrib for more details, and
  6. // an example subclass that you can copy for your own use.
  7. #include "attrcb.h"
  8. #include "attribs.h"
  9. #include <bfc/map.h>
  10. //
  11. // Forward References
  12. class WAComponentClient;
  13. class Attribute;
  14. //
  15. // Class Definition
  16. template <class TCallback>
  17. class AttrHandler {
  18. protected:
  19. // Heh, oops. Can't have one AttrCallback handle lots of different attribs, anymore. I fix.
  20. class AttrHandlerChild : public AttrCallback {
  21. public:
  22. AttrHandlerChild(AttrHandler *_parent) : AttrCallback() {
  23. ASSERT(_parent != NULL);
  24. recursion = 0;
  25. callback = NULL;
  26. parent = _parent;
  27. }
  28. // Here is where we split out the different value types
  29. virtual void onValueChange(Attribute *attr) {
  30. if (!recursion) {
  31. // protect our programmers from stack overflow, please.
  32. recursion = 1;
  33. if ((callback != NULL) && (parent != NULL)) {
  34. int id;
  35. // find the id from the map (friendly)
  36. int success = parent->attribmap.getItem(attr,&id);
  37. if (success) {
  38. // and send it to the proper handling function (poorman's RTTI)
  39. switch (attr->getAttributeType()) {
  40. case AttributeType::INT:
  41. callback->onIntChange(id,*static_cast<_int *>(attr));
  42. break;
  43. case AttributeType::BOOL:
  44. callback->onBoolChange(id, *static_cast<_bool *>(attr));
  45. break;
  46. case AttributeType::FLOAT:
  47. callback->onFloatChange(id, *static_cast<_float *>(attr));
  48. break;
  49. case AttributeType::STRING:
  50. callback->onStringChange(id, *static_cast<_string *>(attr));
  51. break;
  52. }
  53. }
  54. }
  55. recursion = 0;
  56. }
  57. }
  58. virtual void bindCallbackObj(TCallback *callbackobj) {
  59. // Be advised, this may be null. That's okay, we test for it above.
  60. callback = callbackobj;
  61. }
  62. private:
  63. int recursion;
  64. TCallback *callback;
  65. AttrHandler *parent;
  66. };
  67. public:
  68. AttrHandler() {
  69. component = NULL;
  70. callback = NULL;
  71. }
  72. // Call this method to bind your component (in your component's constructor)
  73. virtual void bindComponent(WAComponentClient *parentcomponent) {
  74. component = parentcomponent;
  75. }
  76. // Call this method to bind your callback object (usually your window in its constructor)
  77. virtual void bindCallbackObj(TCallback *callbackobj) {
  78. // Bind ourselves.
  79. callback = callbackobj;
  80. // Then go through and rebind any children.
  81. int i, num = attrchildren.getNumItems();
  82. for (i = 0; i < num; i++) {
  83. AttrHandlerChild *child = attrchildren.enumItem(i);
  84. child->bindCallbackObj(callback);
  85. }
  86. }
  87. // Call this method to register each attribute.
  88. virtual void registerAttribute(Attribute *attr, int id) {
  89. ASSERTPR(component != NULL, "BIND YOUR COMPONENT before registering Attributes to a Handler.");
  90. // register the attrib with a child object as its callback
  91. AttrHandlerChild *child = new AttrHandlerChild(this);
  92. attrchildren.addItem(child);
  93. component->registerAttribute(attr, child);
  94. // and save its id mapping
  95. attribmap.addItem(attr, id);
  96. }
  97. #if 0
  98. // Your callback object (probably your primary window class) must implement
  99. // its own versions of these methods here. They will be called by the
  100. // switch statement below.
  101. virtual void onIntChange(int id, int *attr);
  102. virtual void onBoolChange(int id, bool *attr);
  103. virtual void onFloatChange(int id, double *attr);
  104. virtual void onStringChange(int id, const char *attr);
  105. #endif
  106. private:
  107. friend AttrHandlerChild;
  108. TCallback *callback;
  109. WAComponentClient *component;
  110. Map< Attribute *, int > attribmap;
  111. PtrList<AttrHandlerChild> attrchildren;
  112. };
  113. #endif // _ATTRHANDLER_H