summaryrefslogtreecommitdiff
path: root/tools/lldb-perf/lib/Results.h
blob: a3d467ff01a1db774dbb414796e51080e3ad657d (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
170
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
201
202
203
204
205
//===-- Results.h -----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef __PerfTestDriver_Results_h__
#define __PerfTestDriver_Results_h__

#include "lldb/lldb-forward.h"
#include <map>
#include <string>
#include <vector>

namespace lldb_perf {

class Results {
public:
  class Array;
  class Dictionary;
  class Double;
  class String;
  class Unsigned;

  class Result {
  public:
    enum class Type { Invalid, Array, Dictionary, Double, String, Unsigned };

    Result(Type type, const char *name, const char *description)
        : m_name(), m_description(), m_type(type) {
      if (name && name[0])
        m_name = name;
      if (description && description[0])
        m_description = description;
    }

    virtual ~Result() {}

    virtual void Write(Results &results) = 0;

    Array *GetAsArray() {
      if (m_type == Type::Array)
        return (Array *)this;
      return NULL;
    }
    Dictionary *GetAsDictionary() {
      if (m_type == Type::Dictionary)
        return (Dictionary *)this;
      return NULL;
    }
    Double *GetAsDouble() {
      if (m_type == Type::Double)
        return (Double *)this;
      return NULL;
    }

    String *GetAsString() {
      if (m_type == Type::String)
        return (String *)this;
      return NULL;
    }
    Unsigned *GetAsUnsigned() {
      if (m_type == Type::Unsigned)
        return (Unsigned *)this;
      return NULL;
    }

    const char *GetName() const {
      if (m_name.empty())
        return NULL;
      return m_name.c_str();
    }

    const char *GetDescription() const {
      if (m_description.empty())
        return NULL;
      return m_description.c_str();
    }

    Type GetType() const { return m_type; }

  protected:
    std::string m_name;
    std::string m_description;
    Type m_type;
  };

  typedef std::shared_ptr<Result> ResultSP;

  class Array : public Result {
  public:
    Array(const char *name, const char *description)
        : Result(Type::Array, name, description) {}

    virtual ~Array() {}

    ResultSP Append(const ResultSP &result_sp);

    void ForEach(const std::function<bool(const ResultSP &)> &callback);

    virtual void Write(Results &results) {}

  protected:
    typedef std::vector<ResultSP> collection;
    collection m_array;
  };

  class Dictionary : public Result {
  public:
    Dictionary() : Result(Type::Dictionary, NULL, NULL) {}

    Dictionary(const char *name, const char *description)
        : Result(Type::Dictionary, name, description) {}

    virtual ~Dictionary() {}

    virtual void Write(Results &results) {}

    void ForEach(const std::function<bool(const std::string &,
                                          const ResultSP &)> &callback);

    ResultSP Add(const char *name, const char *description,
                 const ResultSP &result_sp);

    ResultSP AddDouble(const char *name, const char *descriptiorn,
                       double value);

    ResultSP AddUnsigned(const char *name, const char *description,
                         uint64_t value);

    ResultSP AddString(const char *name, const char *description,
                       const char *value);

  protected:
    typedef std::map<std::string, ResultSP> collection;
    collection m_dictionary;
  };

  class String : public Result {
  public:
    String(const char *name, const char *description, const char *value)
        : Result(Type::String, name, description), m_string() {
      if (value && value[0])
        m_string = value;
    }

    virtual ~String() {}

    virtual void Write(Results &results) {}

    const char *GetValue() const {
      return m_string.empty() ? NULL : m_string.c_str();
    }

  protected:
    std::string m_string;
  };

  class Double : public Result {
  public:
    Double(const char *name, const char *description, double value)
        : Result(Type::Double, name, description), m_double(value) {}

    virtual ~Double() {}

    virtual void Write(Results &results) {}

    double GetValue() const { return m_double; }

  protected:
    double m_double;
  };

  class Unsigned : public Result {
  public:
    Unsigned(const char *name, const char *description, uint64_t value)
        : Result(Type::Unsigned, name, description), m_unsigned(value) {}

    virtual ~Unsigned() {}

    virtual void Write(Results &results) {}

    uint64_t GetValue() const { return m_unsigned; }

  protected:
    uint64_t m_unsigned;
  };

  Results() : m_results() {}

  ~Results() {}

  Dictionary &GetDictionary() { return m_results; }

  void Write(const char *path);

protected:
  Dictionary m_results;
};

} // namespace lldb_perf
#endif // #ifndef __PerfTestDriver_Results_h__