1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ContentType.h
11 #ifndef ZYPP_CONTENTTYPE_H
12 #define ZYPP_CONTENTTYPE_H
17 ///////////////////////////////////////////////////////////////////
20 ///////////////////////////////////////////////////////////////////
21 /// \class ContentType
22 /// \brief Mime type like \c 'type/subtype' classification of content
24 /// Used e.g. in \ref callback::UserData to describe the kind of
25 /// user data passed as \c void* to a callback. Neither type nor
26 /// subtype may contain a '/'.
27 ///////////////////////////////////////////////////////////////////
31 /** Default ctor: empty */
35 /** Ctor taking <tt>"type[/subtype]"</tt>
36 * \throws std::invalid_argument if string is malformed
38 explicit ContentType( const std::string & type_r )
40 std::string::size_type pos = type_r.find( "/" );
41 if ( pos == std::string::npos )
43 testAndSet( _type, type_r );
47 testAndSet( _type, type_r.substr( 0, pos ) );
48 testAndSet( _subtype, type_r.substr( pos+1 ) );
52 /** Ctor taking type and subtype
53 * \throws std::invalid_argument if string is malformed
55 ContentType( const std::string & type_r, const std::string & subtype_r )
57 testAndSet( _type, type_r );
58 testAndSet( _subtype, subtype_r );
63 const std::string & type() const
67 * \throws std::invalid_argument if string is malformed
69 void type( const std::string & type_r )
73 const std::string & subtype() const
77 * \throws std::invalid_argument if string is malformed
79 void subtype( const std::string & subtype_r )
80 { _subtype = subtype_r; }
83 /** Whether type and subtype are empty */
85 { return emptyType() && emptySubtype(); }
86 /** Whether type is empty */
87 bool emptyType() const
88 { return _type.empty(); }
89 /** Whether subtype is empty */
90 bool emptySubtype() const
91 { return _subtype.empty(); }
93 /** Validate object in a boolean context: !empty */
94 explicit operator bool () const
97 /** String representation <tt>"type[/subtype]"</tt> */
98 std::string asString() const
99 { std::string ret( type() ); if ( ! emptySubtype() ) { ret += "/"; ret += subtype(); } return ret; }
102 void testAndSet( std::string & var_r, const std::string & val_r )
104 if ( val_r.find_first_of( "/ \t\r\n" ) != std::string::npos )
105 throw std::invalid_argument( "ContentType: illegal char in '" + val_r + "'" );
110 std::string _subtype;
113 /** \relates ContentType Stream output */
114 inline std::ostream & operator<<( std::ostream & str, const ContentType & obj )
115 { return str << obj.asString(); }
117 /** \relates ContentType */
118 inline bool operator==( const ContentType & lhs, const ContentType & rhs )
119 { return lhs.type() == rhs.type() && lhs.subtype() == rhs.subtype(); }
121 /** \relates ContentType */
122 inline bool operator!=( const ContentType & lhs, const ContentType & rhs )
123 { return !( lhs == rhs ); }
125 /** \relates ContentType */
126 inline bool operator<( const ContentType & lhs, const ContentType & rhs )
127 { int cmp = lhs.type().compare( rhs.type() ); return cmp < 0 || ( cmp == 0 && lhs.subtype() < rhs.subtype() ); }
129 /** \relates ContentType */
130 inline bool operator<=( const ContentType & lhs, const ContentType & rhs )
131 { return lhs < rhs || lhs == rhs; }
133 /** \relates ContentType */
134 inline bool operator>( const ContentType & lhs, const ContentType & rhs )
135 { return !( lhs <= rhs ); }
137 /** \relates ContentType */
138 inline bool operator>=( const ContentType & lhs, const ContentType & rhs )
139 { return !( lhs < rhs ); }
143 ///////////////////////////////////////////////////////////////////
144 #endif // ZYPP_CONTENTTYPE_H