formater.hh 3.93 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2012, 2013, 2016 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
4
5
6
7
8
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19

20
#pragma once
21

22
#include <spot/misc/common.hh>
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <vector>

namespace spot
{
  class printable
  {
  public:
32
    virtual ~printable()
33
34
    {
    }
35

36
37
38
39
40
41
42
43
44
45
46
    virtual void
    print(std::ostream&, const char*) const = 0;
  };


  template <class T>
  class printable_value: public printable
  {
  protected:
    T val_;
  public:
47
    const T& val() const
48
49
50
51
    {
      return val_;
    }

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    T& val()
    {
      return val_;
    }

    operator const T&() const
    {
      return val();
    }

    operator T&()
    {
      return val();
    }

67
68
69
70
71
72
73
74
    printable_value&
    operator=(const T& new_val)
    {
      val_ = new_val;
      return *this;
    }

    virtual void
75
    print(std::ostream& os, const char*) const override
76
77
78
79
80
81
82
83
84
85
    {
      os << val_;
    }
  };

  /// The default callback simply writes "%c".
  class printable_id: public printable
  {
  public:
    virtual void
86
    print(std::ostream& os, const char* x) const override
87
88
89
90
91
92
93
94
95
96
    {
      os << '%' << *x;
    }
  };

  /// Called by default for "%%" and "%\0".
  class printable_percent: public printable
  {
  public:
    virtual void
97
    print(std::ostream& os, const char*) const override
98
99
100
101
102
103
    {
      os << '%';
    }
  };


104
  class SPOT_API formater
105
106
107
108
109
110
111
112
113
114
115
  {
    printable_id id;
    printable_percent percent;
  public:

    formater()
      : has_(256), call_(256, &id)
    {
      call_['%'] = call_[0] = &percent;
    }

116
117
118
119
    virtual ~formater()
    {
    }

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    /// \brief Scan the %-sequences occuring in \a fmt.
    ///
    /// Set has['c'] for each %c in \a fmt.   \a has must
    /// be 256 wide.
    /// @{
    void
    scan(const char* fmt, std::vector<bool>& has) const;

    void
    scan(const std::string& fmt, std::vector<bool>& has) const
    {
      scan(fmt.c_str(), has);
    }
    /// @}

135
    /// Collect the %-sequences occurring in \a fmt.
136
    /// @{
137
    void
138
    prime(const char* fmt);
139
140
141
142
143
144

    void
    prime(const std::string& fmt)
    {
      prime(fmt.c_str());
    }
145
    /// @}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

    /// Whether %c occurred in the primed formats.
    bool
    has(char c) const
    {
      return has_[c];
    }

    /// Declare a callback function for %c.
    void
    declare(char c, const printable* f)
    {
      call_[c] = f;
    }

    /// Remember where to output any string.
    void
    set_output(std::ostream& output)
    {
      output_ = &output;
    }

    /// Expand the %-sequences in \a fmt, write the result on \a output_.
    std::ostream&
170
    format(const char* fmt);
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

    /// Expand the %-sequences in \a fmt, write the result on \a output.
    std::ostream&
    format(std::ostream& output, const char* fmt)
    {
      set_output(output);
      return format(fmt);
    }

    /// Expand the %-sequences in \a fmt, write the result on \a output_.
    std::ostream&
    format(const std::string& fmt)
    {
      return format(fmt.c_str());
    }

    /// Expand the %-sequences in \a fmt, write the result on \a output.
    std::ostream&
    format(std::ostream& output, const std::string& fmt)
    {
      return format(output, fmt.c_str());
    }

  private:
    std::vector<bool> has_;
    std::vector<const printable*> call_;
  protected:
    std::ostream* output_;
  };
}