OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [common/] [attribute.h] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sergeykhbr
/**
2
 * @file
3
 * @copyright  Copyright 2016 GNSS Sensor Ltd. All right reserved.
4
 * @author     Sergey Khabarov - sergeykhbr@gmail.com
5
 * @brief      Core attribute class declaration.
6
 */
7
 
8
#ifndef __DEBUGGER_ATTRIBUTE_H__
9
#define __DEBUGGER_ATTRIBUTE_H__
10
 
11
#include <stdint.h>
12
#include <string.h>
13
#include "iattr.h"
14
 
15
namespace debugger {
16
 
17
enum KindType {
18
        Attr_Invalid,
19
        Attr_String,
20
        Attr_Integer,
21
        Attr_UInteger,
22
        Attr_Floating,
23
        Attr_List,
24
        Attr_Data,
25
        Attr_Nil,
26
        Attr_Dict,
27
        Attr_Boolean,
28
        Attr_Interface,
29
        Attr_PyObject,
30
};
31
 
32
class AttributePairType;
33
 
34
class AttributeType : public IAttribute {
35
  public:
36
    KindType kind_;
37
    unsigned size_;
38
    union {
39
        char *string;
40
        int64_t integer;
41
        bool boolean;
42
        double floating;
43
        AttributeType *list;
44
        AttributePairType *dict;
45
        uint8_t *data;
46
        uint8_t data_bytes[8];  // Data without allocation
47
        void *py_object;
48
        IFace *iface;
49
        char *uobject;
50
    } u_;
51
 
52
    AttributeType(const AttributeType& other) {
53
        size_ = 0;
54
        clone(&other);
55
    }
56
 
57
    AttributeType() {
58
        kind_ = Attr_Invalid;
59
        size_ = 0;
60
        u_.integer = 0;
61
    }
62
    ~AttributeType() {
63
        attr_free();
64
    }
65
 
66
    void attr_free();
67
 
68
    explicit AttributeType(const char *str) {
69
        make_string(str);
70
    }
71
 
72
    explicit AttributeType(IFace *mod) {
73
        kind_ = Attr_Interface;
74
        u_.iface = mod;
75
    }
76
 
77
    explicit AttributeType(KindType type) {
78
        kind_ = type;
79
        size_ = 0;
80
        u_.integer = 0;
81
    }
82
 
83
    explicit AttributeType(bool val) {
84
        kind_ = Attr_Boolean;
85
        size_ = 0;
86
        u_.boolean = val;
87
    }
88
 
89
    AttributeType(KindType type, uint64_t v) {
90
        if (type == Attr_Integer) {
91
            make_int64(static_cast<int64_t>(v));
92
        } else if (type == Attr_UInteger) {
93
            make_uint64(v);
94
        }
95
    }
96
 
97
    unsigned size() const { return size_; }
98
 
99
    bool is_floating() const {
100
        return kind_ == Attr_Floating;
101
    }
102
 
103
    double to_float() const {
104
        return u_.floating;
105
    }
106
 
107
    bool is_integer() const {
108
        return kind_ == Attr_Integer || kind_ == Attr_UInteger;
109
    }
110
 
111
    bool is_int64() const {
112
        return kind_ == Attr_Integer;
113
    }
114
 
115
    int to_int() const {
116
        return static_cast<int>(u_.integer);
117
    }
118
 
119
    int to_uint32() const {
120
        return static_cast<uint32_t>(u_.integer);
121
    }
122
 
123
    int64_t to_int64() const {
124
        return u_.integer;
125
    }
126
 
127
    bool is_uint64() const {
128
        return kind_ == Attr_UInteger;
129
    }
130
 
131
    uint64_t to_uint64() const {
132
        return u_.integer;
133
    }
134
 
135
    bool is_bool() const {
136
        return kind_ == Attr_Boolean;
137
    }
138
 
139
    bool to_bool() const { return u_.boolean; }
140
 
141
    bool is_string() const {
142
        return kind_ == Attr_String;
143
    }
144
 
145
    const char * to_string() const {
146
        return u_.string;
147
    }
148
 
149
    bool is_equal(const char *v);
150
 
151
    // capitalize letters in string;
152
    const char * to_upper() const {
153
        if (kind_ != Attr_String) {
154
            return 0;
155
        }
156
        char *p = u_.string;
157
        while (*p) {
158
            if (p[0] >= 'a' && p[0] <= 'z') {
159
                p[0] = p[0] - 'a' + 'A';
160
            }
161
            p++;
162
        }
163
        return u_.string;
164
    }
165
 
166
    bool is_list() const {
167
        return kind_ == Attr_List;
168
    }
169
 
170
    bool is_dict() const {
171
        return kind_ == Attr_Dict;
172
    }
173
 
174
    bool is_data() const {
175
        return kind_ == Attr_Data;
176
    }
177
 
178
    bool is_iface() const {
179
        return kind_ == Attr_Interface;
180
    }
181
 
182
    IFace *to_iface() const {
183
        return u_.iface;
184
    }
185
 
186
    bool is_nil() const {
187
        return kind_ == Attr_Nil;
188
    }
189
 
190
    bool is_invalid() const {
191
        return kind_ == Attr_Invalid;
192
    }
193
 
194
 
195
    void clone(const AttributeType *v);
196
 
197
    void make_nil() {
198
        kind_ = Attr_Nil;
199
        size_ = 0;
200
        u_.integer = 0;
201
    }
202
 
203
    void make_iface(IFace *value) {
204
        kind_ = Attr_Interface;
205
        u_.iface = value;
206
    }
207
 
208
    void make_floating(double value) {
209
        kind_ = Attr_Floating;
210
        size_ = 0;
211
        u_.floating = value;
212
    }
213
 
214
    void force_to_floating() {
215
        kind_ = Attr_Floating;
216
    }
217
 
218
    void make_int64(int64_t value) {
219
        kind_ = Attr_Integer;
220
        size_ = 0;
221
        u_.integer = value;
222
    }
223
 
224
    void make_uint64(uint64_t value) {
225
        kind_ = Attr_UInteger;
226
        size_ = 0;
227
        u_.integer = value;
228
    }
229
 
230
    void make_boolean(bool value) {
231
        kind_ = Attr_Boolean;
232
        size_ = 0;
233
        u_.boolean = value;
234
    }
235
 
236
    void make_string(const char *value);
237
 
238
    void make_data(unsigned size);
239
 
240
    void make_data(unsigned size, const void *data);
241
 
242
    void make_list(unsigned size);
243
 
244
    void add_to_list(const AttributeType *item) {
245
        realloc_list(size()+1);
246
        (*this)[size()-1] = (*item);
247
    }
248
 
249
    void insert_to_list(unsigned idx, const AttributeType *item);
250
 
251
    void remove_from_list(unsigned idx);
252
 
253
    void trim_list(unsigned start, unsigned end);
254
 
255
    void swap_list_item(unsigned n, unsigned m);
256
 
257
    void realloc_list(unsigned size);
258
 
259
    void make_dict();
260
    void realloc_dict(unsigned size);
261
 
262
    // Getter:
263
    double floating() const { return u_.floating; }
264
 
265
    int64_t integer() const { return u_.integer; }
266
 
267
    const char *string() const { return u_.string; }
268
 
269
    bool boolean() const { return u_.boolean; }
270
 
271
    const AttributeType *list(unsigned idx) const {
272
        return &u_.list[idx];
273
    }
274
    AttributeType *list(unsigned idx) {
275
        return &u_.list[idx];
276
    }
277
 
278
    /* Quicksort algorithm with 'list' attribute */
279
    void sort(int idx = 0);
280
 
281
    bool has_key(const char *key) const;
282
 
283
    const AttributeType *dict_key(unsigned idx) const;
284
    AttributeType *dict_key(unsigned idx);
285
 
286
    const AttributeType *dict_value(unsigned idx) const;
287
    AttributeType *dict_value(unsigned idx);
288
 
289
    const uint8_t *data() const {
290
        if (size_ > 8) {
291
            return u_.data;
292
        }
293
        return u_.data_bytes;
294
    }
295
    uint8_t *data() {
296
        if (size_ > 8) {
297
            return u_.data;
298
        }
299
        return u_.data_bytes;
300
    }
301
 
302
    AttributeType& operator=(const AttributeType& other);
303
 
304
    /**
305
     * @brief Access to the single element of the 'list' attribute:
306
     */
307
    const AttributeType& operator[](unsigned idx) const;
308
    /** @overload */
309
    AttributeType& operator[](unsigned idx);
310
 
311
    /**
312
     * @brief Access to the single value attribute of the 'dictionary':
313
     */
314
    const AttributeType& operator[](const char *key) const;
315
    /** @overload */
316
    AttributeType& operator[](const char *key);
317
 
318
    /**
319
     * @brief Access to the single byte of the 'data' attribute:
320
     */
321
    const uint8_t& operator()(unsigned idx) const;
322
 
323
    char *to_config();
324
    void from_config(const char *str);
325
};
326
 
327
class AttributePairType {
328
  public:
329
    AttributeType key_;
330
    AttributeType value_;
331
};
332
 
333
}  // namespace debugger
334
 
335
#endif  // __DEBUGGER_ATTRIBUTE_H__

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.