summaryrefslogtreecommitdiff
path: root/cpp/src/Slice/OutputUtil.h
blob: 6d8bf1e79de9617e48f12a5b7dabaf23f871ce12 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#ifndef OUTPUT_UTIL_H
#define OUTPUT_UTIL_H

#include <fstream>
#include <sstream>

namespace Slice
{

class NextLine { };
class StartBlock { };
class EndBlock { };
class Separator { };

extern NextLine nl;
extern StartBlock sb;
extern EndBlock eb;
extern Separator sp;

// ----------------------------------------------------------------------
// Indent
// ----------------------------------------------------------------------

class Output : ::__Ice::noncopyable
{
public:

    Output();

    void open(const char*); // Open output stream

    void print(const char*); // Print a string

    void inc(); // Increment indentation level
    void dec(); // Decrement indentation level

    void useCurrentPosAsIndent(); // Save the current position as indentation
    void zeroIndent(); // Use zero identation
    void restoreIndent(); // Restore indentation

    void nl(); // Print newline
    void sb(); // Start a block
    void eb(); // End a block
    void sp(); // Print separator

    bool operator!() const; // Check whether the output state is ok

private:

    std::ofstream out_;
    int pos_;
    int indent_;
    int indentSave_;
    bool separator_;
};

template<typename T>
Output& operator<<(Output& out, const T& val)
{
    std::ostringstream s;
    s << val;
    out.print(s.str().c_str());
    return out;
}

Output& operator<<(Output&, const NextLine&);
Output& operator<<(Output&, const StartBlock&);
Output& operator<<(Output&, const EndBlock&);
Output& operator<<(Output&, const Separator&);

}

#endif