Simple-Log  alpha-v0.7
ISink.hpp
Go to the documentation of this file.
1 // Copyright Dominic Koepke 2021 - 2021.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // https://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef SL_LOG_ISINK_HPP
7 #define SL_LOG_ISINK_HPP
8 
9 #pragma once
10 
11 #include "Record.hpp"
12 
13 #include <stdexcept>
14 #include <type_traits>
15 
16 namespace sl::log
17 {
27  template <Record TRecord>
28  class ISink
29  {
30  public:
34  using Record_t = std::remove_cvref_t<TRecord>;
35 
39  ISink(const ISink&) = delete;
43  ISink& operator =(const ISink&) = delete;
44 
48  virtual ~ISink() noexcept = default;
49 
55  virtual void log(const Record_t& record) = 0;
56 
62  virtual void setEnabled(bool enable = true) noexcept = 0;
63 
68  [[nodiscard]]
69  virtual bool isEnabled() const noexcept = 0;
70 
71  protected:
75  ISink() = default;
79  ISink(ISink&&) = default;
83  ISink& operator =(ISink&&) = default;
84  };
85 
93  template <Record TRecord, std::derived_from<ISink<TRecord>> TSink>
95  {
96  public:
101  explicit ScopedSinkDisabling(TSink& sink) noexcept :
102  m_Sink{ &sink }
103  {
104  m_Sink->setEnabled(false);
105  }
106 
111  {
112  if (m_Sink)
113  {
114  m_Sink->setEnabled();
115  m_Sink = nullptr;
116  }
117  }
118 
123 
128 
133  {
134  *this = std::move(other);
135  }
136 
141  {
142  using std::swap;
143  swap(m_Sink, other.m_Sink);
144  return *this;
145  }
146 
151  [[nodiscard]]
152  TSink& operator *() const noexcept
153  {
154  return *m_Sink;
155  }
156 
161  [[nodiscard]]
162  TSink* operator ->() const noexcept
163  {
164  return m_Sink;
165  }
166 
171  [[nodiscard]]
172  TSink* get() const noexcept
173  {
174  return m_Sink;
175  }
176 
177  private:
178  TSink* m_Sink = nullptr;
179  };
180 
181  class SinkException final :
182  public std::runtime_error
183  {
184  public:
185  explicit SinkException(const std::string& message) :
186  runtime_error{ message }
187  {
188  }
189 
190  explicit SinkException(const char* message) :
191  runtime_error{ message }
192  {
193  }
194  };
195 
197 }
198 
199 #endif
Sink interface class.
Definition: ISink.hpp:29
virtual ~ISink() noexcept=default
virtual default destructor
virtual void log(const Record_t &record)=0
virtual log function
virtual bool isEnabled() const noexcept=0
Checks if the Sink object is enabled.
ISink & operator=(const ISink &)=delete
Deleted copy-assign operator.
std::remove_cvref_t< TRecord > Record_t
Used Record type.
Definition: ISink.hpp:34
virtual void setEnabled(bool enable=true) noexcept=0
Enables or disables the Sink object.
ISink(const ISink &)=delete
Deleted copy-constructor.
Wrapper class which disables Sinks on construction and enables them on destruction.
Definition: ISink.hpp:95
ScopedSinkDisabling(TSink &sink) noexcept
Constructor which disables passed sink.
Definition: ISink.hpp:101
~ScopedSinkDisabling() noexcept
Constructor which enables passed sink.
Definition: ISink.hpp:110
ScopedSinkDisabling(ScopedSinkDisabling &&other) noexcept
Move constructor.
Definition: ISink.hpp:132
ScopedSinkDisabling(const ScopedSinkDisabling &)=delete
Deleted copy constructor.
TSink * get() const noexcept
Returns a pointer to the Sink instance.
Definition: ISink.hpp:172
Definition: ISink.hpp:183
SinkException(const std::string &message)
Definition: ISink.hpp:185
SinkException(const char *message)
Definition: ISink.hpp:190
concept Record
Concept which all the necessary concepts for Record types.
Definition: Record.hpp:204
Definition: BasicSink.hpp:22