Skip to content

Commit f03b9ee

Browse files
committed
Config.hpp
1 parent 45714b6 commit f03b9ee

3 files changed

Lines changed: 62 additions & 3 deletions

File tree

section5/Config.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2020 by Chrono
2+
3+
#ifndef _HELLO_HPP
4+
#define _HELLO_HPP
5+
6+
#include "cpplang.hpp"
7+
8+
extern "C" {
9+
#include <luajit.h>
10+
#include <lualib.h>
11+
#include <lauxlib.h>
12+
}
13+
14+
#include <LuaBridge/LuaBridge.h>
15+
16+
BEGIN_NAMESPACE(cpp_study)
17+
18+
class Config final
19+
{
20+
public:
21+
using vm_type = std::shared_ptr<lua_State>;
22+
using value_type = luabridge::LuaRef;
23+
24+
//using string_type = std::string;
25+
using string_view_type = const std::string&;
26+
public:
27+
Config() noexcept
28+
{
29+
assert(m_vm);
30+
31+
luaL_openlibs(m_vm.get());
32+
}
33+
34+
~Config() = default;
35+
public:
36+
void load(string_view_type filename)
37+
{
38+
auto status = luaL_dofile(m_vm.get(), filename.c_str());
39+
40+
if (!status) {
41+
throw std::runtime_error("failed to parse config");
42+
}
43+
}
44+
45+
value_type get(string_view_type key) const
46+
{
47+
return luabridge::getGlobal(m_vm.get(), key.c_str());
48+
}
49+
private:
50+
vm_type m_vm {luaL_newstate(), lua_close};
51+
};
52+
53+
END_NAMESPACE(cpp_study)
54+
55+
#endif //_HELLO_HPP
56+
File renamed without changes.

section5/srv.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Copyright (c) 2020 by Chrono
22
//
3-
// g++ srv.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -o a.out;./a.out
4-
// g++ srv.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -g -O0 -o a.out
5-
// g++ srv.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -g -O0 -o a.out;./a.out
3+
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -o a.out;./a.out
4+
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -g -O0 -o a.out
5+
// g++ srv.cpp -std=c++14 -I../common -I../common/include -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -lzmq -lpthread -g -O0 -o a.out;./a.out
66

77
//#include <iostream>
88

99
#include "cpplang.hpp"
1010
#include "Summary.hpp"
1111
#include "Zmq.hpp"
12+
#include "Config.hpp"
1213

1314
// you should put json.hpp in ../common
1415
#include "json.hpp"
@@ -38,6 +39,7 @@ int main()
3839
Summary sum;
3940
std::atomic_int count {0};
4041

42+
// todo: try-catch
4143
auto cycle = [&](const auto& addr)
4244
{
4345
using zmq_ctx = ZmqContext<1>;
@@ -59,6 +61,7 @@ int main()
5961

6062
// async process msg
6163

64+
// todo: try-catch
6265
std::async(std::launch::async,
6366
[&sum, &count](decltype(msg_ptr) ptr)
6467
{

0 commit comments

Comments
 (0)