libzypp 17.37.1
MediaUrl.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#ifndef ZYPP_MEDIA_MEDIAURL_INCLUDED
10#define ZYPP_MEDIA_MEDIAURL_INCLUDED
11
12#include <zypp-core/Url.h>
13#include <any>
14#include <string>
15#include <unordered_map>
16
17namespace zypp::media {
18// The header is exposed and in old SPs yast builds with c++11.
19#ifdef __cpp_lib_any
23 class MediaUrl {
24 public:
25
26 using SettingsMap = std::unordered_map<std::string, std::any>;
27
28 MediaUrl(zypp::Url url,
29 std::unordered_map<std::string, std::any> settings = {});
30
31 ~MediaUrl() = default;
32 MediaUrl(const MediaUrl &) = default;
33 MediaUrl(MediaUrl &&) = default;
34 MediaUrl &operator=(const MediaUrl &) = default;
35 MediaUrl &operator=(MediaUrl &&) = default;
36
37 bool hasConfig( const std::string &key ) const;
38 void setConfig( const std::string &key, std::any value );
39 const std::any &getConfig( const std::string &key ) const;
40 const SettingsMap &config() const;
41
42 const zypp::Url &url() const;
43 void setUrl(const zypp::Url &newUrl);
44
45 template <typename T>
46 std::enable_if_t<!std::is_same_v<T, std::any>> setConfig ( const std::string &key, T &&value ) {
47 setConfig( key, std::make_any<T>( std::forward<T>(value) ) );
48 }
49
50 template <typename T>
51 std::enable_if_t<!std::is_same_v<T, std::any>, const T&> getConfig( const std::string &key ) const {
52 const std::any &c = getConfig(key);
53 // use the pointer overloads to get to a const reference of the containing type
54 // we need to throw std::out_of_range manually here
55 const T* ref = std::any_cast<const T>(&c);
56 if ( !ref )
57 throw std::out_of_range("Key was not found in settings map.");
58
59 return *ref;
60 }
61
62 private:
63 zypp::Url _url;
64 SettingsMap _settings;
65 };
66
67 std::ostream & operator<<( std::ostream & str, const MediaUrl & url );
68
72 bool operator<( const MediaUrl &lhs, const MediaUrl &rhs );
73
77 bool operator==( const MediaUrl &lhs, const MediaUrl &rhs );
78
79
80 bool operator!=( const MediaUrl &lhs, const MediaUrl &rhs );
81
82#else // __cpp_lib_any
83 class MediaUrl;
84#endif // __cpp_lib_any
85}
86
87
88#endif
typename enable_if< B, T >::type enable_if_t
Definition TypeTraits.h:45
std::ostream & operator<<(std::ostream &str, const MediaHandler &obj)
bool operator!=(const MediaUrl &lhs, const MediaUrl &rhs)
Definition MediaUrl.cc:65
bool operator==(const MediaUrl &lhs, const MediaUrl &rhs)
Definition MediaUrl.cc:60
bool operator<(const MediaUrl &lhs, const MediaUrl &rhs)
Definition MediaUrl.cc:55
SolvableSpec & operator=(const SolvableSpec &)=default