@@ -21,8 +21,10 @@ class Config final
2121 using vm_type = std::shared_ptr<lua_State>;
2222 using value_type = luabridge::LuaRef;
2323
24- // using string_type = std::string;
24+ using string_type = std::string;
2525 using string_view_type = const std::string&;
26+ using regex_type = std::regex;
27+ using match_type = std::smatch;
2628public:
2729 Config () noexcept
2830 {
@@ -37,17 +39,28 @@ class Config final
3739 {
3840 auto status = luaL_dofile (m_vm.get (), filename.c_str ());
3941
40- if (! status) {
42+ if (status != 0 ) {
4143 throw std::runtime_error (" failed to parse config" );
4244 }
4345 }
4446
45- value_type get (string_view_type key) const
47+ value_type get (string_view_type key)
4648 {
47- return luabridge::getGlobal (m_vm.get (), key.c_str ());
49+ if (!std::regex_match (key, m_what, m_conf_reg)) {
50+ throw std::runtime_error (" config key error" );
51+ }
52+
53+ auto w1 = m_what[1 ].str ();
54+ auto w2 = m_what[2 ].str ();
55+
56+ return luabridge::getGlobal (
57+ m_vm.get (), w1.c_str ())[w2];
4858 }
4959private:
5060 vm_type m_vm {luaL_newstate (), lua_close};
61+
62+ regex_type m_conf_reg {R"( ^(\w+)\.(\w+)$)" };
63+ match_type m_what;
5164};
5265
5366END_NAMESPACE (cpp_study)
0 commit comments