-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathcompressor_factory.cc
55 lines (45 loc) · 1.32 KB
/
compressor_factory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#include "trpc/compressor/compressor_factory.h"
#include "trpc/util/likely.h"
#include "trpc/util/log/logging.h"
namespace trpc::compressor {
CompressorFactory::CompressorFactory() {
compressors_.resize(kMaxType);
for (std::size_t i = 0; i < kMaxType; ++i) {
compressors_[i] = nullptr;
}
}
CompressorFactory::~CompressorFactory() { Clear(); }
bool CompressorFactory::Register(const CompressorPtr& compressor) {
TRPC_ASSERT(compressor != nullptr);
auto type = compressor->Type();
if (TRPC_UNLIKELY(type >= kMaxType)) {
return false;
}
compressors_[type] = compressor;
return true;
}
Compressor* CompressorFactory::Get(CompressType compress_type) {
if (TRPC_UNLIKELY(compress_type >= kMaxType)) {
return nullptr;
}
return compressors_[compress_type].get();
}
void CompressorFactory::Clear() {
for (auto&& ptr : compressors_) {
if (ptr) {
ptr.Reset();
}
}}
} // namespace trpc::compressor