aboutsummaryrefslogtreecommitdiff
path: root/odb/emitter.hxx
blob: f68253be1a245f2603a4fe8e2add85ceb8558ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// file      : odb/emitter.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#ifndef ODB_EMITTER_HXX
#define ODB_EMITTER_HXX

#include <string>
#include <sstream>

// Emit a code construct one line at a time.
//
struct emitter
{
  virtual void
  pre ();

  virtual void
  line (const std::string&) = 0;

  virtual void
  post ();
};

// Send output line-by-line (std::endl marker) to the emitter.
//
class emitter_ostream: public std::ostream
{
public:
  emitter_ostream (emitter& e): std::ostream (&buf_), buf_ (e) {}

private:
  class streambuf: public std::stringbuf
  {
  public:
    streambuf (emitter& e): e_ (e) {}

    virtual int
    sync ();

  private:
    emitter& e_;
  };

  streambuf buf_;
};

#endif // ODB_EMITTER_HXX