Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
InternetMediaType.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_DataExchange_InternetMediaType_h_
5#define _Stroika_Foundation_DataExchange_InternetMediaType_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
10#include "Stroika/Foundation/Containers/Mapping.h"
12#include "Stroika/Foundation/DataExchange/DefaultSerializer.h"
13
14/**
15 * \file
16 *
17 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
18 *
19 */
20
22
23 using Characters::String;
24
25 /**
26 * MIME content-types are also sometimes referred to as 'Internet media types'.
27 *
28 * References:
29 * o https://en.wikipedia.org/wiki/Media_type
30 * o https://tools.ietf.org/html/rfc2045#section-5.1
31 * o https://tools.ietf.org/html/rfc2046
32 *
33 * From https://en.wikipedia.org/wiki/Media_type:
34 * type "/" [tree "."] subtype ["+" suffix] *[";" parameter]
35 *
36 * The currently registered types are:
37 * application, audio, example, font, image, message, model, multipart, text and video
38 *
39 * \note - this class stores the type, subtype, suffix, and parameters, but it does NOT store any comments from the content-type
40 *
41 * \note The 'tree' is just merged into the 'subtype'
42 *
43 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
44 * o Standard Stroika Comparison support (operator<=>,operator==, etc);
45 *
46 * NO GUARANTEE about the meaning of the ordering? for now. May use atom ordering
47 * or case insensitive string ordering, or other. Just legal total ordering.
48 *
49 * \see @see InternetMediaTypeRegistry for properties of a given media type
50 *
51 * TODO:
52 * @todo consider losing empty/clear members, and if they are ever needed, use optional<InternetMediaType>
53 */
54 class [[nodiscard]] InternetMediaType {
55 public:
57
58 public:
59 /**
60 * Note that according to https://tools.ietf.org/html/rfc2045#page-10, these types
61 * are compared in a case insensitive manner (by type/subtype) and the case constructed with
62 * in the string may not be preserved.
63 * "Matching of media type and subtype; is ALWAYS case-insensitive"
64 *
65 * If type provided, subType must be as well (require) but can be the special empty string value (matching anything for IsA). And no parameters allowed if type is empty.
66 * The one-argument String overload parses the Content-Type in the usual way.
67 *
68 * if suffix is provided, it must not start with the prefixing '.' or '+', and must not be empty (use nullopt in that case)
69 */
70 InternetMediaType () = default;
71 InternetMediaType (const InternetMediaType&) = default;
72 explicit InternetMediaType (const String& ct);
73 explicit InternetMediaType (AtomType type, AtomType subType, const Containers::Mapping<String, String>& parameters = {});
74 explicit InternetMediaType (AtomType type, AtomType subType, optional<AtomType> suffix,
75 const Containers::Mapping<String, String>& parameters = {});
76 explicit InternetMediaType (const String& type, const String& subType, const Containers::Mapping<String, String>& parameters = {});
77 explicit InternetMediaType (const String& type, const String& subType, const optional<String>& suffix,
78 const Containers::Mapping<String, String>& parameters = {});
79
80 public:
81 /**
82 * \brief Gets the primary (major) type of the full internet media type (as a string or atom)
83 *
84 * Supported RETURN_TYPES:
85 * o String
86 * o AtomType
87 */
88 template <typename RETURN_TYPE = String>
89 nonvirtual RETURN_TYPE GetType () const;
90
91 public:
92 /**
93 * Supported RETURN_TYPES:
94 * o String
95 * o AtomType
96 */
97 template <typename RETURN_TYPE = String>
98 nonvirtual RETURN_TYPE GetSubType () const;
99
100 public:
101 /**
102 * \brief this is the +XXX part of the internet media type (e.g. +xml) and is often omitted (but note this omits the + sign)
103 *
104 * Supported RETURN_TYPES:
105 * o String
106 * o AtomType
107 */
108 template <typename RETURN_TYPE = String>
109 nonvirtual optional<RETURN_TYPE> GetSuffix () const;
110
111 public:
112 /**
113 */
114 nonvirtual Containers::Mapping<String, String> GetParameters () const;
115
116 public:
117 /**
118 * \brief convert to type T
119 * supported types:
120 * o String
121 * o wstring
122 */
123 template <typename T>
124 nonvirtual T As () const;
125
126 public:
127 /**
128 * @todo consider losing empty/clear members, and if they are ever needed, use optional<InternetMediaType>
129 */
130 nonvirtual bool empty () const;
131
132 public:
133 /**
134 * @todo consider losing empty/clear members, and if they are ever needed, use optional<InternetMediaType>
135 */
136 nonvirtual void clear ();
137
138 public:
139 /**
140 */
141 nonvirtual strong_ordering operator<=> (const InternetMediaType& rhs) const;
142
143 public:
144 /**
145 * \note this compares all the parameters and options - so "text/plain" != "text/plain;param=3".
146 * To get this looser form of comparison, use InternetMediaTypeRegistry::sThe->IsA (InternetMediaTypes::kJSON, compareWith);
147 */
148 nonvirtual bool operator== (const InternetMediaType& rhs) const;
149
150 private:
151 nonvirtual strong_ordering THREEWAYCOMPARE_ (const InternetMediaType& rhs) const;
152
153 public:
154 /**
155 * @see Characters::ToString ();
156 */
157 nonvirtual String ToString () const;
158
159 private:
160 AtomType fType_;
161 AtomType fSubType_;
162 optional<AtomType> fSuffix_;
163 Containers::Mapping<String, String> fParameters_{String::EqualsComparer{Characters::eCaseInsensitive}};
164 };
165 template <>
166 nonvirtual String InternetMediaType::As () const;
167 template <>
168 nonvirtual wstring InternetMediaType::As () const;
169
170}
171
172namespace std {
173 template <>
174 struct hash<Stroika::Foundation::DataExchange::InternetMediaType> {
175 size_t operator() (const Stroika::Foundation::DataExchange::InternetMediaType& arg) const;
176 };
177}
179 template <>
180 struct DefaultSerializer<Stroika::Foundation::DataExchange::InternetMediaType> {
182 };
183}
184
185/*
186 ********************************************************************************
187 ***************************** Implementation Details ***************************
188 ********************************************************************************
189 */
190#include "InternetMediaType.inl"
191
192#endif /*_Stroika_Foundation_DataExchange_InternetMediaType_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual RETURN_TYPE GetType() const
Gets the primary (major) type of the full internet media type (as a string or atom)
nonvirtual T As() const
convert to type T supported types: o String o wstring
nonvirtual optional< RETURN_TYPE > GetSuffix() const
this is the +XXX part of the internet media type (e.g. +xml) and is often omitted (but note this omit...
STL namespace.