libzypp  14.32.0
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( const std::string & type_r )
40  {
41  std::string::size_type pos = type_r.find( "/" );
42  if ( pos == std::string::npos )
43  {
44  testAndSet( _type, type_r );
45  }
46  else
47  {
48  testAndSet( _type, type_r.substr( 0, pos ) );
49  testAndSet( _subtype, type_r.substr( pos+1 ) );
50  }
51  }
52 
56  ContentType( const std::string & type_r, const std::string & subtype_r )
57  {
58  testAndSet( _type, type_r );
59  testAndSet( _subtype, subtype_r );
60  }
61 
62  public:
64  const std::string & type() const
65  { return _type; }
66 
70  void type( const std::string & type_r )
71  { _type = type_r; }
72 
74  const std::string & subtype() const
75  { return _subtype; }
76 
80  void subtype( const std::string & subtype_r )
81  { _subtype = subtype_r; }
82 
83  public:
85  bool empty() const
86  { return emptyType() && emptySubtype(); }
88  bool emptyType() const
89  { return _type.empty(); }
91  bool emptySubtype() const
92  { return _subtype.empty(); }
93 
95  explicit operator bool () const
96  { return !empty(); }
97 
99  std::string asString() const
100  { std::string ret( type() ); if ( ! emptySubtype() ) { ret += "/"; ret += subtype(); } return ret; }
101 
102  private:
103  void testAndSet( std::string & var_r, const std::string & val_r )
104  {
105  if ( val_r.find_first_of( "/ \t\r\n" ) != std::string::npos )
106  throw std::invalid_argument( "ContentType: illegal char in '" + val_r + "'" );
107  var_r = val_r;
108  }
109  private:
110  std::string _type;
111  std::string _subtype;
112  };
113 
115  inline std::ostream & operator<<( std::ostream & str, const ContentType & obj )
116  { return str << obj.asString(); }
117 
119  inline bool operator==( const ContentType & lhs, const ContentType & rhs )
120  { return lhs.type() == rhs.type() && lhs.subtype() == rhs.subtype(); }
121 
123  inline bool operator!=( const ContentType & lhs, const ContentType & rhs )
124  { return !( lhs == rhs ); }
125 
127  inline bool operator<( const ContentType & lhs, const ContentType & rhs )
128  { int cmp = lhs.type().compare( rhs.type() ); return cmp < 0 || ( cmp == 0 && lhs.subtype() < rhs.subtype() ); }
129 
131  inline bool operator<=( const ContentType & lhs, const ContentType & rhs )
132  { return lhs < rhs || lhs == rhs; }
133 
135  inline bool operator>( const ContentType & lhs, const ContentType & rhs )
136  { return !( lhs <= rhs ); }
137 
139  inline bool operator>=( const ContentType & lhs, const ContentType & rhs )
140  { return !( lhs < rhs ); }
141 
142 
143 } // namespace zypp
145 #endif // ZYPP_CONTENTTYPE_H
void subtype(const std::string &subtype_r)
Set subtype.
Definition: ContentType.h:80
bool operator!=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:123
bool emptySubtype() const
Whether subtype is empty.
Definition: ContentType.h:91
bool operator<(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:127
bool operator>(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:135
bool empty() const
Whether type and subtype are empty.
Definition: ContentType.h:85
void testAndSet(std::string &var_r, const std::string &val_r)
Definition: ContentType.h:103
ContentType(const std::string &type_r, const std::string &subtype_r)
Ctor taking type and subtype.
Definition: ContentType.h:56
bool operator>=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:139
String related utilities and Regular expression matching.
std::string _subtype
Definition: ContentType.h:111
std::string _type
Definition: ContentType.h:110
const std::string & subtype() const
Get subtype.
Definition: ContentType.h:74
const std::string & type() const
Get type.
Definition: ContentType.h:64
bool operator==(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:119
std::ostream & operator<<(std::ostream &str, const ContentType &obj)
Definition: ContentType.h:115
ContentType(const std::string &type_r)
Ctor taking "type[/subtype]"
Definition: ContentType.h:39
void type(const std::string &type_r)
Set type.
Definition: ContentType.h:70
ContentType()
Default ctor: empty.
Definition: ContentType.h:33
SolvableIdType size_type
Definition: PoolMember.h:99
bool operator<=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:131
std::string asString() const
String representation "type[/subtype]"
Definition: ContentType.h:99
bool emptyType() const
Whether type is empty.
Definition: ContentType.h:88
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Mime type like 'type/subtype' classification of content.
Definition: ContentType.h:29