libzypp  14.29.1
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 
18 namespace zypp
19 {
29  {
30  public:
33  {}
34 
38  explicit ContentType( const std::string & type_r )
39  {
40  std::string::size_type pos = type_r.find( "/" );
41  if ( pos == std::string::npos )
42  {
43  testAndSet( _type, type_r );
44  }
45  else
46  {
47  testAndSet( _type, type_r.substr( 0, pos ) );
48  testAndSet( _subtype, type_r.substr( pos+1 ) );
49  }
50  }
51 
55  ContentType( const std::string & type_r, const std::string & subtype_r )
56  {
57  testAndSet( _type, type_r );
58  testAndSet( _subtype, subtype_r );
59  }
60 
61  public:
63  const std::string & type() const
64  { return _type; }
65 
69  void type( const std::string & type_r )
70  { _type = type_r; }
71 
73  const std::string & subtype() const
74  { return _subtype; }
75 
79  void subtype( const std::string & subtype_r )
80  { _subtype = subtype_r; }
81 
82  public:
84  bool empty() const
85  { return emptyType() && emptySubtype(); }
87  bool emptyType() const
88  { return _type.empty(); }
90  bool emptySubtype() const
91  { return _subtype.empty(); }
92 
94  explicit operator bool () const
95  { return !empty(); }
96 
98  std::string asString() const
99  { std::string ret( type() ); if ( ! emptySubtype() ) { ret += "/"; ret += subtype(); } return ret; }
100 
101  private:
102  void testAndSet( std::string & var_r, const std::string & val_r )
103  {
104  if ( val_r.find_first_of( "/ \t\r\n" ) != std::string::npos )
105  throw std::invalid_argument( "ContentType: illegal char in '" + val_r + "'" );
106  var_r = val_r;
107  }
108  private:
109  std::string _type;
110  std::string _subtype;
111  };
112 
114  inline std::ostream & operator<<( std::ostream & str, const ContentType & obj )
115  { return str << obj.asString(); }
116 
118  inline bool operator==( const ContentType & lhs, const ContentType & rhs )
119  { return lhs.type() == rhs.type() && lhs.subtype() == rhs.subtype(); }
120 
122  inline bool operator!=( const ContentType & lhs, const ContentType & rhs )
123  { return !( lhs == rhs ); }
124 
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() ); }
128 
130  inline bool operator<=( const ContentType & lhs, const ContentType & rhs )
131  { return lhs < rhs || lhs == rhs; }
132 
134  inline bool operator>( const ContentType & lhs, const ContentType & rhs )
135  { return !( lhs <= rhs ); }
136 
138  inline bool operator>=( const ContentType & lhs, const ContentType & rhs )
139  { return !( lhs < rhs ); }
140 
141 
142 } // namespace zypp
144 #endif // ZYPP_CONTENTTYPE_H
void subtype(const std::string &subtype_r)
Set subtype.
Definition: ContentType.h:79
bool operator!=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:122
bool emptySubtype() const
Whether subtype is empty.
Definition: ContentType.h:90
bool operator<(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:126
bool operator>(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:134
bool empty() const
Whether type and subtype are empty.
Definition: ContentType.h:84
void testAndSet(std::string &var_r, const std::string &val_r)
Definition: ContentType.h:102
ContentType(const std::string &type_r, const std::string &subtype_r)
Ctor taking type and subtype.
Definition: ContentType.h:55
bool operator>=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:138
String related utilities and Regular expression matching.
std::string _subtype
Definition: ContentType.h:110
std::string _type
Definition: ContentType.h:109
const std::string & subtype() const
Get subtype.
Definition: ContentType.h:73
const std::string & type() const
Get type.
Definition: ContentType.h:63
bool operator==(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:118
std::ostream & operator<<(std::ostream &str, const ContentType &obj)
Definition: ContentType.h:114
ContentType(const std::string &type_r)
Ctor taking "type[/subtype]"
Definition: ContentType.h:38
void type(const std::string &type_r)
Set type.
Definition: ContentType.h:69
ContentType()
Default ctor: empty.
Definition: ContentType.h:32
SolvableIdType size_type
Definition: PoolMember.h:99
bool operator<=(const ContentType &lhs, const ContentType &rhs)
Definition: ContentType.h:130
std::string asString() const
String representation "type[/subtype]"
Definition: ContentType.h:98
bool emptyType() const
Whether type is empty.
Definition: ContentType.h:87
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Mime type like 'type/subtype' classification of content.
Definition: ContentType.h:28