Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++17 #1887

Merged
merged 5 commits into from
Nov 19, 2023
Merged

C++17 #1887

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ endfunction()

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

include(TestCXX11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
include(TestCXX17)
if(NOT CYGWIN)
# We don't want to use -std=gnu++11 instead of -std=c++11, but among other
# things, -std=c++11 on cygwin defines __STRICT_ANSI__ which makes cygwin
# We don't want to use -std=gnu++17 instead of -std=c++17, but among other
# things, -std=c++17 on cygwin defines __STRICT_ANSI__ which makes cygwin
# not to compile: undefined references to strerror_r, to fdopen, to
# strcasecmp, etc (their declarations in system headers are between ifdef)
set(CMAKE_CXX_EXTENSIONS false)
Expand Down
1 change: 0 additions & 1 deletion NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ZNC includes code from jQuery UI (http://jqueryui.com/), licensed under the MIT
ZNC includes code from Selectize (http://brianreavis.github.io/selectize.js/), licensed under the Apache License 2.0.
ZNC includes modified code from CMakeFindFrameworks.cmake by Kitware, Inc., licensed under BSD License.
ZNC includes modified code from TestLargeFiles.cmake, licensed under Boost Software License, Version 1.0.
ZNC includes code from BackportCpp (https://github.com/bitwizeshift/string_view-standalone), licensed under the MIT license.
ZNC includes code from cctz (https://github.com/google/cctz), licensed under the Apache License 2.0.

ZNC is developed by these people:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Core:

* GNU make
* pkg-config
* GCC 4.8 or clang 3.2
* GCC 8 or clang 5
* CMake

## Optional Requirements
Expand Down
28 changes: 14 additions & 14 deletions cmake/TestCXX11.cmake → cmake/TestCXX17.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
# limitations under the License.
#

if(NOT DEFINED cxx11check)
message(STATUS "Checking for C++11 support")
get_filename_component(_CXX11Check_dir "${CMAKE_CURRENT_LIST_FILE}"
if(NOT DEFINED cxx17check)
message(STATUS "Checking for C++17 support")
get_filename_component(_CXX17Check_dir "${CMAKE_CURRENT_LIST_FILE}"
DIRECTORY)
try_compile(cxx11_supported
"${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cxx11check"
"${_CXX11Check_dir}/cxx11check" cxx11check
OUTPUT_VARIABLE _CXX11Check_tryout)
if(cxx11_supported)
message(STATUS "Checking for C++11 support - supported")
SET(cxx11check 1 CACHE INTERNAL "C++11 support")
try_compile(cxx17_supported
"${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cxx17check"
"${_CXX17Check_dir}/cxx17check" cxx17check
OUTPUT_VARIABLE _CXX17Check_tryout)
if(cxx17_supported)
message(STATUS "Checking for C++17 support - supported")
SET(cxx17check 1 CACHE INTERNAL "C++17 support")
file(APPEND
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log"
"Output of C++11 check:\n${_CXX11Check_tryout}\n")
"Output of C++17 check:\n${_CXX17Check_tryout}\n")
else()
file(APPEND
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
"Error in C++11 check:\n${_CXX11Check_tryout}\n")
message(STATUS "Checking for C++11 support - not supported")
"Error in C++17 check:\n${_CXX17Check_tryout}\n")
message(STATUS "Checking for C++17 support - not supported")
message(FATAL_ERROR " Upgrade your compiler.\n"
" GCC 4.8+ and Clang 3.2+ are known to work.")
" GCC 8+ and Clang 5+ should work.")
endif()
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ cmake_minimum_required(VERSION 3.13)
project(cxx11check LANGUAGES CXX)
set(CMAKE_VERBOSE_MAKEFILE true)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED true)
add_executable(main main.cpp)
2 changes: 2 additions & 0 deletions cmake/cxx11check/main.cpp → cmake/cxx17check/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// warranty.

#include <map>
#include <string_view>

template <typename T>
struct check {
Expand Down Expand Up @@ -59,5 +60,6 @@ void test() { func<foo>(0); }
int main() {
std::map<int, int> m;
m.emplace(2, 4);
auto [x, y] = *m.begin();
return 0;
}
21 changes: 4 additions & 17 deletions include/znc/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
#ifndef ZNC_MESSAGE_H
#define ZNC_MESSAGE_H

// Remove this after Feb 2016 when Debian 7 is EOL
#if __cpp_ref_qualifiers >= 200710
#define ZNC_LVREFQUAL &
#elif defined(__clang__)
#define ZNC_LVREFQUAL &
#elif __GNUC__ > 4 || \
__GNUC__ == 4 && (__GNUC_MINOR__ > 8 || \
__GNUC_MINOR__ == 8 && __GNUC_PATCHLEVEL__ >= 1)
#define ZNC_LVREFQUAL &
#else
#define ZNC_LVREFQUAL
#endif

#ifdef SWIG
#define ZNC_MSG_DEPRECATED(msg)
#else
Expand Down Expand Up @@ -166,7 +153,7 @@ class CMessage {
// Implicit and explicit conversion to a subclass reference.
#ifndef SWIG
template <typename M>
M& As() ZNC_LVREFQUAL {
M& As() & {
static_assert(std::is_base_of<CMessage, M>{},
"Must be subclass of CMessage");
static_assert(sizeof(M) == sizeof(CMessage),
Expand All @@ -175,7 +162,7 @@ class CMessage {
}

template <typename M>
const M& As() const ZNC_LVREFQUAL {
const M& As() const& {
static_assert(std::is_base_of<CMessage, M>{},
"Must be subclass of CMessage");
static_assert(sizeof(M) == sizeof(CMessage),
Expand All @@ -185,12 +172,12 @@ class CMessage {

template <typename M, typename = typename std::enable_if<
std::is_base_of<CMessage, M>{}>::type>
operator M&() ZNC_LVREFQUAL {
operator M&() & {
return As<M>();
}
template <typename M, typename = typename std::enable_if<
std::is_base_of<CMessage, M>{}>::type>
operator const M&() const ZNC_LVREFQUAL {
operator const M&() const& {
return As<M>();
}
// REGISTER_ZNC_MESSAGE allows SWIG to instantiate correct .As<> calls.
Expand Down
17 changes: 8 additions & 9 deletions include/znc/Translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <znc/ZNCString.h>
#include <unordered_map>
#include <variant>

struct CTranslationInfo {
static std::map<CString, CTranslationInfo> GetTranslations();
Expand Down Expand Up @@ -83,20 +84,18 @@

class COptionalTranslation {
public:
COptionalTranslation(const CString& sText)
: m_bTranslating(false), m_sText(sText) {}
COptionalTranslation(const CString& sText) : m_text(sText) {}
COptionalTranslation(const char* s) : COptionalTranslation(CString(s)) {}
COptionalTranslation(const CDelayedTranslation& dTranslation)
: m_bTranslating(true), m_dTranslation(dTranslation) {}
COptionalTranslation(const CDelayedTranslation& dTranslation) : m_text(dTranslation) {}
CString Resolve() const {
return m_bTranslating ? m_dTranslation.Resolve() : m_sText;
if (m_text.index() == 0) {
return std::get<0>(m_text);

Check warning on line 92 in include/znc/Translation.h

View check run for this annotation

Codecov / codecov/patch

include/znc/Translation.h#L92

Added line #L92 was not covered by tests
}
return std::get<1>(m_text).Resolve();
}

private:
bool m_bTranslating;
// TODO switch to std::variant<CString, CDelayedTranslation> after C++17
CString m_sText;
CDelayedTranslation m_dTranslation;
std::variant<CString, CDelayedTranslation> m_text;
};

// Used by everything except modules.
Expand Down
10 changes: 2 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ add_custom_target(version
add_dependencies(znclib copy_csocket_h copy_csocket_cc version)

set(znc_include_dirs
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/bpstd>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_FULL_INCLUDEDIR}>")
Expand Down Expand Up @@ -87,16 +86,11 @@ set_target_properties(znclib PROPERTIES
OUTPUT_NAME "znc"
SOVERSION "${ZNC_VERSION}")

# CMake started supporting metafeature cxx_std_11 only in 3.8
set(required_cxx11_features
cxx_range_for cxx_nullptr cxx_override
cxx_lambdas cxx_auto_type)
target_compile_features(znc PUBLIC ${required_cxx11_features})
target_compile_features(znclib PUBLIC ${required_cxx11_features})

add_library(ZNC INTERFACE)
target_link_libraries(ZNC INTERFACE ${znc_link} ${zncpubdeps})
target_compile_definitions(ZNC INTERFACE "znc_export_lib_EXPORTS")
target_compile_features(ZNC INTERFACE cxx_std_17)
target_compile_features(znclib PUBLIC cxx_std_17)

if(HAVE_I18N)
add_subdirectory(po)
Expand Down
17 changes: 9 additions & 8 deletions src/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

#include <string_view>

#include <znc/Message.h>
#include <znc/Utils.h>
#include "bpstd/string_view.hpp"

CMessage::CMessage(const CString& sMessage) {
Parse(sMessage);
Expand Down Expand Up @@ -165,7 +166,7 @@ void CMessage::Parse(const CString& sMessage) {
// Find the end of the first word
const char* p = begin;
while (p < end && *p != ' ') ++p;
bpstd::string_view result(begin, p - begin);
std::string_view result(begin, p - begin);
begin = p;
// Prepare for the following word
while (begin < end && *begin == ' ') ++begin;
Expand All @@ -175,23 +176,23 @@ void CMessage::Parse(const CString& sMessage) {
// <tags>
m_mssTags.clear();
if (begin < end && *begin == '@') {
bpstd::string_view svTags = next_word().substr(1);
std::vector<bpstd::string_view> vsTags;
std::string_view svTags = next_word().substr(1);
std::vector<std::string_view> vsTags;
// Split by ';'
while (true) {
auto delim = svTags.find_first_of(';');
if (delim == bpstd::string_view::npos) {
if (delim == std::string_view::npos) {
vsTags.push_back(svTags);
break;
}
vsTags.push_back(svTags.substr(0, delim));
svTags = svTags.substr(delim + 1);
}
// Save key and value
for (bpstd::string_view svTag : vsTags) {
for (std::string_view svTag : vsTags) {
auto delim = svTag.find_first_of('=');
CString sKey = std::string(delim == bpstd::string_view::npos ? svTag : svTag.substr(0, delim));
CString sValue = delim == bpstd::string_view::npos ? std::string() : std::string(svTag.substr(delim + 1));
CString sKey = std::string(delim == std::string_view::npos ? svTag : svTag.substr(0, delim));
CString sValue = delim == std::string_view::npos ? std::string() : std::string(svTag.substr(delim + 1));
m_mssTags[sKey] =
sValue.Escape(CString::EMSGTAG, CString::CString::EASCII);
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
cmake_minimum_required(VERSION 3.13)
project(ZNCIntegrationTest LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED true)

set(CMAKE_THREAD_PREFER_PTHREAD true)
Expand Down