Skip to content

Commit 107343f

Browse files
committed
Config.hpp
1 parent 07de8a5 commit 107343f

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

section5/Config.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,27 @@ class Config final
4444
}
4545
}
4646

47-
value_type get(string_view_type key)
47+
template<typename T>
48+
T get(string_view_type key)
4849
{
49-
if (!std::regex_match(key, m_what, m_conf_reg)) {
50+
if (!std::regex_match(key, m_what, m_reg)) {
5051
throw std::runtime_error("config key error");
5152
}
5253

5354
auto w1 = m_what[1].str();
5455
auto w2 = m_what[2].str();
5556

56-
return luabridge::getGlobal(
57-
m_vm.get(), w1.c_str())[w2];
57+
using namespace luabridge;
58+
59+
auto v = getGlobal(
60+
m_vm.get(), w1.c_str());
61+
62+
return LuaRef_cast<T>(v[w2]);
5863
}
5964
private:
6065
vm_type m_vm {luaL_newstate(), lua_close};
6166

62-
regex_type m_conf_reg {R"(^(\w+)\.(\w+)$)"};
67+
regex_type m_reg {R"(^(\w+)\.(\w+)$)"};
6368
match_type m_what;
6469
};
6570

section5/srv.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ try
3737
{
3838
cout << "hello cpp_study server" << endl;
3939

40+
Config conf;
41+
conf.load("./conf.lua");
42+
4043
Summary sum;
4144
std::atomic_int count {0};
4245

@@ -86,12 +89,8 @@ try
8689
}
8790
};
8891

89-
Config conf;
90-
conf.load("./conf.lua");
91-
92-
string listen_addr = conf.get("config.zmq_ipc_addr");
93-
94-
auto fu = std::async(cycle, listen_addr);
92+
auto fu = std::async(cycle,
93+
conf.get<string>("config.zmq_ipc_addr"));
9594

9695
fu.wait();
9796
}

0 commit comments

Comments
 (0)