libzypp  15.28.6
ContentType.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
11 #ifndef ZYPP_CONTENTTYPE_H
12 #define ZYPP_CONTENTTYPE_H
13 
14 #include <iosfwd>
15 #include <string>
16 #include <stdexcept>
17 
19 namespace zypp
20 {
30  {
31  public:
34  {}
35 
39  explicit ContentType( std::string type_r )
40  {
41  std::string::size_type pos = type_r.find( "/" );
42  if ( pos != std::string::npos )
43  {
44  testAndSet( _subtype, type_r.substr( pos+1 ) );
45  type_r.erase( pos );
46  }
47  testAndSet( _type, std::move(type_r) );
48  }
49 
53  ContentType( std::string type_r, std::string subtype_r )
54  {
55  testAndSet( _type, std::move(type_r) );
56  testAndSet( _subtype, std::move(subtype_r) );
57  }
58 
59  public:
61  const std::string & type() const
62  { return _type; }
63 
67  void type( std::string type_r )
68  { _type = std::move(type_r); }
69 
71  const std::string & subtype() const
72  { return _subtype; }
73 
77  void subtype( std::string subtype_r )
78  { _subtype = std::move(subtype_r); }
79 
80  public:
82  bool empty() const
83  { return emptyType() && emptySubtype(); }
85  bool emptyType() const
86  { return _type.empty(); }
88  bool emptySubtype() const
89  { return _subtype.empty(); }
90 
92  explicit operator bool () const
93  { return !empty(); }
94 
96  std::string asString() const
97  { std::string ret( type() ); if ( ! emptySubtype() ) { ret += "/"; ret += subtype(); } return ret; }
98 
99  private:
100  void testAndSet( std::string & var_r, std::string val_r )
101  {
102  if ( val_r.find_first_of( "/ \t\r\n" ) != std::string::npos )
103  throw std::invalid_argument( "ContentType: illegal char in '" + val_r + "'" );
104  var_r = std::move(val_r);
105  }
106  private:
107  std::string _type;
108  std::string _subtype;
109  };
110 
112  inline std::ostream & operator<<( std::ostream & str, const ContentType & obj )
113  { return str << obj.asString(); }
114 
116  inline bool operator==( const ContentType & lhs, const ContentType & rhs )
117  { return lhs.type() == rhs.type() && lhs.subtype() == rhs.subtype(); }
118 
120  inline bool operator!=( const ContentType & lhs, const ContentType & rhs )
121  { return !( lhs == rhs ); }
122 
124  inline bool operator<( const ContentType & lhs, const ContentType & rhs )
125  { int cmp = lhs.type().compare( rhs.type() ); return cmp < 0 || ( cmp == 0 && lhs.subtype() < rhs.subtype() ); }
126 
128  inline bool operator<=( const ContentType & lhs, const ContentType & rhs )
129  { return lhs < rhs || lhs == rhs; }
130 
132  inline bool operator>( const ContentType & lhs, const ContentType & rhs )
133  { return !( lhs <= rhs ); }
134 
136  inline bool operator>=( const ContentType & lhs, const ContentType & rhs )
137  { return !( lhs < rhs ); }
138 
139 
140 } // namespace zypp
142 #endif // ZYPP_CONTENTTYPE_H
bool operator!=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:120
bool emptySubtype() const
Whether subtype is empty.
Definition: ContentType.h:88
bool operator<(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:124
bool operator>(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:132
void subtype(std::string subtype_r)
Set subtype.
Definition: ContentType.h:77
bool empty() const
Whether type and subtype are empty.
Definition: ContentType.h:82
void type(std::string type_r)
Set type.
Definition: ContentType.h:67
bool operator>=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:136
std::string _subtype
Definition: ContentType.h:108
std::string _type
Definition: ContentType.h:107
const std::string & subtype() const
Get subtype.
Definition: ContentType.h:71
const std::string & type() const
Get type.
Definition: ContentType.h:61
ContentType(std::string type_r)
Ctor taking "type[/subtype]"
Definition: ContentType.h:39
bool operator==(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:116
std::ostream & operator<<(std::ostream &str, const ContentType &obj)
Definition: ContentType.h:112
void testAndSet(std::string &var_r, std::string val_r)
Definition: ContentType.h:100
ContentType(std::string type_r, std::string subtype_r)
Ctor taking type and subtype.
Definition: ContentType.h:53
ContentType()
Default ctor: empty.
Definition: ContentType.h:33
SolvableIdType size_type
Definition: PoolMember.h:152
bool operator<=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:128
std::string asString() const
String representation "type[/subtype]"
Definition: ContentType.h:96
bool emptyType() const
Whether type is empty.
Definition: ContentType.h:85
Mime type like 'type/subtype' classification of content.
Definition: ContentType.h:29