Skip to content

Commit efeffae

Browse files
committed
1. chef_snippet.hpp new CHEF_PROPERTY_WITH_INIT_VALUE 2. chef_stuff_op.hpp new unix_timestamp_msec
1 parent 19d271b commit efeffae

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Starry Night - linux c++ 基础库
1+
# Starry Night - linux c++ basic lib
22

33
![platform](https://img.shields.io/badge/platform-linux%20%7C%20macos%20-green.svg)
44
[![Release](https://img.shields.io/github/release/q191201771/starry-night.svg)](https://github.com/q191201771/starry-night/releases)
@@ -47,7 +47,7 @@ MT | chef_wait_event_counter.hpp | chef_env | 阻塞等待1~N个事件发生。
4747
MT | chef_task_thread.hpp | chef_env | 开启一个线程,可以往里面持续添加异步任务,任务串行执行,且执行顺序与添加顺序一致。支持添加延时任务。任务可以是业务方的任意函数(通过bind/function实现)。 |
4848
MT | chef_thread_pool.hpp | chef_env | 线程池,池中的空闲线程抢占式执行加入的任务。适用于任务不要求强顺序性执行的场景。 |
4949
MT | chef_thread_group.hpp | chef_env | 线程池,添加异步任务时支持 指定线程号 和 池内自动调度 两种方式。指定线程号方式将任务按业务方自身需求分类打到固定线程中执行。池内自动调度方式支持round-robin轮转循环,随机,当前最空闲(即未完成任务数最少)线程三种方式。 |
50-
IP | chef_filelock.hpp | nope | 文件锁,可用于多进程间(无亲缘关系的进程间也可以)的互斥锁,并且是进程崩溃安全的(即进程退出时会自动释放持有的文件锁)。 |
50+
IPC | chef_filelock.hpp | nope | 文件锁,可用于多进程间(无亲缘关系的进程间也可以)的互斥锁,并且是进程崩溃安全的(即进程退出时会自动释放持有的文件锁)。 |
5151
SO | chef_fmt_op.hpp | c++11 | 方便的生成格式化字符串,类似于sprintf,格式符由`%d` `%s`等等简化成统一的`{}` |
5252
SO | chef_stringify_stl.hpp | nope | 字符串化stl容器。支持所有stl类型容器,支持多维嵌套容器,支持容器元素为自定义类型,支持自定义样式 |
5353
SO | chef_strings_op.hpp | nope | std::string常用操作帮助函数集合 |
@@ -59,7 +59,7 @@ SS | chef_daemon_op.hpp | nope | 守护进程 |
5959
SS | chef_env_var_op.hpp | nope | 读写系统环境变量 |
6060
PH | chef_defer.hpp | chef_env | 类似golang defer,支持c goto清理等场景 |
6161
PH | chef_count_dump.hpp | chef_env | 在各种线程模型下高效的对多个tag进行计数(打点)。并支持定时将计数落盘 |
62-
PH | chef_snippet.hpp | nope | 用宏减少一些手写重复代码。比如你有一个结构体,里面有各种类型的各种名称的成员变量,有可能有锁或无锁。你不再需要手写这些变量的声明、set、get函数等一堆代码 |
62+
PH | chef_snippet.hpp | c++11 | 用宏减少一些手写重复代码。比如你有一个结构体,里面有各种类型的各种名称的成员变量,有可能有锁或无锁。你不再需要手写这些变量的声明、set、get函数等一堆代码 |
6363
PH | chef_noncopyable.hpp | nope | 禁用拷贝构造等函数 |
6464
PH | chef_stuff_op.hpp | nope | 一些暂时没归类的功能代码片段 |
6565
CE | chef_crypto_md5_op.hpp | nope | md5加密 |

src/chef_base/chef_snippet.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @tag v1.5.14
33
* @file chef_snippet.hpp
4-
* @deps nope
4+
* @deps c++11
55
* @platform linux | macos | xxx
66
*
77
* @author
@@ -16,6 +16,9 @@
1616
CHEF_PROPERTY(std::string, name);
1717
CHEF_PROPERTY(int, age);
1818
19+
// 附带初始化值
20+
CHEF_PROPERTY_WITH_INIT_VALUE(int, num_of_child, 128);
21+
1922
// 我们用std中的mutex来保证score和money的读写是带锁保护的,你也可以选择其他lock_guard类型的锁使用,比如boost,但使用方需在包含该模块之前包含boost相应的头文件
2023
// 此处score和money共用了一把锁m,使用方也可以根据业务情况自由选择锁粒度
2124
CHEF_PROPERTY_STD_LOCK(m);
@@ -28,7 +31,7 @@
2831
f.set_age(18);
2932
f.set_score(88.8);
3033
f.set_money(500 * 10000);
31-
std::cout << f.name() << " " << f.age() << " " << f.score() << " " << f.money() << std::endl;
34+
std::cout << f.name() << " " << f.age() << " " << f.score() << " " << f.money() << " " << f.num_of_child() << std::endl;
3235
```
3336
*
3437
*/
@@ -51,6 +54,14 @@
5154
private: \
5255
Type name##_;
5356

57+
// 带初始化值的数据成员
58+
#define CHEF_PROPERTY_WITH_INIT_VALUE(Type, name, value) \
59+
public: \
60+
Type name() const { return name##_; } \
61+
void set_##name(Type v) { name##_ = v; } \
62+
private: \
63+
Type name##_ = value;
64+
5465
// 锁数据成员
5566
#define CHEF_PROPERTY_LOCK(LockType, lockName) \
5667
private: \

src/chef_base/chef_stuff_op.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ namespace chef {
5959
// 获取当前时间点,单位毫秒,一般用于计算两个时间点间的间隔用
6060
static uint64_t tick_msec();
6161

62+
static uint64_t unix_timestamp_msec();
63+
6264
// 获取线程号
6365
static int gettid();
6466

@@ -171,6 +173,12 @@ inline uint64_t stuff_op::tick_msec() {
171173
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
172174
}
173175

176+
inline uint64_t stuff_op::unix_timestamp_msec() {
177+
struct timeval tv;
178+
gettimeofday(&tv, NULL);
179+
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
180+
}
181+
174182
inline std::string stuff_op::bytes_to_hex(const uint8_t *buf, std::size_t len, std::size_t num_per_line) {
175183
if (!buf || len == 0 || num_per_line == 0) { return std::string(); }
176184

src/chef_base_test/chef_snippet_test.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ struct Foo {
88
CHEF_PROPERTY(std::string, name);
99
CHEF_PROPERTY(int, age);
1010

11+
CHEF_PROPERTY_WITH_INIT_VALUE(int, num_of_child, 1);
12+
1113
CHEF_PROPERTY_STD_LOCK(pro_lock);
1214
CHEF_PROPERTY_WITH_STD_LOCK(pro_lock, float, score);
1315
CHEF_PROPERTY_WITH_STD_LOCK(pro_lock, long, money);
@@ -19,7 +21,7 @@ static void example() {
1921
f.set_age(18);
2022
f.set_score(88.8);
2123
f.set_money(500 * 10000);
22-
std::cout << f.name() << " " << f.age() << " " << f.score() << " " << f.money() << std::endl;
24+
std::cout << f.name() << " " << f.age() << " " << f.score() << " " << f.money() << " " << f.num_of_child() << std::endl;
2325
}
2426

2527
int main() {

src/chef_base_test/chef_stuff_op_test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ int main() {
5353

5454
example();
5555

56+
std::cout << "unix timestamp msec:" << chef::stuff_op::unix_timestamp_msec() << std::endl;
5657
std::cout << "tick msec:" << chef::stuff_op::tick_msec() << std::endl;
5758
std::cout << "tid:" << chef::stuff_op::gettid() << std::endl;
5859
chef::stuff_op::set_thread_name("yokothd");
5960

6061
readable_bytes_test();
6162
get_host_by_name_test();
6263
std::cout << "tick msec:" << chef::stuff_op::tick_msec() << std::endl;
64+
std::cout << "unix timestamp msec:" << chef::stuff_op::unix_timestamp_msec() << std::endl;
6365

6466
std::string hex;
6567
hex = chef::stuff_op::bytes_to_hex((const uint8_t *)"aA", 2);

0 commit comments

Comments
 (0)