HermesCommon  3.0
mixins.h
Go to the documentation of this file.
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://www.hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef __HERMES_COMMON_MIXINS_H
23 #define __HERMES_COMMON_MIXINS_H
24 
25 #include "common.h"
26 #include "exceptions.h"
27 #include "api.h"
28 
29 namespace Hermes
30 {
38  namespace Mixins
43  {
46  class HERMES_API StateQueryable
47  {
48  public:
50  virtual bool isOkay() const = 0;
51 
53  virtual std::string getClassName() const = 0;
54 
56  void check() const;
57  };
58 
62  class HERMES_API Loggable
63  {
64  private:
65  typedef void(*callbackFn)(const char*);
66 
67  public:
68  Loggable(bool verbose_output = false, callbackFn verbose_callback = NULL, bool add_newline = true);
69 
70  void set_file_output_only(bool onOff);
72  void set_timestamps(bool onOff);
74  void set_erase_on_beginning(bool onOff);
75  void set_logFile_name(const char* filename);
76  void set_logFile_name(std::string filename);
77  static void set_static_logFile_name(const char* filename);
78  static void set_static_logFile_name(std::string filename);
79 
80  void info(const char* msg, ...) const;
81  void info_if(bool cond, const char* msg, ...) const;
82  void warn(const char* msg, ...) const;
83  void warn_if(bool cond, const char* msg, ...) const;
84  void error(const char* msg, ...) const;
85  void error_if(bool cond, const char* msg, ...) const;
86 
89  virtual void set_verbose_output(bool to_set);
90 
92  bool get_verbose_output() const;
93 
97  virtual void set_verbose_callback(callbackFn callback);
98 
100  callbackFn get_verbose_callback() const;
101 
103  class HERMES_API Static
104  {
105  public:
106  static void info(const char* msg, ...);
107  static void warn(const char* msg, ...);
108  static void error(const char* msg, ...);
109  };
110 
112  char* logFileName;
113  static char* staticLogFileName;
114  protected:
115 
116  /* file operations */
117  void hermes_fwrite(const void* ptr, size_t size, size_t nitems, FILE* stream) const;
118  void hermes_fread(void* ptr, size_t size, size_t nitems, FILE* stream) const;
119 
120  private:
122 
125  bool write_console(const char code, const char* text) const;
126 
128  class HERMES_API HermesLogEventInfo
129  {
130  public:
131  HermesLogEventInfo(const char code, const char* src_function, const char* src_file, const int src_line);
133  const char code;
135  const char* src_function;
137  const char* src_file;
139  const int src_line;
140  };
141 
142  HermesLogEventInfo* hermes_build_log_info(char event) const;
143 
144  static std::map<std::string, bool> logger_written;
145 
147 
153  void hermes_log_message(const char code, const char* msg) const;
154 
157  bool verbose_output;
158 
160  bool file_output_only;
161 
163  callbackFn verbose_callback;
164 
166  bool print_timestamps;
167 
169  bool erase_on_beginning;
170 
172  bool log_file_written;
173 
174  bool add_newline;
175  };
176 
183  class HERMES_API TimeMeasurable
184  {
185  public:
187  TimeMeasurable(const char *name = nullptr);
188 
191  {
193  HERMES_SKIP
194  };
195 
197  const TimeMeasurable& reset();
199  const TimeMeasurable& tick_reset();
201  const TimeMeasurable& tick(TimeMeasurable::TimerPeriodTickType type = HERMES_ACCUMULATE);
202 
204  const std::string& name() const;
205 
207  double accumulated() const;
208 
210  std::string accumulated_str() const;
211 
213 
214  double last() const;
215 
217  std::string last_str() const;
218 
219  private:
220 #ifdef _WINDOWS //Windows
221  typedef uint64_t SysTime;
223  double frequency;
224 #else //Linux
225  typedef timespec SysTime;
226 #endif
227  const std::string period_name;
230  double last_period;
232  SysTime last_time;
234  double accum;
235 
237  SysTime get_time() const;
239  double period_in_seconds(const SysTime& begin, const SysTime& end) const;
241  std::string to_string(const double time) const;
242  };
243 
246  class HERMES_API IntegrableWithGlobalOrder
247  {
248  public:
251  virtual void set_global_integration_order(unsigned int order);
252  bool global_integration_order_set;
253  unsigned int global_integration_order;
254  };
255 
258  class HERMES_API SettableComputationTime
259  {
260  public:
263  virtual void set_time(double time);
264  virtual void set_time_step(double time_step);
265  double time;
266  double time_step;
267  };
268 
271  class HERMES_API OutputAttachable
272  {
273  public:
276  virtual bool on_initialization();
278  virtual bool on_step_begin();
280  virtual bool on_initial_step_end();
282  virtual bool on_step_end();
284  virtual bool on_finish();
285 
286  template<typename T>
287  class Parameter
288  {
289  private:
290  T* value;
291  friend class OutputAttachable;
292  };
293 
294  template<typename T>
295  const T& get_parameter_value(const Parameter<T>& parameter);
296 
297  template<typename T>
298  T& get_parameter_value(Parameter<T>& parameter);
299 
300  protected:
301  template<typename T>
302  void set_parameter_value(Parameter<T>& parameter, T* value);
303  };
304  }
305 
309 
313 }
314 #endif
General namespace for the Hermes library.
Main Hermes API.
Class using time measurement Can be used directly (is not abstract), so one can use e...
Definition: mixins.h:183
Class that allows overriding integration order in its discrete problems Internal. ...
Definition: mixins.h:246
char * logFileName
Logfile name.
Definition: mixins.h:112
File containing definition of exceptions classes.
TimerPeriodTickType
Tick type. Used by the class Hermes::TimePeriod.
Definition: mixins.h:190
Class that allows for attaching any method to particular parts of its functionality. Internal.
Definition: mixins.h:271
File containing common definitions, and basic global enums etc. for HermesCommon. ...
Class that allows overriding integration order in its discrete problems Internal. ...
Definition: mixins.h:258
Accumulate a period between ticks.
Definition: mixins.h:192
For static logging.
Definition: mixins.h:103
Class the output of which is loggable, i.e. that uses functionality of info(), warn() Contains the cl...
Definition: mixins.h:62