global.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 
  2. #include "global.h"
  3. #include "module/imodule.h"
  4. void global::regist_lua(sol::state* lua)
  5. {
  6. m_value_ptr.lock();
  7. for_iter(iter, (*m_value_ptr.parent()))
  8. {
  9. auto im = static_cast<module::imodule*>(iter->second);
  10. im->regist_global(iter->first, lua);
  11. }
  12. m_value_ptr.unlock();
  13. }
  14. void* global::get_ptr(const std::string& name)
  15. {
  16. void* result = nullptr;
  17. m_value_ptr.get(name,result);
  18. return result;
  19. }
  20. bool global::regist_ptr(const std::string& name, void* value, sol::this_state ts)
  21. {
  22. sol::state_view lua(ts);
  23. // INIT中先注册一次,防止被销毁
  24. {
  25. lua.registry()[name] = this;
  26. lua[name] = this;
  27. }
  28. return m_value_ptr.add(name, value);
  29. }
  30. VarType global::get(const std::string& name, sol::this_state s)
  31. {
  32. VarType value;
  33. if (m_values.get(name, value))
  34. {
  35. return value;
  36. }
  37. return sol::make_object(s, sol::nil);
  38. }
  39. void global::set(const std::string& name, VarType value)
  40. {
  41. m_values.set(name, value, true);
  42. }
  43. void global::clear()
  44. {
  45. m_values.clear();
  46. m_value_ptr.clear();
  47. //m_value_ptr.lock();
  48. //for_iter(iter, (*m_value_ptr.parent()))
  49. //{
  50. // auto im = static_cast<module::imodule*>(iter->second);
  51. // im->delete_global();
  52. //}
  53. //m_value_ptr.unlock();
  54. //m_value_ptr.clear();
  55. }