Apache Mesos
representation.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_REPRESENTATION_HPP__
14 #define __STOUT_REPRESENTATION_HPP__
15 
16 #include <functional>
17 
18 // The class template `Representation` is a generic base class to support types
19 // that have multiple human-readable representations of itself.
20 //
21 // `Time` is a good example of such a type. Even though the __underlying__
22 // representation may be a `std::chrono::time_point` or even a `time_t`, there
23 // are multiple formats in which we can print them out.
24 //
25 // NOTE: These classes are light-weight objects that simply holds a reference to
26 // a type `T`, and therefore cannot be used with a temporary.
27 //
28 // NOTE: The pattern is to inherit from `Representation`, and pull in
29 // `Representation`'s constructors with using declarations. The explicit `using`
30 // declarations are necessary since inheritance does not inherit the base class'
31 // constructors. Also note that a type alias is not sufficient, since we need to
32 // give rise to distinct types.
33 //
34 // Example:
35 //
36 // ```
37 // // Two different representations of `Time`.
38 //
39 // struct RFC1123 : Representation<Time>
40 // {
41 // using Representation<Time>::Representation;
42 // };
43 //
44 // struct RFC3339 : Representation<Time>
45 // {
46 // using Representation<Time>::Representation;
47 // };
48 //
49 // // `operator<<` for the two different representations of `Time`.
50 //
51 // std::ostream& operator<<(std::ostream& stream, const RFC1123& rfc1123)
52 // {
53 // const Time& t = rfc1123;
54 // // Print in `RFC1123` format.
55 // }
56 //
57 // std::ostream& operator<<(std::ostream& stream, const RFC3339& rfc3339)
58 // {
59 // const Time& t = rfc1123;
60 // // Print in `RFC3339` format.
61 // }
62 //
63 // int main()
64 // {
65 // Time t = ...;
66 // std::cout << RFC1123(t); // Print in `RFC1123` format.
67 // std::cout << RFC3339(t); // Print in `RFC1123` format.
68 // }
69 // ```
70 
71 template <typename T>
72 struct Representation : std::reference_wrapper<const T>
73 {
74  // We pull in `std::reference_wrapper`'s constructors since inheritance does
75  // not inherit the base class' constructors.
76  using std::reference_wrapper<const T>::reference_wrapper;
77 
78  explicit Representation(const T& t) : std::reference_wrapper<const T>(t) {}
79 
80  // Disallow rebinding.
81  Representation& operator=(const Representation&) = delete;
83 };
84 
85 #endif // __STOUT_REPRESENTATION_HPP__
Representation & operator=(const Representation &)=delete
Definition: type_utils.hpp:550
Representation(const T &t)
Definition: representation.hpp:78
Definition: representation.hpp:72