hobj.h 1.6 KB

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