CUBRID Engine  latest
printer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * printer.hpp - printing classes
21  */
22 
23 #ifndef _PRINTER_HPP_
24 #define _PRINTER_HPP_
25 
26 #include "string_buffer.hpp"
27 #include <string>
28 
29 /*
30  * print_output : interface class to print contents
31  * operator () : template variadic operator to allows printf-like syntax
32  * it automaticaly calls virtual flush () method which needs to be implemented in specialized class
33  */
35 {
36  protected:
38 
39  public:
41 
43  {
44  assert (m_sb.len () == 0);
45  }
46 
47  virtual int flush (void) = 0;
48 
50  {
51  return &m_sb;
52  }
53 
54  void operator+= (const char ch);
55 
56  template<typename... Args> inline int operator() (Args &&... args);
57 };
58 
59 
60 template<typename... Args> int print_output::operator() (Args &&... args)
61 {
62  m_sb (std::forward<Args> (args)...);
63 
64  int res = flush ();
65 
66  return res;
67 }
68 
69 /*
70  * file_print_output : ouput to a file
71  */
73 {
74  private:
75  FILE *output_file;
76 
77  public:
78  file_print_output (FILE *fp);
80 
81  static file_print_output &std_output (void);
82  int flush (void);
83 };
84 
85 
86 /*
87  * string_print_output : ouput to a string buffer
88  */
89 
91 {
92  public:
95 
96  int flush (void);
97 
98  const char *get_buffer () const
99  {
100  return m_sb.get_buffer ();
101  }
102 
103  void clear (void)
104  {
105  m_sb.clear ();
106  }
107 };
108 
109 #endif // _PRINTER_HPP_
string_buffer m_sb
Definition: printer.hpp:37
int operator()(Args &&...args)
Definition: printer.hpp:60
virtual int flush(void)=0
#define assert(x)
string_buffer * grab_string_buffer(void)
Definition: printer.hpp:49
FILE * output_file
Definition: printer.hpp:75
void operator+=(const char ch)
Definition: printer.cpp:25
const char * get_buffer() const
size_t len() const
void clear(void)
Definition: printer.hpp:103
const char * get_buffer() const
Definition: printer.hpp:98