All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
StringPipe.h
Go to the documentation of this file.
1 // Copyright 2015, 2016 Thomas Trapp
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEXT_STRING_PIPE_H_INCLUDED
16 #define HEXT_STRING_PIPE_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::StringPipe
20 
21 #include <memory>
22 #include <string>
23 #include <utility>
24 
25 
26 namespace hext {
27 
28 
29 /// Abstract base for every StringPipe. A StringPipe transforms a given
30 /// string into another one, for example a TrimPipe removes all left and right
31 /// whitespace from a string. StringPipes can be chained ("linked list").
32 ///
33 /// Note: You probably don't want to inherit from this class directly, unless
34 /// you want to provide your own StringPipe::clone() method. If your
35 /// subclass has a copy constructor, you can extend from
36 /// hext::Cloneable<YourSubclass, hext::StringPipe> which provides a
37 /// generic clone method.
39 {
40 public:
41  StringPipe() noexcept;
42  StringPipe(const StringPipe& other);
43  StringPipe(StringPipe&&) noexcept = default;
44  StringPipe& operator=(const StringPipe& other);
45  StringPipe& operator=(StringPipe&&) noexcept = default;
46  virtual ~StringPipe() noexcept = default;
47 
48  /// Clones derived object.
49  virtual std::unique_ptr<StringPipe> clone() const = 0;
50 
51  /// Transforms str.
52  ///
53  /// Note: Use StringPipe::pipe to apply all transformations in this chain.
54  virtual std::string transform(std::string str) const = 0;
55 
56  /// Calls StringPipe::transform successively until the whole StringPipe chain
57  /// was traversed.
58  ///
59  /// @returns The result of applying this StringPipe and every connected
60  /// one to str.
61  std::string pipe(std::string str) const;
62 
63  /// Append a StringPipe at the end of the chain.
64  void append(std::unique_ptr<StringPipe> pipe) noexcept;
65 
66  /// Construct a StringPipe at the end of the chain.
67  template<typename StringPipeType, typename... Args>
68  void emplace(Args&&... arg)
69  {
70  this->append(std::make_unique<StringPipeType>(std::forward<Args>(arg)...));
71  }
72 
73 private:
74  /// The next StringPipe in this chain.
75  std::unique_ptr<StringPipe> next_;
76 };
77 
78 
79 } // namespace hext
80 
81 
82 #endif // HEXT_STRING_PIPE_H_INCLUDED
83 
StringPipe() noexcept
Abstract base for every StringPipe.
Definition: StringPipe.h:38
void emplace(Args &&...arg)
Construct a StringPipe at the end of the chain.
Definition: StringPipe.h:68
StringPipe & operator=(const StringPipe &other)
void append(std::unique_ptr< StringPipe > pipe) noexcept
Append a StringPipe at the end of the chain.
virtual ~StringPipe() noexcept=default
virtual std::unique_ptr< StringPipe > clone() const =0
Clones derived object.
virtual std::string transform(std::string str) const =0
Transforms str.
std::string pipe(std::string str) const
Calls StringPipe::transform successively until the whole StringPipe chain was traversed.