libe3 0.2.5
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
encoder_factory.cpp
Go to the documentation of this file.
1/**
2 * @file encoder_factory.cpp
3 * @brief Factory for creating encoder instances
4 *
5 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
10#if LIBE3_ENABLE_ASN1
11#include "asn1_encoder.hpp"
12#endif
13#if LIBE3_ENABLE_JSON
14#include "json_encoder.hpp"
15#endif
16#if LIBE3_ENABLE_PROTOBUF
17#include "protobuf_encoder.hpp"
18#endif
19#include "libe3/logger.hpp"
20
21namespace libe3 {
22
23namespace {
24constexpr const char* LOG_TAG = "EncoderFactory";
25
26// Human-readable name and the CMake flag that compiles in each format, used to
27// produce an actionable error when a requested encoding was not built in.
28struct EncodingInfo {
29 const char* name;
30 const char* enable_flag;
31};
32
33EncodingInfo encoding_info(EncodingFormat format) {
34 switch (format) {
36 return {"ASN.1", "LIBE3_ENABLE_ASN1"};
38 return {"JSON", "LIBE3_ENABLE_JSON"};
40 return {"Protocol Buffers", "LIBE3_ENABLE_PROTOBUF"};
41 }
42 return {"unknown", nullptr};
43}
44} // namespace
45
46std::unique_ptr<E3Encoder> create_encoder(EncodingFormat format) {
47 switch (format) {
48#if LIBE3_ENABLE_ASN1
50 return std::make_unique<Asn1E3Encoder>();
51#endif
52#if LIBE3_ENABLE_JSON
54 return std::make_unique<JsonE3Encoder>();
55#endif
56#if LIBE3_ENABLE_PROTOBUF
58 return std::make_unique<ProtobufE3Encoder>();
59#endif
60 default:
61 break;
62 }
63
64 // The requested format's case was either compiled out (its LIBE3_ENABLE_*
65 // flag was off in this build) or is not a known EncodingFormat value.
66 const EncodingInfo info = encoding_info(format);
67 if (info.enable_flag) {
68 E3_LOG_ERROR(LOG_TAG)
69 << "Encoding format " << info.name
70 << " requested but not compiled into this libe3 build; rebuild with -D"
71 << info.enable_flag << "=ON";
72 } else {
73 E3_LOG_ERROR(LOG_TAG) << "Unknown encoding format requested: "
74 << static_cast<unsigned>(format);
75 }
76 return nullptr;
77}
78
79} // namespace libe3
ASN.1 APER Encoder for E3AP PDUs.
Abstract E3 Encoder interface for PDU encoding/decoding.
JSON Encoder for E3AP PDUs.
Logging facility for libe3.
#define E3_LOG_ERROR(component)
Definition logger.hpp:225
std::unique_ptr< E3Encoder > create_encoder(EncodingFormat format)
Factory function to create appropriate encoder.
EncodingFormat
E3AP encoding formats supported by the library.
Definition types.hpp:51
@ JSON
JSON encoding (for development/debugging).
Definition types.hpp:53
@ ASN1
ASN.1 PER encoding (standard O-RAN format).
Definition types.hpp:52
@ PROTOBUF
Protocol Buffers encoding.
Definition types.hpp:54
Protocol Buffers encoder for E3AP PDUs.