77#ifndef LIB_MGIS_CONTEXT_HXX
88#define LIB_MGIS_CONTEXT_HXX 1
99
10+ #include < memory>
11+ #include < variant>
1012#include < ostream>
1113#include " MGIS/Config.hxx"
14+ #include " MGIS/Raise.hxx"
15+ #include " MGIS/LogStream.hxx"
1216#include " MGIS/VerbosityLevel.hxx"
1317#include " MGIS/ErrorBacktrace.hxx"
1418
@@ -33,9 +37,32 @@ namespace mgis {
3337 * to debug a specific rm.
3438 *
3539 * The default logging stream is the one returned by the
36- * `mgis::getLogStream ` free function.
40+ * `mgis::getDefaultLogStream ` free function.
3741 */
3842 struct MGIS_EXPORT Context final : public ErrorBacktrace {
43+ enum FailureHandlerPolicy { ABORT, RAISE };
44+ template <FailureHandlerPolicy policy>
45+ struct FailureHandler {
46+ FailureHandler (Context &c) : ctx(c) {}
47+ template <typename T>
48+ constexpr T &&operator ()(std::optional<T> &&v) const {
49+ if (isInvalid (v)) {
50+ raise (this ->ctx .getErrorMessage ());
51+ }
52+ return std::move (*v);
53+ }
54+ template <typename T>
55+ constexpr T &operator ()(OptionalReference<T> &&v) const {
56+ if (isInvalid (v)) {
57+ raise (this ->ctx .getErrorMessage ());
58+ }
59+ return *v;
60+ }
61+
62+ private:
63+ // ! \brief reference to the context that created the failure handler
64+ Context &ctx;
65+ };
3966 /* !
4067 * \brief default constructor
4168 *
@@ -55,6 +82,77 @@ namespace mgis {
5582 * \param[in] l: the new verbose level
5683 */
5784 void setVerbosityLevel (const VerbosityLevel) noexcept ;
85+ /* !
86+ * \return a failure handler
87+ * \param[in] policy: policy used to treat a failure
88+ * \note the context must outlive the failure hander
89+ */
90+ template <FailureHandlerPolicy policy = FailureHandlerPolicy::RAISE>
91+ constexpr FailureHandler<policy> getFailureHandler () {
92+ return {*this };
93+ }
94+ /* !
95+ * \brief set the current log stream.
96+ * \param[in] s: log stream
97+ * \note the user is responsible for ensuring that the given object is alive
98+ */
99+ void setLogStream (std::ostream &) noexcept ;
100+ /* !
101+ * \brief set the current log stream.
102+ * \param[in] s: log stream
103+ * \note if an empty shared pointer is given, the log stream is reset to the
104+ * default one, i.e. the one returned by the `mgis::getDefaultLogStream`
105+ * free function.
106+ */
107+ void setLogStream (std::shared_ptr<std::ostream>) noexcept ;
108+ // ! \brief reset the default log stream
109+ void resetLogStream () noexcept ;
110+ /* !
111+ * \brief disable the default log stream
112+ *
113+ * \note logging is disable by creating a no-op output stream
114+ */
115+ void disableLogStream () noexcept ;
116+ /* !
117+ * \return the current log stream
118+ *
119+ * \note if no log stream is set, the default one is returned. See
120+ * `getDefaultLogStream` for details.
121+ */
122+ [[nodiscard]] std::ostream &log () noexcept ;
123+ /* !
124+ * \brief display the given arguments in the log stream if the current
125+ * verbosity level (as returned by the `getVerbosityLevel` method) is
126+ * greater than a minimal one.
127+ *
128+ * \tparam Args: types of the arguments
129+ * \return the current log stream
130+ * \param[in] l: minimal verbosity level
131+ * \param[in] args: streamed object
132+ * \note note nothing is displayed if the current verbositiy
133+ * level is below the first argument
134+ */
135+ template <typename ... Args>
136+ std::ostream &log (const VerbosityLevel, Args &&...) noexcept ;
137+ /* !
138+ * \brief a simple wrapper around the `log` method to print a warning
139+ *
140+ * \tparam Args: types of the arguments
141+ * \param[in] args: streamed object
142+ */
143+ template <typename ... Args>
144+ void warning (Args &&...) noexcept ;
145+ /* !
146+ * \brief a simple wrapper around the `log` method which sets the minimun
147+ * verbosity level to `verboseDebug`
148+ *
149+ * \tparam Args: types of the arguments
150+ * \param[in] args: streamed object
151+ * \note note nothing is displayed if the current verbositiy level is below
152+ * `verboseDebug`
153+ */
154+ template <typename ... Args>
155+ void debug (Args &&...) noexcept ;
58156 // ! \brief destructor
59157 ~Context () noexcept override ;
60158
@@ -64,13 +162,19 @@ namespace mgis {
64162 Context (const Context &) = delete ;
65163 Context &operator =(Context &&) = delete ;
66164 Context &operator =(const Context &) = delete ;
165+ // ! \brief current log stream
166+ std::variant<std::monostate, std::ostream *, std::shared_ptr<std::ostream>>
167+ log_stream;
67168 /* !
68- * \brief local level of verbosity, initialize by the global option
69- * returned by the `getVerbosityLevel` function
169+ * \brief local level of verbosity, initialize by the
170+ * global option returned by the `getVerbosityLevel`
171+ * function
70172 */
71173 VerbosityLevel verbosity;
72174 }; // end of class Context
73175
74176} // end of namespace mgis
75177
178+ #include " MGIS/Context.ixx"
179+
76180#endif /* LIB_MGIS_CONTEXT_HXX */
0 commit comments