1
0

hobj.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef HW_OBJ_H_
  2. #define HW_OBJ_H_
  3. #include <string>
  4. #include <map>
  5. #include <list>
  6. #include "hdef.h"
  7. #include "hvar.h"
  8. class HObj {
  9. public:
  10. HObj(HObj* parent = NULL) {
  11. _parent = parent;
  12. }
  13. virtual ~HObj() {
  14. deleteAllChild();
  15. }
  16. std::string name() {
  17. return _objName;
  18. }
  19. void setName(char* name) {
  20. _objName = name;
  21. }
  22. HObj* parent() {
  23. return _parent;
  24. }
  25. void setParent(HObj* ptr) {
  26. _parent = ptr;
  27. }
  28. void addChild(HObj* ptr) {
  29. _children.push_back(ptr);
  30. }
  31. void removeChild(HObj* ptr) {
  32. auto iter = _children.begin();
  33. while (iter != _children.end()) {
  34. if ((*iter) == ptr) {
  35. iter = _children.erase(iter);
  36. } else {
  37. ++iter;
  38. }
  39. ++iter;
  40. }
  41. }
  42. void deleteAllChild() {
  43. auto iter = _children.begin();
  44. while (iter != _children.end()) {
  45. SAFE_DELETE(*iter);
  46. ++iter;
  47. }
  48. _children.clear();
  49. }
  50. HObj* findChild(std::string objName) {
  51. auto iter = _children.begin();
  52. while (iter != _children.end()) {
  53. if ((*iter)->name() == objName)
  54. return *iter;
  55. iter++;
  56. }
  57. }
  58. HVar property(std::string key) {
  59. auto iter = _properties.find(key);
  60. if (iter != _properties.end())
  61. return iter->second;
  62. return HVar();
  63. }
  64. void setProperty(std::string key, HVar value) {
  65. _properties[key] = value;
  66. }
  67. method_t method(std::string key) {
  68. auto iter = _methods.find(key);
  69. if (iter != _methods.end())
  70. return iter->second;
  71. return NULL;
  72. }
  73. void setMethod(std::string key, method_t method) {
  74. _methods[key] = method;
  75. }
  76. public:
  77. std::string _objName;
  78. std::map<std::string, HVar> _properties;
  79. std::map<std::string, method_t> _methods;
  80. HObj* _parent;
  81. std::list<HObj*> _children;
  82. };
  83. #endif // HW_OBJ_H_