hobj.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef HV_OBJ_H_
  2. #define HV_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. }
  40. }
  41. void deleteAllChild() {
  42. auto iter = _children.begin();
  43. while (iter != _children.end()) {
  44. SAFE_DELETE(*iter);
  45. ++iter;
  46. }
  47. _children.clear();
  48. }
  49. HObj* findChild(std::string objName) {
  50. for (auto iter = _children.begin(); iter != _children.end(); ++iter) {
  51. if ((*iter)->name() == objName) {
  52. return *iter;
  53. }
  54. }
  55. return NULL;
  56. }
  57. HVar property(std::string key) {
  58. auto iter = _properties.find(key);
  59. if (iter != _properties.end())
  60. return iter->second;
  61. return HVar();
  62. }
  63. void setProperty(std::string key, HVar value) {
  64. _properties[key] = value;
  65. }
  66. method_t method(std::string key) {
  67. auto iter = _methods.find(key);
  68. if (iter != _methods.end())
  69. return iter->second;
  70. return NULL;
  71. }
  72. void setMethod(std::string key, method_t method) {
  73. _methods[key] = method;
  74. }
  75. public:
  76. std::string _objName;
  77. std::map<std::string, HVar> _properties;
  78. std::map<std::string, method_t> _methods;
  79. HObj* _parent;
  80. std::list<HObj*> _children;
  81. };
  82. #endif // HV_OBJ_H_