From bba82e219a90d0b9984a55fa8ebc60985aa06c7e Mon Sep 17 00:00:00 2001 From: zhangzhibiao Date: Tue, 23 Jun 2026 16:26:15 +0800 Subject: [PATCH] Add C API for client version --- include/pulsar/c/version.h | 24 ++++++++++++++++++++++++ lib/c/c_Version.cc | 24 ++++++++++++++++++++++++ tests/c/c_VersionTest.cc | 26 ++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 lib/c/c_Version.cc create mode 100644 tests/c/c_VersionTest.cc diff --git a/include/pulsar/c/version.h b/include/pulsar/c/version.h index ab63c8a7..61ab8de4 100644 --- a/include/pulsar/c/version.h +++ b/include/pulsar/c/version.h @@ -20,3 +20,27 @@ #pragma once #include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Get the Pulsar C++ client library version string at runtime. + * + * The returned string is owned by the library and must not be freed. + */ +PULSAR_PUBLIC const char *pulsar_version_str(void); + +/** + * Get the Pulsar C++ client library version as a numeric value at runtime. + * + * The value uses the same format as the PULSAR_VERSION macro: + * major * 1000000 + minor * 1000 + patch. + */ +PULSAR_PUBLIC int pulsar_version(void); + +#ifdef __cplusplus +} +#endif diff --git a/lib/c/c_Version.cc b/lib/c/c_Version.cc new file mode 100644 index 00000000..d92e5ad4 --- /dev/null +++ b/lib/c/c_Version.cc @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include + +const char *pulsar_version_str(void) { return PULSAR_VERSION_STR; } + +int pulsar_version(void) { return PULSAR_VERSION; } diff --git a/tests/c/c_VersionTest.cc b/tests/c/c_VersionTest.cc new file mode 100644 index 00000000..cb167d8f --- /dev/null +++ b/tests/c/c_VersionTest.cc @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include + +TEST(CVersionTest, testRuntimeVersion) { + ASSERT_STREQ(PULSAR_VERSION_STR, pulsar_version_str()); + ASSERT_EQ(PULSAR_VERSION, pulsar_version()); +}