1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ContentType.h
11 #ifndef ZYPP_CONTENTTYPE_H
12 #define ZYPP_CONTENTTYPE_H
18 ///////////////////////////////////////////////////////////////////
21 ///////////////////////////////////////////////////////////////////
22 /// \class ContentType
23 /// \brief Mime type like \c 'type/subtype' classification of content
25 /// Used e.g. in \ref callback::UserData to describe the kind of
26 /// user data passed as \c void* to a callback. Neither type nor
27 /// subtype may contain a '/'.
28 ///////////////////////////////////////////////////////////////////
32 /** Default ctor: empty */
36 /** Ctor taking <tt>"type[/subtype]"</tt>
37 * \throws std::invalid_argument if string is malformed
39 explicit ContentType( std::string type_r )
41 std::string::size_type pos = type_r.find( "/" );
42 if ( pos != std::string::npos )
44 testAndSet( _subtype, type_r.substr( pos+1 ) );
47 testAndSet( _type, std::move(type_r) );
50 /** Ctor taking type and subtype
51 * \throws std::invalid_argument if string is malformed
53 ContentType( std::string type_r, std::string subtype_r )
55 testAndSet( _type, std::move(type_r) );
56 testAndSet( _subtype, std::move(subtype_r) );
61 const std::string & type() const
65 * \throws std::invalid_argument if string is malformed
67 void type( std::string type_r )
68 { _type = std::move(type_r); }
71 const std::string & subtype() const
75 * \throws std::invalid_argument if string is malformed
77 void subtype( std::string subtype_r )
78 { _subtype = std::move(subtype_r); }
81 /** Whether type and subtype are empty */
83 { return emptyType() && emptySubtype(); }
84 /** Whether type is empty */
85 bool emptyType() const
86 { return _type.empty(); }
87 /** Whether subtype is empty */
88 bool emptySubtype() const
89 { return _subtype.empty(); }
91 /** Validate object in a boolean context: !empty */
92 explicit operator bool () const
95 /** String representation <tt>"type[/subtype]"</tt> */
96 std::string asString() const
97 { std::string ret( type() ); if ( ! emptySubtype() ) { ret += "/"; ret += subtype(); } return ret; }
100 void testAndSet( std::string & var_r, std::string val_r )
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);
108 std::string _subtype;
111 /** \relates ContentType Stream output */
112 inline std::ostream & operator<<( std::ostream & str, const ContentType & obj )
113 { return str << obj.asString(); }
115 /** \relates ContentType */
116 inline bool operator==( const ContentType & lhs, const ContentType & rhs )
117 { return lhs.type() == rhs.type() && lhs.subtype() == rhs.subtype(); }
119 /** \relates ContentType */
120 inline bool operator!=( const ContentType & lhs, const ContentType & rhs )
121 { return !( lhs == rhs ); }
123 /** \relates ContentType */
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() ); }
127 /** \relates ContentType */
128 inline bool operator<=( const ContentType & lhs, const ContentType & rhs )
129 { return lhs < rhs || lhs == rhs; }
131 /** \relates ContentType */
132 inline bool operator>( const ContentType & lhs, const ContentType & rhs )
133 { return !( lhs <= rhs ); }
135 /** \relates ContentType */
136 inline bool operator>=( const ContentType & lhs, const ContentType & rhs )
137 { return !( lhs < rhs ); }
141 ///////////////////////////////////////////////////////////////////
142 #endif // ZYPP_CONTENTTYPE_H