hvar.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef H_VAR_H
  2. #define H_VAR_H
  3. #include "hdef.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. class HVar{
  7. public:
  8. enum TYPE{
  9. UNKNOWN,
  10. BOOLEAN,
  11. INTEGER,
  12. FLOAT,
  13. STRING,
  14. POINTER
  15. } type;
  16. union DATA{
  17. bool b;
  18. int64 i;
  19. float64 f;
  20. char* str;
  21. void* ptr;
  22. } data;
  23. HVar() {memset(&data, 0, sizeof(data)); type = UNKNOWN;}
  24. HVar(bool b) {data.b = b; type = BOOLEAN;}
  25. HVar(int64 i) {data.i = i; type = INTEGER;}
  26. HVar(float64 f) {data.f = f; type = FLOAT;}
  27. HVar(char* str) {
  28. data.str = (char*)malloc(strlen(str)+1);
  29. strcpy(data.str, str);
  30. type = STRING;
  31. }
  32. HVar(void* ptr) {data.ptr = ptr; type = POINTER;}
  33. ~HVar() {
  34. if (type == STRING) {
  35. SAFE_FREE(data.str);
  36. }
  37. }
  38. bool isNull() {return type == UNKNOWN;}
  39. bool isValid() {return type != UNKNOWN;}
  40. bool toBool() {return data.b;}
  41. int64 toInt() {return data.i;}
  42. float64 toFloat() {return data.f;}
  43. char* toString() {return data.str;}
  44. void* toPointer() {return data.ptr;}
  45. };
  46. #endif // H_VAR_H