All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros 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 "hext/Visibility.h"
22 
23 #include <memory>
24 #include <string>
25 #include <utility>
26 
27 
28 namespace hext {
29 
30 
31 /// Abstract base for every StringPipe. A StringPipe transforms a given
32 /// string into another one, for example a TrimPipe removes all left and right
33 /// whitespace from a string. StringPipes can be chained ("linked list").
34 ///
35 /// Note: You probably don't want to inherit from this class directly, unless
36 /// you want to provide your own StringPipe::clone() method. If your
37 /// subclass has a copy constructor, you can extend from
38 /// hext::Cloneable<YourSubclass, hext::StringPipe> which provides a
39 /// generic clone method.
41 {
42 public:
43  StringPipe() noexcept;
44  StringPipe(const StringPipe& other);
45  StringPipe(StringPipe&&) noexcept = default;
46  StringPipe& operator=(const StringPipe& other);
47  StringPipe& operator=(StringPipe&&) noexcept = default;
48  virtual ~StringPipe() noexcept = default;
49 
50  /// Clones derived object.
51  virtual std::unique_ptr<StringPipe> clone() const = 0;
52 
53  /// Transforms str.
54  ///
55  /// Note: Use StringPipe::pipe to apply all transformations in this chain.
56  virtual std::string transform(std::string str) const = 0;
57 
58  /// Calls StringPipe::transform successively until the whole StringPipe chain
59  /// was traversed.
60  ///
61  /// @returns The result of applying this StringPipe and every connected
62  /// one to str.
63  std::string pipe(std::string str) const;
64 
65  /// Append a StringPipe at the end of the chain.
66  void append(std::unique_ptr<StringPipe> pipe) noexcept;
67 
68  /// Construct a StringPipe at the end of the chain.
69  template<typename StringPipeType, typename... Args>
70  void emplace(Args&&... arg)
71  {
72  this->append(std::make_unique<StringPipeType>(std::forward<Args>(arg)...));
73  }
74 
75 private:
76  /// The next StringPipe in this chain.
77  std::unique_ptr<StringPipe> next_;
78 };
79 
80 
81 } // namespace hext
82 
83 
84 #endif // HEXT_STRING_PIPE_H_INCLUDED
85 
Abstract base for every StringPipe.
Definition: StringPipe.h:40
void emplace(Args &&...arg)
Construct a StringPipe at the end of the chain.
Definition: StringPipe.h:70
Defines HEXT_PUBLIC and HEXT_PRIVATE.
#define HEXT_PUBLIC
Definition: Visibility.h:26