mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
* refactor: change `leftUntilDone` to `left_until_done` * refactor: change `magnetLink` to `magnet_link` * refactor: change `manualAnnounceTime` to `manual_announce_time` * refactor: change `maxConnectedPeers` to `max_connected_peers` * refactor: change `metadataPercentComplete` to `metadata_percent_complete` * refactor: change `peersConnected` to `peers_connected` * refactor: change `peersFrom` to `peers_from` * refactor: change `peersGettingFromUs` to `peers_getting_from_us` * refactor: change `peersSendingToUs` to `peers_sending_to_us` * refactor: change `percentComplete` to `percent_complete` * refactor: change `percentDone` to `percent_done` * refactor: change `pieceCount` to `piece_count` * refactor: use quark when possible * refactor: change `pieceSize` to `piece_size` * refactor: change `primary-mime-type` to `primary_mime_type` * refactor: change `rateDownload` to `rate_download` * refactor: change `rateUpload` to `rate_upload` * refactor: change `recheckProgress` to `recheck_progress` * refactor: change `secondsDownloading` to `seconds_downloading` * refactor: change `secondsSeeding` to `seconds_seeding` * refactor: change `sizeWhenDone` to `size_when_done` * refactor: change `startDate` to `start_date` * refactor: change `trackerStats` to `tracker_stats` * refactor: change `totalSize` to `total_size` * refactor: change `torrentFile` to `torrent_file` * refactor: change `uploadedEver` to `uploaded_ever` * refactor: change `uploadRatio` to `upload_ratio` * refactor: change `webseedsSendingToUs` to `webseeds_sending_to_us` * refactor: change `bytesCompleted` to `bytes_completed` * refactor: change `clientName` to `client_name` * refactor: change `clientIsChoked` to `client_is_choked` * refactor: change `clientIsInterested` to `client_is_interested` * refactor: change `flagStr` to `flag_str` * refactor: change `isDownloadingFrom` to `is_downloading_from` * refactor: change `isEncrypted` to `is_encrypted` * refactor: change `isIncoming` to `is_incoming` * refactor: change `isUploadingTo` to `is_uploading_to` * refactor: change `isUTP` to `is_utp` * refactor: change `peerIsChoked` to `peer_is_choked` * refactor: change `peerIsInterested` to `peer_is_interested` * refactor: change `rateToClient` to `rate_to_client` * refactor: change `rateToPeer` to `rate_to_peer` * refactor: change `fromCache` to `from_cache` * refactor: change `fromDht` to `from_dht` * refactor: change `fromIncoming` to `from_incoming` * refactor: change `fromLpd` to `from_lpd` * refactor: change `fromLtep` to `from_ltep` * refactor: change `fromPex` to `from_pex` * refactor: change `fromTracker` to `from_tracker` * refactor: change `announceState` to `announce_state` * refactor: change `downloadCount` to `download_count` * refactor: change `hasAnnounced` to `has_announced` * refactor: change `hasScraped` to `has_scraped` * refactor: change `isBackup` to `is_backup` * refactor: change `lastAnnouncePeerCount` to `last_announce_peer_count` * refactor: change `lastAnnounceResult` to `last_announce_result` * refactor: change `lastAnnounceStartTime` to `last_announce_start_time` * refactor: change `lastAnnounceSucceeded` to `last_announce_succeeded` * refactor: change `lastAnnounceTime` to `last_announce_time` * refactor: change `lastAnnounceTimedOut` to `last_announce_timed_out` * refactor: change `lastScrapeResult` to `last_scrape_result` * refactor: change `lastScrapeStartTime` to `last_scrape_start_time` * refactor: change `lastScrapeSucceeded` to `last_scrape_succeeded` * refactor: change `lastScrapeTime` to `last_scrape_time` * refactor: change `lastScrapeTimedOut` to `last_scrape_timed_out` * refactor: change `leecherCount` to `leecher_count` * refactor: change `nextAnnounceTime` to `next_announce_time` * refactor: change `nextScrapeTime` to `next_scrape_time` * refactor: change `scrapeState` to `scrape_state` * refactor: change `seederCount` to `seeder_count` * refactor: change `torrent-added` to `torrent_added` * refactor: change `torrent-duplicate` to `torrent_duplicate` * refactor: change `torrent-remove` to `torrent_remove` * refactor: change `delete-local-data` to `delete_local_data` * refactor: change `torrent-rename-path` to `torrent_rename_path` * refactor: change `alt-speed-down` to `alt_speed_down` * refactor: convert `pref_toggle_entries` to quark array * refactor: change `alt-speed-enabled` to `alt_speed_enabled` * refactor: change `compact-view` to `compact_view` * refactor: change `sort-reversed` to `sort_reversed` * refactor: change `show-filterbar` to `show_filterbar` * refactor: change `show-statusbar` to `show_statusbar` * refactor: change `show-toolbar` to `show_toolbar` * refactor: change `alt-speed-time-begin` to `alt_speed_time_begin` * refactor: change `alt-speed-time-day` to `alt_speed_time_day` * refactor: change `alt-speed-time-end` to `alt_speed_time_end` * refactor: change `alt-speed-up` to `alt_speed_up` * refactor: change `alt-speed-time-enabled` to `alt_speed_time_enabled` * refactor: change `blocklist-enabled` to `blocklist_enabled` * refactor: change `blocklist-size` to `blocklist_size` * refactor: change `blocklist-url` to `blocklist_url` * refactor: change `cache-size-mb` to `cache_size_mb` * refactor: change `config-dir` to `config_dir` * refactor: change `default-trackers` to `default_trackers` * refactor: change `dht-enabled` to `dht_enabled` * refactor: change `download-dir-free-space` to `download_dir_free_space` * refactor: change `download-queue-enabled` to `download_queue_enabled` * refactor: change `download-queue-size` to `download_queue_size` * refactor: change `idle-seeding-limit-enabled` to `idle_seeding_limit_enabled` * refactor: change `idle-seeding-limit` to `idle_seeding_limit` * refactor: change `incomplete-dir-enabled` to `incomplete_dir_enabled` * refactor: change `incomplete-dir` to `incomplete_dir` * refactor: change `lpd-enabled` to `lpd_enabled` * refactor: change `peer-limit-global` to `peer_limit_global` * refactor: change `peer-limit-per-torrent` to `peer_limit_per_torrent` * refactor: change `peer-port-random-on-start` to `peer_port_random_on_start` * refactor: change `peer-port` to `peer_port` * refactor: change `pex-enabled` to `pex_enabled` * refactor: change `port-forwarding-enabled` to `port_forwarding_enabled` * refactor: change `queue-stalled-enabled` to `queue_stalled_enabled` * refactor: change `queue-stalled-minutes` to `queue_stalled_minutes` * refactor: change `rename-partial-files` to `rename_partial_files` * refactor: change `rpc-version-minimum` to `rpc_version_minimum` * refactor: change `rpc-version-semver` to `rpc_version_semver` * refactor: change `rpc-version` to `rpc_version` * refactor: change `script-torrent-added-enabled` to `script_torrent_added_enabled` * refactor: change `script-torrent-added-filename` to `script_torrent_added_filename` * refactor: change `script-torrent-done-enabled` to `script_torrent_done_enabled` * refactor: change `script-torrent-done-filename` to `script_torrent_done_filename` * refactor: change `script-torrent-done-seeding-enabled` to `script_torrent_done_seeding_enabled` * refactor: change `script-torrent-done-seeding-filename` to `script_torrent_done_seeding_filename` * refactor: change `seed-queue-enabled` to `seed_queue_enabled` * refactor: change `seed-queue-size` to `seed_queue_size` * refactor: change `seedRatioLimited` to `seed_ratio_limited` * refactor: change `session-id` to `session_id` * refactor: change `speed-limit-down-enabled` to `speed_limit_down_enabled` * refactor: change `speed-limit-down` to `speed_limit_down` * refactor: change `speed-limit-up-enabled` to `speed_limit_up_enabled` * refactor: change `speed-limit-up` to `speed_limit_up` * refactor: change `start-added-torrents` to `start_added_torrents` * refactor: change `trash-original-torrent-files` to `trash_original_torrent_files` * refactor: change `utp-enabled` to `utp_enabled` * refactor: change `tcp-enabled` to `tcp_enabled` * docs: add missing docs for RPC `tcp_enabled` * refactor: change `speed-units` to `speed_units` * refactor: change `speed-bytes` to `speed_bytes` * refactor: change `size-units` to `size_units` * refactor: change `size-bytes` to `size_bytes` * refactor: change `memory-units` to `memory_units` * refactor: change `memory-bytes` to `memory_bytes` * refactor: change `session-set` to `session_set` * refactor: change `session-get` to `session_get` * refactor: change `session-stats` to `session_stats` * refactor: change `activeTorrentCount` to `active_torrent_count` * refactor: change `downloadSpeed` to `download_speed` * refactor: change `pausedTorrentCount` to `paused_torrent_count` * refactor: change `torrentCount` to `torrent_count` * refactor: change `uploadSpeed` to `upload_speed` * refactor: change `cumulative-stats` to `cumulative_stats` * refactor: change `current-stats` to `current_stats` * refactor: change `uploadedBytes` and `uploaded-bytes` to `uploaded_bytes` * refactor: change `downloadedBytes` and `downloaded-bytes` to `downloaded_bytes` * refactor: change `filesAdded` and `files-added` to `files_added` * refactor: change `sessionCount` and `session-count` to `session_count` * refactor: change `secondsActive` and `seconds-active` to `seconds_active` * refactor: change `blocklist-update` to `blocklist_update` * refactor: change `port-test` to `port_test` * refactor: change `session-close` to `session_close` * refactor: change `queue-move-top` to `queue_move_top` * refactor: change `queue-move-up` to `queue_move_up` * refactor: change `queue-move-down` to `queue_move_down` * refactor: change `queue-move-bottom` to `queue_move_bottom` * refactor: change `free-space` to `free_space` * refactor: change `group-set` to `group_set` * refactor: change `group-get` to `group_get` * refactor: change `announce-ip` to `announce_ip` * refactor: change `announce-ip-enabled` to `announce_ip_enabled` * refactor: change `upload-slots-per-torrent` to `upload_slots_per_torrent` * refactor: change `trash-can-enabled` to `trash_can_enabled` * refactor: change `watch-dir-enabled` to `watch_dir_enabled` * refactor: change `watch-dir-force-generic` to `watch_dir_force_generic` * refactor: change `watch-dir` to `watch_dir` * refactor: change `message-level` to `message_level` * refactor: change `scrape-paused-torrents-enabled` to `scrape_paused_torrents_enabled` * refactor: change `torrent-added-verify-mode` to `torrent_added_verify_mode` * refactor: change `sleep-per-seconds-during-verify` to `sleep_per_seconds_during_verify` * refactor: change `bind-address-ipv4` to `bind_address_ipv4` * refactor: change `bind-address-ipv6` to `bind_address_ipv6` * refactor: change `peer-congestion-algorithm` to `peer_congestion_algorithm` * refactor: change `peer-socket-tos` to `peer_socket_tos` * refactor: change `peer-port-random-high` to `peer_port_random_high` * refactor: change `peer-port-random-low` to `peer_port_random_low` * refactor: change `anti-brute-force-enabled` to `anti_brute_force_enabled` * refactor: change `rpc-authentication-required` to `rpc_authentication_required` * refactor: change `rpc-bind-address` to `rpc_bind_address` * refactor: change `rpc-enabled` to `rpc_enabled` * refactor: change `rpc-host-whitelist` to `rpc_host_whitelist` * refactor: change `rpc-host-whitelist-enabled` to `rpc_host_whitelist_enabled` * refactor: change `rpc-password` to `rpc_password` * refactor: change `rpc-port` to `rpc_port` * refactor: change `rpc-socket-mode` to `rpc_socket_mode` * refactor: change `rpc-url` to `rpc_url` * refactor: change `rpc-username` to `rpc_username` * refactor: change `rpc-whitelist` to `rpc_whitelist` * refactor: change `rpc-whitelist-enabled` to `rpc_whitelist_enabled` * refactor: change `ratio-limit-enabled` to `ratio_limit_enabled` * refactor: change `ratio-limit` to `ratio_limit` * refactor: change `show-options-window` to `show_options_window` * refactor: change `open-dialog-dir` to `open_dialog_dir` * refactor: change `inhibit-desktop-hibernation` to `inhibit_desktop_hibernation` * refactor: change `show-notification-area-icon` to `show_notification_area_icon` * refactor: change `start-minimized` to `start_minimized` * refactor: change `torrent-added-notification-enabled` to `torrent_added_notification_enabled` * refactor: change `anti-brute-force-threshold` to `anti_brute_force_threshold` * refactor: change `torrent-complete-notification-enabled` to `torrent_complete_notification_enabled` * refactor: change `prompt-before-exit` to `prompt_before_exit` * refactor: change `sort-mode` to `sort_mode` * refactor: change `statusbar-stats` to `statusbar_stats` * refactor: change `show-extra-peer-details` to `show_extra_peer_details` * refactor: change `show-backup-trackers` to `show_backup_trackers` * refactor: change `blocklist-date` to `blocklist_date` * refactor: change `blocklist-updates-enabled` to `blocklist_updates_enabled` * refactor: change `main-window-layout-order` to `main_window_layout_order` * refactor: change `main-window-height` to `main_window_height` * refactor: change `main-window-width` to `main_window_width` * refactor: change `main-window-x` to `main_window_x` * refactor: change `main-window-y` to `main_window_y` * refactor: change `filter-mode` to `filter_mode` * refactor: change `filter-trackers` to `filter_trackers` * refactor: change `filter-text` to `filter_text` * refactor: change `remote-session-enabled` to `remote_session_enabled` * refactor: change `remote-session-host` to `remote_session_host` * refactor: change `remote-session-https` to `remote_session_https` * refactor: change `remote-session-password` to `remote_session_password` * refactor: change `remote-session-port` to `remote_session_port` * refactor: change `remote-session-requres-authentication` to `remote_session_requires_authentication` * refactor: change `remote-session-username` to `remote_session_username` * refactor: change `torrent-complete-sound-command` to `torrent_complete_sound_command` * refactor: change `torrent-complete-sound-enabled` to `torrent_complete_sound_enabled` * refactor: change `user-has-given-informed-consent` to `user_has_given_informed_consent` * refactor: change `read-clipboard` to `read_clipboard` * refactor: change `details-window-height` to `details_window_height` * refactor: change `details-window-width` to `details_window_width` * refactor: change `main-window-is-maximized` to `main_window_is_maximized` * refactor: change `port-is-open` to `port_is_open` * refactor: change `show-tracker-scrapes` to `show_tracker_scrapes` * refactor: change `max-peers` to `max_peers` * refactor: change `peers2-6` to `peers2_6` * refactor: change `seeding-time-seconds` to `seeding_time_seconds` * refactor: change `downloading-time-seconds` to `downloading_time_seconds` * refactor: change `ratio-mode` to `ratio_mode` * refactor: change `idle-limit` to `idle_limit` * refactor: change `idle-mode` to `idle_mode` * refactor: change `speed-Bps` to `speed_Bps` * refactor: change `use-global-speed-limit` to `use_global_speed_limit` * refactor: change `use-speed-limit` to `use_speed_limit` * chore: remove TODO comment * docs: add upgrade instructions to `5.0.0` * chore: bump rpc semver major version * chore: housekeeping
649 lines
19 KiB
C++
649 lines
19 KiB
C++
// This file Copyright © Mnemosyne LLC.
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm> // std::move()
|
|
#include <cstddef> // size_t
|
|
#include <cstdint> // int64_t
|
|
#include <initializer_list>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <type_traits> // std::is_same_v
|
|
#include <utility> // std::pair
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
#include "libtransmission/error.h"
|
|
#include "libtransmission/quark.h"
|
|
#include "libtransmission/tr-macros.h" // TR_CONSTEXPR20
|
|
|
|
/**
|
|
* A variant that holds typical benc/json types: bool, int,
|
|
* double, string, vectors of variants, and maps of variants.
|
|
* Useful when serializing / deserializing benc/json data.
|
|
*
|
|
* @see tr_variant_serde
|
|
*/
|
|
struct tr_variant
|
|
{
|
|
public:
|
|
enum Type : uint8_t
|
|
{
|
|
NoneIndex,
|
|
NullIndex,
|
|
BoolIndex,
|
|
IntIndex,
|
|
DoubleIndex,
|
|
StringIndex,
|
|
VectorIndex,
|
|
MapIndex
|
|
};
|
|
|
|
using Vector = std::vector<tr_variant>;
|
|
|
|
class Map
|
|
{
|
|
public:
|
|
Map() = default;
|
|
|
|
explicit Map(size_t const n_reserve)
|
|
{
|
|
vec_.reserve(n_reserve);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto begin() noexcept
|
|
{
|
|
return std::begin(vec_);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto begin() const noexcept
|
|
{
|
|
return std::cbegin(vec_);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto cbegin() const noexcept
|
|
{
|
|
return std::cbegin(vec_);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto end() noexcept
|
|
{
|
|
return std::end(vec_);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto end() const noexcept
|
|
{
|
|
return std::cend(vec_);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto cend() const noexcept
|
|
{
|
|
return std::cend(vec_);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto find(tr_quark const key) noexcept
|
|
{
|
|
auto const predicate = [key](auto const& item)
|
|
{
|
|
return item.first == key;
|
|
};
|
|
return std::find_if(std::begin(vec_), std::end(vec_), predicate);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto find(tr_quark const key) const noexcept
|
|
{
|
|
return Vector::const_iterator{ const_cast<Map*>(this)->find(key) };
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto find(std::initializer_list<tr_quark> keys) noexcept
|
|
{
|
|
static auto constexpr Predicate = [](auto const& item, tr_quark key)
|
|
{
|
|
return item.first == key;
|
|
};
|
|
return std::find_first_of(std::begin(vec_), std::end(vec_), std::begin(keys), std::end(keys), Predicate);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto find(std::initializer_list<tr_quark> keys) const noexcept
|
|
{
|
|
return Vector::const_iterator{ const_cast<Map*>(this)->find(keys) };
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto size() const noexcept
|
|
{
|
|
return std::size(vec_);
|
|
}
|
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto empty() const noexcept
|
|
{
|
|
return std::empty(vec_);
|
|
}
|
|
|
|
void reserve(size_t const new_cap)
|
|
{
|
|
vec_.reserve(std::max(new_cap, size_t{ 16U }));
|
|
}
|
|
|
|
auto erase(tr_quark const key)
|
|
{
|
|
if (auto iter = find(key); iter != end())
|
|
{
|
|
vec_.erase(iter);
|
|
return 1U;
|
|
}
|
|
|
|
return 0U;
|
|
}
|
|
|
|
[[nodiscard]] tr_variant& operator[](tr_quark const& key)
|
|
{
|
|
if (auto const iter = find(key); iter != end())
|
|
{
|
|
return iter->second;
|
|
}
|
|
|
|
return vec_.emplace_back(key, tr_variant{}).second;
|
|
}
|
|
|
|
template<typename Val>
|
|
std::pair<tr_variant&, bool> try_emplace(tr_quark const key, Val&& val)
|
|
{
|
|
if (auto iter = find(key); iter != end())
|
|
{
|
|
return { iter->second, false };
|
|
}
|
|
|
|
return { vec_.emplace_back(key, tr_variant{ std::forward<Val>(val) }).second, true };
|
|
}
|
|
|
|
template<typename Val>
|
|
std::pair<tr_variant&, bool> insert_or_assign(tr_quark const key, Val&& val)
|
|
{
|
|
auto res = try_emplace(key, std::forward<Val>(val));
|
|
if (!res.second)
|
|
{
|
|
res.first = std::forward<Val>(val);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
// --- custom functions
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] TR_CONSTEXPR20 auto* find_if(tr_quark const key) noexcept
|
|
{
|
|
auto const iter = find(key);
|
|
return iter != end() ? iter->second.get_if<Type>() : nullptr;
|
|
}
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] TR_CONSTEXPR20 auto const* find_if(tr_quark const key) const noexcept
|
|
{
|
|
return const_cast<Map*>(this)->find_if<Type>(key);
|
|
}
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] TR_CONSTEXPR20 auto* find_if(std::initializer_list<tr_quark> keys) noexcept
|
|
{
|
|
auto const iter = find(keys);
|
|
return iter != end() ? iter->second.get_if<Type>() : nullptr;
|
|
}
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] TR_CONSTEXPR20 auto* find_if(std::initializer_list<tr_quark> keys) const noexcept
|
|
{
|
|
return const_cast<Map*>(this)->find_if<Type>(keys);
|
|
}
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] std::optional<Type> value_if(tr_quark const key) const noexcept
|
|
{
|
|
if (auto it = find(key); it != end())
|
|
{
|
|
return it->second.value_if<Type>();
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
template<typename Type>
|
|
[[nodiscard]] std::optional<Type> value_if(std::initializer_list<tr_quark> keys) const noexcept
|
|
{
|
|
if (auto it = find(keys); it != end())
|
|
{
|
|
return it->second.value_if<Type>();
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
private:
|
|
using Vector = std::vector<std::pair<tr_quark, tr_variant>>;
|
|
Vector vec_;
|
|
};
|
|
|
|
constexpr tr_variant() noexcept = default;
|
|
~tr_variant() = default;
|
|
tr_variant(tr_variant const&) = delete;
|
|
tr_variant(tr_variant&& that) noexcept = default;
|
|
tr_variant& operator=(tr_variant const&) = delete;
|
|
tr_variant& operator=(tr_variant&& that) noexcept = default;
|
|
|
|
template<typename Val>
|
|
tr_variant(Val&& value) // NOLINT(bugprone-forwarding-reference-overload, google-explicit-constructor)
|
|
{
|
|
*this = std::forward<Val>(value);
|
|
}
|
|
|
|
[[nodiscard]] static auto make_map(size_t const n_reserve = 0U)
|
|
{
|
|
auto ret = tr_variant{};
|
|
ret.val_.emplace<Map>(n_reserve);
|
|
return ret;
|
|
}
|
|
|
|
[[nodiscard]] static auto make_vector(size_t const n_reserve = 0U)
|
|
{
|
|
auto ret = tr_variant{};
|
|
ret.val_.emplace<Vector>().reserve(n_reserve);
|
|
return ret;
|
|
}
|
|
|
|
[[nodiscard]] static auto make_raw(void const* value, size_t n_bytes)
|
|
{
|
|
return tr_variant{ std::string_view{ reinterpret_cast<char const*>(value), n_bytes } };
|
|
}
|
|
|
|
template<typename CharSpan>
|
|
[[nodiscard]] static auto make_raw(CharSpan const& value)
|
|
{
|
|
static_assert(sizeof(typename CharSpan::value_type) == 1U);
|
|
return make_raw(std::data(value), std::size(value));
|
|
}
|
|
|
|
[[nodiscard]] static tr_variant unmanaged_string(std::string_view val)
|
|
{
|
|
auto ret = tr_variant{};
|
|
ret.val_.emplace<StringHolder>().set_unmanaged(val);
|
|
return ret;
|
|
}
|
|
|
|
template<typename Val>
|
|
tr_variant& operator=(Val value)
|
|
{
|
|
if constexpr (std::is_same_v<Val, std::nullptr_t>)
|
|
{
|
|
val_.emplace<std::nullptr_t>(value);
|
|
}
|
|
else if constexpr (std::is_same_v<Val, std::string_view>)
|
|
{
|
|
val_.emplace<StringHolder>(std::string{ value });
|
|
}
|
|
// note: std::is_integral_v<bool> is true, so this check
|
|
// must come first to prevent bools from being stored as ints
|
|
else if constexpr (std::is_same_v<Val, bool>)
|
|
{
|
|
val_.emplace<bool>(value);
|
|
}
|
|
else if constexpr (std::is_integral_v<Val> || std::is_enum_v<Val>)
|
|
{
|
|
val_ = static_cast<int64_t>(value);
|
|
}
|
|
else
|
|
{
|
|
val_ = std::move(value);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
tr_variant& operator=(std::string&& value)
|
|
{
|
|
val_.emplace<StringHolder>(std::move(value));
|
|
return *this;
|
|
}
|
|
|
|
tr_variant& operator=(std::string const& value)
|
|
{
|
|
*this = std::string{ value };
|
|
return *this;
|
|
}
|
|
|
|
tr_variant& operator=(char const* const value)
|
|
{
|
|
*this = std::string{ value != nullptr ? value : "" };
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]] constexpr auto index() const noexcept
|
|
{
|
|
return val_.index();
|
|
}
|
|
|
|
[[nodiscard]] constexpr auto has_value() const noexcept
|
|
{
|
|
return index() != NoneIndex;
|
|
}
|
|
|
|
template<typename Val>
|
|
[[nodiscard]] constexpr auto* get_if() noexcept
|
|
{
|
|
if constexpr (std::is_same_v<Val, std::string_view>)
|
|
{
|
|
auto const* const val = std::get_if<StringHolder>(&val_);
|
|
return val != nullptr ? &val->sv_ : nullptr;
|
|
}
|
|
else
|
|
{
|
|
return std::get_if<Val>(&val_);
|
|
}
|
|
}
|
|
|
|
template<typename Val>
|
|
[[nodiscard]] constexpr auto const* get_if() const noexcept
|
|
{
|
|
return const_cast<tr_variant*>(this)->get_if<Val>();
|
|
}
|
|
|
|
template<size_t Index>
|
|
[[nodiscard]] constexpr auto* get_if() noexcept
|
|
{
|
|
if constexpr (Index == StringIndex)
|
|
{
|
|
auto const* const val = std::get_if<StringIndex>(&val_);
|
|
return val != nullptr ? &val->sv_ : nullptr;
|
|
}
|
|
else
|
|
{
|
|
return std::get_if<Index>(&val_);
|
|
}
|
|
}
|
|
|
|
template<size_t Index>
|
|
[[nodiscard]] constexpr auto const* get_if() const noexcept
|
|
{
|
|
return const_cast<tr_variant*>(this)->get_if<Index>();
|
|
}
|
|
|
|
template<typename Val>
|
|
[[nodiscard]] constexpr std::optional<Val> value_if() noexcept
|
|
{
|
|
if (auto const* const val = get_if<Val>())
|
|
{
|
|
return *val;
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
template<typename Val>
|
|
[[nodiscard]] std::optional<Val> value_if() const noexcept
|
|
{
|
|
return const_cast<tr_variant*>(this)->value_if<Val>();
|
|
}
|
|
|
|
template<typename Val>
|
|
[[nodiscard]] constexpr bool holds_alternative() const noexcept
|
|
{
|
|
if constexpr (std::is_same_v<Val, std::string_view>)
|
|
{
|
|
return std::holds_alternative<StringHolder>(val_);
|
|
}
|
|
else
|
|
{
|
|
return std::holds_alternative<Val>(val_);
|
|
}
|
|
}
|
|
|
|
void clear()
|
|
{
|
|
val_.emplace<std::monostate>();
|
|
}
|
|
|
|
tr_variant& merge(tr_variant const& that)
|
|
{
|
|
std::visit(Merge{ *this }, that.val_);
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
// Holds a string_view to either an unmanaged/external string or to
|
|
// one owned by the class. If the string is unmanaged, only sv_ is used.
|
|
// If we own the string, then sv_ points to the managed str_.
|
|
class StringHolder
|
|
{
|
|
public:
|
|
StringHolder() = default;
|
|
~StringHolder() = default;
|
|
explicit StringHolder(std::string&& str) noexcept;
|
|
StringHolder(StringHolder&& that) noexcept;
|
|
StringHolder(StringHolder const&) = delete;
|
|
void set_unmanaged(std::string_view sv);
|
|
StringHolder& operator=(StringHolder&& that) noexcept;
|
|
StringHolder& operator=(StringHolder const&) = delete;
|
|
|
|
std::string_view sv_;
|
|
|
|
private:
|
|
std::string str_;
|
|
};
|
|
|
|
class Merge
|
|
{
|
|
public:
|
|
explicit Merge(tr_variant& tgt);
|
|
void operator()(std::monostate const& src);
|
|
void operator()(std::nullptr_t const& src);
|
|
void operator()(bool const& src);
|
|
void operator()(int64_t const& src);
|
|
void operator()(double const& src);
|
|
void operator()(tr_variant::StringHolder const& src);
|
|
void operator()(tr_variant::Vector const& src);
|
|
void operator()(tr_variant::Map const& src);
|
|
|
|
private:
|
|
tr_variant& tgt_;
|
|
};
|
|
|
|
std::variant<std::monostate, std::nullptr_t, bool, int64_t, double, StringHolder, Vector, Map> val_;
|
|
};
|
|
|
|
template<>
|
|
[[nodiscard]] std::optional<int64_t> tr_variant::value_if() noexcept;
|
|
template<>
|
|
[[nodiscard]] std::optional<bool> tr_variant::value_if() noexcept;
|
|
template<>
|
|
[[nodiscard]] std::optional<double> tr_variant::value_if() noexcept;
|
|
|
|
// --- Strings
|
|
|
|
bool tr_variantGetStrView(tr_variant const* variant, std::string_view* setme);
|
|
|
|
bool tr_variantGetRaw(tr_variant const* variant, std::byte const** setme_raw, size_t* setme_len);
|
|
bool tr_variantGetRaw(tr_variant const* variant, uint8_t const** setme_raw, size_t* setme_len);
|
|
|
|
// --- Real Numbers
|
|
|
|
bool tr_variantGetReal(tr_variant const* variant, double* value_setme);
|
|
|
|
// --- Booleans
|
|
|
|
bool tr_variantGetBool(tr_variant const* variant, bool* setme);
|
|
|
|
// --- Ints
|
|
|
|
bool tr_variantGetInt(tr_variant const* var, int64_t* setme);
|
|
|
|
// --- Lists
|
|
|
|
void tr_variantInitList(tr_variant* initme, size_t n_reserve);
|
|
void tr_variantListReserve(tr_variant* var, size_t n_reserve);
|
|
|
|
tr_variant* tr_variantListAdd(tr_variant* var);
|
|
tr_variant* tr_variantListAddBool(tr_variant* var, bool value);
|
|
tr_variant* tr_variantListAddInt(tr_variant* var, int64_t value);
|
|
tr_variant* tr_variantListAddReal(tr_variant* var, double value);
|
|
tr_variant* tr_variantListAddStr(tr_variant* var, std::string_view value);
|
|
tr_variant* tr_variantListAddStrView(tr_variant* var, std::string_view value);
|
|
tr_variant* tr_variantListAddRaw(tr_variant* var, void const* value, size_t n_bytes);
|
|
tr_variant* tr_variantListAddList(tr_variant* var, size_t n_reserve);
|
|
tr_variant* tr_variantListAddDict(tr_variant* var, size_t n_reserve);
|
|
tr_variant* tr_variantListChild(tr_variant* var, size_t pos);
|
|
|
|
bool tr_variantListRemove(tr_variant* var, size_t pos);
|
|
|
|
[[nodiscard]] constexpr size_t tr_variantListSize(tr_variant const* const var)
|
|
{
|
|
if (var != nullptr)
|
|
{
|
|
if (auto const* const vec = var->get_if<tr_variant::Vector>(); vec != nullptr)
|
|
{
|
|
return std::size(*vec);
|
|
}
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
// --- Dictionaries
|
|
|
|
void tr_variantInitDict(tr_variant* initme, size_t n_reserve);
|
|
void tr_variantDictReserve(tr_variant* var, size_t n_reserve);
|
|
bool tr_variantDictRemove(tr_variant* var, tr_quark key);
|
|
|
|
tr_variant* tr_variantDictAdd(tr_variant* var, tr_quark key);
|
|
tr_variant* tr_variantDictAddReal(tr_variant* var, tr_quark key, double value);
|
|
tr_variant* tr_variantDictAddInt(tr_variant* var, tr_quark key, int64_t value);
|
|
tr_variant* tr_variantDictAddBool(tr_variant* var, tr_quark key, bool value);
|
|
tr_variant* tr_variantDictAddStr(tr_variant* var, tr_quark key, std::string_view value);
|
|
tr_variant* tr_variantDictAddStrView(tr_variant* var, tr_quark key, std::string_view value);
|
|
tr_variant* tr_variantDictAddList(tr_variant* var, tr_quark key, size_t n_reserve);
|
|
tr_variant* tr_variantDictAddDict(tr_variant* var, tr_quark key, size_t n_reserve);
|
|
tr_variant* tr_variantDictAddRaw(tr_variant* var, tr_quark key, void const* value, size_t n_bytes);
|
|
|
|
bool tr_variantDictChild(tr_variant* var, size_t pos, tr_quark* setme_key, tr_variant** setme_value);
|
|
tr_variant* tr_variantDictFind(tr_variant* var, tr_quark key);
|
|
bool tr_variantDictFindList(tr_variant* var, tr_quark key, tr_variant** setme);
|
|
bool tr_variantDictFindDict(tr_variant* var, tr_quark key, tr_variant** setme_value);
|
|
bool tr_variantDictFindInt(tr_variant* var, tr_quark key, int64_t* setme);
|
|
bool tr_variantDictFindReal(tr_variant* var, tr_quark key, double* setme);
|
|
bool tr_variantDictFindBool(tr_variant* var, tr_quark key, bool* setme);
|
|
bool tr_variantDictFindStrView(tr_variant* var, tr_quark key, std::string_view* setme);
|
|
bool tr_variantDictFindRaw(tr_variant* var, tr_quark key, uint8_t const** setme_raw, size_t* setme_len);
|
|
bool tr_variantDictFindRaw(tr_variant* var, tr_quark key, std::byte const** setme_raw, size_t* setme_len);
|
|
|
|
/* this is only quasi-supported. don't rely on it too heavily outside of libT */
|
|
void tr_variantMergeDicts(tr_variant* tgt, tr_variant const* src);
|
|
|
|
/**
|
|
* Helper class for serializing and deserializing benc/json data.
|
|
*
|
|
* @see tr_variant
|
|
*/
|
|
class tr_variant_serde
|
|
{
|
|
public:
|
|
[[nodiscard]] static tr_variant_serde benc() noexcept
|
|
{
|
|
return tr_variant_serde{ Type::Benc };
|
|
}
|
|
|
|
[[nodiscard]] static tr_variant_serde json() noexcept
|
|
{
|
|
return tr_variant_serde{ Type::Json };
|
|
}
|
|
|
|
// Serialize data as compactly as possible, e.g.
|
|
// omit pretty-printing JSON whitespace
|
|
constexpr tr_variant_serde& compact() noexcept
|
|
{
|
|
compact_ = true;
|
|
return *this;
|
|
}
|
|
|
|
// When set, assumes that the `input` passed to parse() is valid
|
|
// for the lifespan of the variant and we can use string_views of
|
|
// `input` instead of cloning new strings.
|
|
constexpr tr_variant_serde& inplace() noexcept
|
|
{
|
|
parse_inplace_ = true;
|
|
return *this;
|
|
}
|
|
|
|
// ---
|
|
|
|
[[nodiscard]] std::optional<tr_variant> parse(std::string_view input);
|
|
|
|
template<typename CharSpan>
|
|
[[nodiscard]] std::optional<tr_variant> parse(CharSpan const& input)
|
|
{
|
|
return parse(std::string_view{ std::data(input), std::size(input) });
|
|
}
|
|
|
|
[[nodiscard]] std::optional<tr_variant> parse_file(std::string_view filename);
|
|
|
|
[[nodiscard]] constexpr char const* end() const noexcept
|
|
{
|
|
return end_;
|
|
}
|
|
|
|
// ---
|
|
|
|
[[nodiscard]] std::string to_string(tr_variant const& var) const;
|
|
|
|
bool to_file(tr_variant const& var, std::string_view filename);
|
|
|
|
// ---
|
|
|
|
// Tracks errors when parsing / saving
|
|
tr_error error_;
|
|
|
|
private:
|
|
friend tr_variant;
|
|
|
|
enum class Type : uint8_t
|
|
{
|
|
Benc,
|
|
Json
|
|
};
|
|
|
|
struct WalkFuncs
|
|
{
|
|
void (*null_func)(tr_variant const& var, std::nullptr_t val, void* user_data);
|
|
void (*int_func)(tr_variant const& var, int64_t val, void* user_data);
|
|
void (*bool_func)(tr_variant const& var, bool val, void* user_data);
|
|
void (*double_func)(tr_variant const& var, double val, void* user_data);
|
|
void (*string_func)(tr_variant const& var, std::string_view val, void* user_data);
|
|
void (*dict_begin_func)(tr_variant const& var, void* user_data);
|
|
void (*list_begin_func)(tr_variant const& var, void* user_data);
|
|
void (*container_end_func)(tr_variant const& var, void* user_data);
|
|
};
|
|
|
|
explicit tr_variant_serde(Type type)
|
|
: type_{ type }
|
|
{
|
|
}
|
|
|
|
[[nodiscard]] std::optional<tr_variant> parse_json(std::string_view input);
|
|
[[nodiscard]] std::optional<tr_variant> parse_benc(std::string_view input);
|
|
|
|
[[nodiscard]] std::string to_json_string(tr_variant const& var) const;
|
|
[[nodiscard]] static std::string to_benc_string(tr_variant const& var);
|
|
|
|
static void walk(tr_variant const& top, WalkFuncs const& walk_funcs, void* user_data, bool sort_dicts);
|
|
|
|
Type type_;
|
|
|
|
bool compact_ = false;
|
|
|
|
bool parse_inplace_ = false;
|
|
|
|
// This is set to the first unparsed character after `parse()`.
|
|
char const* end_ = nullptr;
|
|
};
|
|
|
|
/* @} */
|