Switch to headers + .cpp files

This commit is contained in:
sha512sum 2024-08-29 16:42:58 +00:00 committed by sha512sum
parent 7619b0c339
commit 3f06b44e25
16 changed files with 322 additions and 266 deletions

View file

@ -1,11 +1,5 @@
cmake_minimum_required(VERSION 3.30)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD
# This specific value changes as experimental support evolves. See
# `Help/dev/experimental.rst` in the CMake source corresponding to
# your CMake build for the exact value to use.
"0e5b6991-d74f-4b3d-a41c-cf096e0b2508")
project(larra
VERSION 0.1
LANGUAGES CXX)
@ -14,20 +8,15 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(FMT_MODULE ON)
set(FMT_MODULE OFF)
set(UTEMPL_MODULE OFF)
set(CXX_EXTENSIONS NO)
set(BOOST_INCLUDE_LIBRARIES "pfr;asio")
option(CPM_USE_LOCAL_PACKAGES "Use local packages" ON)
file(GLOB_RECURSE LIB_SOURCES "src/lib/*.cpp")
file(GLOB_RECURSE LIB_SOURCES "library/src/*.cpp")
file(GLOB_RECURSE SOURCES "src/*.cpp")
file(GLOB_RECURSE IMPL_SOURCES "src/*_impl.cpp")
file(GLOB_RECURSE LIB_IMPL_SOURCES "src/lib/*_impl.cpp")
list(REMOVE_ITEM LIB_SOURCES ${LIB_IMPL_SOURCES})
list(REMOVE_ITEM SOURCES ${IMPL_SOURCES} ${LIB_SOURCES})
set(CMAKE_CXX_MODULE_STD 1)
set_target_properties(__cmake_cxx23 PROPERTIES CXX_EXTENSIONS OFF)
file(
DOWNLOAD
@ -58,21 +47,19 @@ CPMAddPackage(
CPMAddPackage("gh:zeux/pugixml@1.14")
CPMAddPackage("gh:fmtlib/fmt#11.0.2")
set(TMP ${CPM_USE_LOCAL_PACKAGES})
set(CPM_USE_LOCAL_PACKAGES OFF)
CPMAddPackage("gh:fmtlib/fmt#11.0.2")
set(CPM_USE_LOCAL_PACKAGES ${TMP})
CPMAddPackage(
NAME utempl
URL "https://github.com/linuxnyasha/utempl/archive/refs/heads/main.zip"
EXCLUDE_FROM_ALL ON
OPTIONS "ENABLE_TESTS OFF" "ENABLE_EXAMPLES OFF"
)
set(CPM_USE_LOCAL_PACKAGES ${TMP})
@ -87,12 +74,13 @@ add_library(larra_xmpp)
add_library(larra::larra_xmpp ALIAS larra_xmpp)
target_compile_features(larra_xmpp INTERFACE cxx_std_23)
target_compile_features(larra_xmpp PUBLIC cxx_std_23)
target_sources(larra_xmpp PUBLIC FILE_SET larraXMPPSet TYPE CXX_MODULES
FILES ${LIB_SOURCES})
target_sources(larra_xmpp PRIVATE ${LIB_SOURCES})
target_sources(larra_xmpp PUBLIC ${LIB_IMPL_SOURCES})
target_include_directories(larra_xmpp PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/library/include>
$<INSTALL_INTERFACE:include>)
install(TARGETS larra_xmpp
@ -121,10 +109,13 @@ target_compile_features(larra INTERFACE cxx_std_23)
target_link_libraries(larra larra_xmpp ${GTKMM_LIBRARIES} ${GTKMM_LIBRARIES})
target_sources(larra PUBLIC FILE_SET larraSet TYPE CXX_MODULES
FILES ${SOURCES})
target_include_directories(larra PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_sources(larra PRIVATE ${SOURCES})
target_sources(larra PUBLIC ${IMPL_SOURCES})
install(TARGETS larra
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
@ -158,12 +149,14 @@ install(EXPORT larraXMPPTargets
if(ENABLE_TESTS)
find_package(GTest REQUIRED)
CPMAddPackage(
NAME GTest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
enable_testing()
file(GLOB_RECURSE SOURCES "tests/*.cpp" )
add_executable(larra_xmpp_tests)
target_sources(larra_xmpp_tests PUBLIC FILE_SET tests TYPE CXX_MODULES
FILES ${SOURCES})
target_sources(larra_xmpp_tests PUBLIC ${SOURCES})
target_link_libraries(larra_xmpp_tests GTest::gtest_main
larra_xmpp)
set_property(TARGET larra_xmpp_tests PROPERTY CXX_STANDARD 23)

View file

@ -0,0 +1,28 @@
#pragma once
#include <gtkmm.h>
#include <print>
namespace larra {
struct Application : Gtk::Application {
Application() : Gtk::Application("org.larra.larra") {
Glib::set_application_name("Larra");
}
static auto create() -> Glib::RefPtr<Application>;
auto on_startup() -> void final;
auto on_activate() -> void final;
private:
auto OnClickPreferences() -> void {
}
auto OnClickManageAccounts() -> void {
}
auto OnClickAbout() -> void {
}
};
} // namespace larra

View file

@ -0,0 +1,14 @@
#pragma once
#include <gtkmm.h>
#include <larra/application.hpp>
namespace larra {
struct MainWindow : Gtk::ApplicationWindow {
Gtk::Box main;
Application& app;
MainWindow(Application& app);
};
} // namespace larra

View file

@ -0,0 +1,74 @@
#pragma once
#include <larra/utils.hpp>
#include <string>
#include <utility>
#include <variant>
namespace larra::xmpp {
struct BareJid {
std::string username;
std::string server;
[[nodiscard]] static auto Parse(std::string_view jid) -> BareJid;
friend auto ToString(const BareJid& jid) -> std::string;
template <typename Self>
[[nodiscard]] auto Username(this Self&& self, std::string username) -> BareJid {
return utils::FieldSetHelper::With<"username">(std::forward<Self>(self), std::move(username));
}
template <typename Self>
[[nodiscard]] auto Server(this Self&& self, std::string server) -> BareJid {
return utils::FieldSetHelper::With<"server">(std::forward<Self>(self), std::move(server));
}
};
struct BareResourceJid {
std::string server;
std::string resource;
[[nodiscard]] static auto Parse(std::string_view jid) -> BareResourceJid;
friend auto ToString(const BareResourceJid& jid) -> std::string;
template <typename Self>
[[nodiscard]] auto Server(this Self&& self, std::string server) -> BareResourceJid {
return utils::FieldSetHelper::With<"server">(std::forward<Self>(self), std::move(server));
}
template <typename Self>
[[nodiscard]] auto Resource(this Self&& self, std::string resource) -> BareResourceJid {
return utils::FieldSetHelper::With<"resource">(std::forward<Self>(self), std::move(resource));
}
};
struct FullJid {
std::string username;
std::string server;
std::string resource;
[[nodiscard]] static auto Parse(std::string_view jid) -> FullJid;
friend auto ToString(const FullJid& jid) -> std::string;
template <typename Self>
[[nodiscard]] auto Username(this Self&& self, std::string username) -> FullJid {
return utils::FieldSetHelper::With<"username">(std::forward<Self>(self), std::move(username));
};
template <typename Self>
[[nodiscard]] auto Server(this Self&& self, std::string server) -> FullJid {
return utils::FieldSetHelper::With<"server">(std::forward<Self>(self), std::move(server));
};
template <typename Self>
[[nodiscard]] auto Resource(this Self&& self, std::string resource) -> FullJid {
return utils::FieldSetHelper::With<"resource">(std::forward<Self>(self), std::move(resource));
};
};
using JidVariant = std::variant<BareJid, BareResourceJid, FullJid>;
struct Jid : JidVariant {
using JidVariant::variant;
[[nodiscard]] static auto Parse(std::string_view jid) -> Jid;
friend auto ToString(const Jid& jid) -> std::string;
};
} // namespace larra::xmpp

View file

@ -0,0 +1,50 @@
module;
c export module larra.library.stream;
export import larra.library.jid;
import larra.library.utils;
import std;
namespace larra::xmpp {
export struct Stream {
std::optional<BareJid> from;
std::optional<BareJid> to;
std::optional<std::string> id;
std::optional<std::string> version;
std::optional<std::string> xmlLang;
template <typename Self>
constexpr auto From(this Self&& self, BareJid value) {
return utils::FieldSetHelper::With<"from">(std::forward<Self>(self), std::move(value));
};
template <typename Self>
constexpr auto To(this Self&& self, BareJid value) {
return utils::FieldSetHelper::With<"to">(std::forward<Self>(self), std::move(value));
};
template <typename Self>
constexpr auto Id(this Self&& self, std::string value) {
return utils::FieldSetHelper::With<"id">(std::forward<Self>(self), std::move(value));
};
template <typename Self>
constexpr auto Version(this Self&& self, std::string value) {
return utils::FieldSetHelper::With<"version">(std::forward<Self>(self), std::move(value));
};
template <typename Self>
constexpr auto XmlLang(this Self&& self, std::string value) {
return utils::FieldSetHelper::With<"xmlLang">(std::forward<Self>(self), std::move(value));
};
constexpr Stream(BareJid to) : to(std::move(to)), version("1.0") {};
friend auto operator<<(pugi::xml_node& node, const Stream& stream) {
if(stream.from) {
auto str = ToString(*stream.from);
node.append_attribute("from") = str.c_str();
};
};
};
} // namespace larra::xmpp

View file

@ -1,8 +1,5 @@
module;
#include <boost/pfr.hpp>
export module larra.library.utils;
import std;
import utempl;
#include <utempl/utils.hpp>
namespace larra::xmpp::utils {
@ -25,7 +22,7 @@ constexpr auto GetType(R(T::* ptr)) -> R;
// return kUtils.With(&SomeStruct::field2, std::forward<Self>(self), std::move(field2)); // With field2
// }
// };
export template <typename T, typename... Fs>
template <typename T, typename... Fs>
struct FieldsDescription {
utempl::Tuple<Fs...> tuple; /*!< tuple for field ptrs */
/* Method accepting field index, self and new value for field and returns object with new field value
@ -80,20 +77,24 @@ struct FieldsDescription {
};
};
namespace impl {
template <typename T, typename... Fs>
consteval auto CreateFieldsDescriptionHelper(Fs&&... fs) -> FieldsDescription<T, std::decay_t<Fs>...> {
return {.tuple = {std::forward<Fs>(fs)...}};
};
}
} // namespace impl
/* Method accepting field ptrs and returns FieldsDescription
*
* \param ptrs field ptrs
* \return FieldsDescription with T and ptrs
*/
export template <typename T, typename... Rs>
template <typename T, typename... Rs>
consteval auto CreateFieldsDescription(Rs(T::*... ptrs)) {
return [&](auto... is) {
return CreateFieldsDescriptionHelper<T>(ptrs...);
return impl::CreateFieldsDescriptionHelper<T>(ptrs...);
} | utempl::kSeq<sizeof...(Rs)>;
};
@ -113,7 +114,7 @@ consteval auto CreateFieldsDescription(Rs(T::*... ptrs)) {
// }
// };
export struct FieldSetHelper {
struct FieldSetHelper {
/* Method accepting field index, self and new value for field and returns object with new field value
*
* \param I field index for object T

80
library/src/jid.cpp Normal file
View file

@ -0,0 +1,80 @@
#include <format>
#include <larra/jid.hpp>
namespace larra::xmpp {
auto ParseBareJid(std::string_view jid, std::size_t at) -> BareJid {
return {static_cast<std::string>(jid.substr(0, at)), static_cast<std::string>(jid.substr(at + 1))};
}
auto ParseBareResourceJid(std::string_view jid, std::size_t slash) -> BareResourceJid {
return {static_cast<std::string>(jid.substr(0, slash)), static_cast<std::string>(jid.substr(slash + 1))};
}
auto ParseFullJid(std::string_view jid, std::size_t at, std::size_t slash) -> FullJid {
return {static_cast<std::string>(jid.substr(0, at)),
static_cast<std::string>(jid.substr(at + 1, slash - at - 1)),
static_cast<std::string>(jid.substr(slash + 1))};
}
auto BareJid::Parse(std::string_view jid) -> BareJid {
const auto at = jid.find('@');
if(at == std::string_view::npos) {
throw std::invalid_argument("Invalid string for jid");
}
return ParseBareJid(jid, at);
}
auto BareResourceJid::Parse(std::string_view jid) -> BareResourceJid {
const auto slash = jid.find('/');
if(slash == std::string_view::npos) {
throw std::invalid_argument("Invalid string for jid");
}
return ParseBareResourceJid(jid, slash);
}
auto FullJid::Parse(std::string_view jid) -> FullJid {
const auto at = jid.find('@');
const auto slash = jid.find('/', at);
if(at == std::string_view::npos || slash == std::string_view::npos) {
throw std::invalid_argument("Invalid string for jid");
}
return ParseFullJid(jid, at, slash);
}
auto Jid::Parse(std::string_view jid) -> Jid {
const auto at = jid.find('@');
const auto slash = jid.find('/', at == std::string_view::npos ? 0 : at);
if(at == std::string_view::npos) {
return ParseBareResourceJid(jid, slash);
}
if(slash == std::string_view::npos) {
return ParseBareJid(jid, at);
}
return ParseFullJid(jid, at, slash);
}
auto ToString(const BareJid& jid) -> std::string {
return std::format("{}@{}", jid.username, jid.server);
}
auto ToString(const BareResourceJid& jid) -> std::string {
return std::format("{}/{}", jid.server, jid.resource);
}
auto ToString(const FullJid& jid) -> std::string {
return std::format("{}@{}/{}", jid.username, jid.server, jid.resource);
}
auto ToString(const Jid& jid) -> std::string {
return std::visit<std::string>(
[](const auto& jid) -> std::string {
return ToString(jid);
},
jid);
}
} // namespace larra::xmpp

0
library/src/library.cpp Normal file
View file

View file

@ -1,19 +1,19 @@
module;
#include <gtkmm.h>
#include <print>
export module larra.application;
#include <larra/application.hpp>
#include <larra/main_window.hpp>
namespace larra {
export struct Application : Gtk::Application {
Application() : Gtk::Application("org.larra.larra") {
Glib::set_application_name("Larra");
auto Application::on_activate() -> void {
auto win = new MainWindow{*this}; // NOLINT
this->add_window(*win);
win->signal_hide().connect([win] {
delete win; // NOLINT
});
win->set_show_menubar();
win->set_visible(true);
}
static auto create() -> Glib::RefPtr<Application> {
return Glib::make_refptr_for_instance<Application>(new Application{}); // NOLINT
}
auto on_startup() -> void final {
auto Application::on_startup() -> void {
this->Gtk::Application::on_startup();
this->add_action("preferences", sigc::mem_fun(*this, &Application::OnClickPreferences));
this->add_action("accounts", sigc::mem_fun(*this, &Application::OnClickManageAccounts));
@ -31,17 +31,10 @@ export struct Application : Gtk::Application {
gmenu->append_submenu("Accounts", accountsMenu);
gmenu->append_submenu("Help", helpMenu);
this->set_menubar(gmenu);
}
};
auto on_activate() -> void final;
auto OnClickPreferences() -> void {
}
auto OnClickManageAccounts() -> void {
}
auto OnClickAbout() -> void {};
auto Application::create() -> Glib::RefPtr<Application> {
return Glib::make_refptr_for_instance<Application>(new Application{}); // NOLINT
};
} // namespace larra

View file

@ -1,16 +0,0 @@
module larra.application;
import larra.main_window;
namespace larra {
auto Application::on_activate() -> void {
auto win = new MainWindow{*this}; // NOLINT
this->add_window(*win);
win->signal_hide().connect([win] {
delete win; // NOLINT
});
win->set_show_menubar();
win->set_visible(true);
}
} // namespace larra

View file

@ -1,147 +0,0 @@
export module larra.library.jid;
import std;
import fmt;
namespace larra::xmpp {
export struct BareJid;
auto ParseBareJid(std::string_view, std::size_t) -> BareJid;
export struct BareJid {
std::string username;
std::string server;
[[nodiscard]] friend auto ToString(const BareJid& jid) -> std::string {
return std::format("{}@{}", jid.username, jid.server);
}
template <typename Self>
[[nodiscard]] auto Username(this Self&& self, std::string username) -> BareJid {
return {std::move(username), std::forward_like<Self>(self.resource)};
}
template <typename Self>
[[nodiscard]] auto Server(this Self&& self, std::string server) -> BareJid {
return {std::forward_like<Self>(self.server), std::move(server)};
}
[[nodiscard]] static auto Parse(std::string_view jid) -> BareJid {
const auto at = jid.find('@');
if(at == std::string_view::npos) {
throw std::invalid_argument("Invalid string for jid");
}
return ParseBareJid(jid, at);
}
};
auto ParseBareJid(std::string_view jid, std::size_t at) -> BareJid {
return {static_cast<std::string>(jid.substr(0, at)), static_cast<std::string>(jid.substr(at + 1))};
}
export struct BareResourceJid;
auto ParseBareResourceJid(std::string_view, std::size_t) -> BareResourceJid;
export struct BareResourceJid {
std::string server;
std::string resource;
[[nodiscard]] friend auto ToString(const BareResourceJid& jid) -> std::string {
return std::format("{}/{}", jid.server, jid.resource);
};
template <typename Self>
[[nodiscard]] auto Server(this Self&& self, std::string server) -> BareResourceJid {
return {std::move(server), std::forward_like<Self>(self.resource)};
};
template <typename Self>
[[nodiscard]] auto Resource(this Self&& self, std::string resource) -> BareResourceJid {
return {std::forward_like<Self>(self.server), std::move(resource)};
};
[[nodiscard]] static auto Parse(std::string_view jid) -> BareResourceJid {
const auto slash = jid.find('/');
if(slash == std::string_view::npos) {
throw std::invalid_argument("Invalid string for jid");
};
return ParseBareResourceJid(jid, slash);
};
};
auto ParseBareResourceJid(std::string_view jid, std::size_t slash) -> BareResourceJid {
return {static_cast<std::string>(jid.substr(0, slash)), static_cast<std::string>(jid.substr(slash + 1))};
}
export struct FullJid;
auto ParseFullJid(std::string_view jid, std::size_t at, std::size_t slash) -> FullJid;
export struct FullJid {
std::string username;
std::string server;
std::string resource;
[[nodiscard]] friend auto ToString(const FullJid& jid) -> std::string {
return std::format("{}@{}/{}", jid.username, jid.server, jid.resource);
};
template <typename Self>
[[nodiscard]] auto Username(this Self&& self, std::string username) -> FullJid {
return {std::move(username), std::forward_like<Self>(self.server), std::forward_like<Self>(self.resource)};
};
template <typename Self>
[[nodiscard]] auto Server(this Self&& self, std::string server) -> FullJid {
return {std::forward_like<Self>(self.username), std::move(server), std::forward_like<Self>(self.resource)};
};
template <typename Self>
[[nodiscard]] auto Resource(this Self&& self, std::string resource) -> FullJid {
return {std::forward_like<Self>(self.username), std::forward_like<Self>(self.server), std::move(resource)};
};
[[nodiscard]] static auto Parse(std::string_view jid) -> FullJid {
const auto at = jid.find('@');
const auto slash = jid.find('/', at);
if(at == std::string_view::npos || slash == std::string_view::npos) {
throw std::invalid_argument("Invalid string for jid");
};
return ParseFullJid(jid, at, slash);
};
};
auto ParseFullJid(std::string_view jid, std::size_t at, std::size_t slash) -> FullJid {
return {static_cast<std::string>(jid.substr(0, at)),
static_cast<std::string>(jid.substr(at + 1, slash - at - 1)),
static_cast<std::string>(jid.substr(slash + 1))};
}
using JidVariant = std::variant<BareJid, BareResourceJid, FullJid>;
export struct Jid : JidVariant {
using JidVariant::variant;
[[nodiscard]] static auto Parse(std::string_view jid) -> Jid {
const auto at = jid.find('@');
const auto slash = jid.find('/', at == std::string_view::npos ? 0 : at);
if(at == std::string_view::npos) {
return ParseBareResourceJid(jid, slash);
}
if(slash == std::string_view::npos) {
return ParseBareJid(jid, at);
}
return ParseFullJid(jid, at, slash);
}
[[nodiscard]] friend auto ToString(const Jid& jid) -> std::string {
return std::visit<std::string>(
[](const auto& jid) -> std::string {
return ToString(jid);
},
jid);
};
};
} // namespace larra::xmpp

View file

@ -1,3 +0,0 @@
export module larra.library;
export import larra.library.jid;
export import larra.library.utils;

View file

@ -1,5 +1,4 @@
export module larra.main;
import larra.application;
#include <larra/application.hpp>
auto main(int argc, char* argv[]) -> int {
auto app = larra::Application::create();

View file

@ -1,14 +1,8 @@
module;
#include <gtkmm.h>
export module larra.main_window;
import larra.application;
#include <larra/main_window.hpp>
namespace larra {
export struct MainWindow : Gtk::ApplicationWindow {
Gtk::Box main;
Application& app;
MainWindow(Application& app) : app(app) {
MainWindow::MainWindow(Application& app) : app(app) {
this->set_title("Larra");
Gtk::Box leftPanel{Gtk::Orientation::VERTICAL};
leftPanel.set_halign(Gtk::Align::START);
@ -17,7 +11,6 @@ export struct MainWindow : Gtk::ApplicationWindow {
leftPanel.append(accounts);
this->main.append(leftPanel);
this->set_child(this->main);
}
};
} // namespace larra

View file

@ -1,7 +1,5 @@
module;
#include <larra/jid.hpp>
#include <gtest/gtest.h>
export module tests.jid;
import larra.library.jid;
namespace larra::xmpp {

View file

@ -1,7 +1,6 @@
module;
#include <gtest/gtest.h>
export module tests.set;
import larra.library.utils;
#include <larra/utils.hpp>
namespace larra::xmpp {