-
Notifications
You must be signed in to change notification settings - Fork 4
/
record.h
174 lines (151 loc) · 3.51 KB
/
record.h
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
/**
* Record.h
*
* Class that wraps a log record for use in Yothalot
*
* @author Emiel Bruijntjes <[email protected]>
* @copyright 2015 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include "recorditerator.h"
/**
* Class definition
*/
class Record :
public Php::Base,
public Php::Countable,
public Php::ArrayAccess,
public Php::Traversable
{
private:
/**
* The record being wrapped
* @var std::shared_ptr<Yothalot::Record>
*/
std::shared_ptr<Yothalot::Record> _record;
public:
/**
* Constructor
* @param record
*/
Record(const std::shared_ptr<Yothalot::Record> &record) :
_record(record) {}
/**
* Destructor
*/
virtual ~Record() {}
/**
* The record identifier
* @return Php::Value
*/
Php::Value identifier() const
{
return (int64_t)_record->identifier();
}
/**
* The record size in bytes
* @return Php::Value
*/
Php::Value size() const
{
return (int64_t)_record->bytes();
}
/**
* Retrieve the number of items in the record
* @return long
*/
virtual long count() override
{
return _record->size();
}
/**
* Retrieve the number of fields in the record
* @return Php::Value
*/
Php::Value fields() const
{
return (int64_t)_record->size();
}
/**
* Convert the record to an array
* @return Php::Value
*/
Php::Value array() const
{
// result value
Php::Array result;
int i = 0;
// iterate over all values
for (auto iter = _record->begin(); iter != _record->end(); ++iter)
{
// check type
if (iter->isNull()) result.set(i, nullptr);
else if (iter->isNumber()) result.set(i, iter->number());
else if (iter->isString()) result.set(i, iter->string());
++i;
}
// done
return result;
}
/**
* Check if a member is set
* @param key
* @return bool
*/
virtual bool offsetExists(const Php::Value &key) override
{
// convert to int
int index = key;
// check if withing range
return index >= 0 && index < (int)_record->size();
}
/**
* Set a member
* @param key
* @param value
*/
virtual void offsetSet(const Php::Value &key, const Php::Value &value) override
{
// not possible
Php::error << "Impossible to set Yothalot\\Record fields" << std::flush;
}
/**
* Retrieve a member
* @param key
* @return value
*/
virtual Php::Value offsetGet(const Php::Value &key) override
{
// convert to int
int index = key;
// check type
if (_record->isNull(index)) return nullptr;
if (_record->isNumber(index)) return _record->number(index);
if (_record->isString(index)) return _record->string(index);
// not possible
return nullptr;
}
/**
* Remove a member
* @param key
*/
virtual void offsetUnset(const Php::Value &key) override
{
// not possible
Php::error << "Impossible to unset Yothalot\\Record fields" << std::flush;
}
/**
* Get the iterator to traverse the array
* @return Iterator
*/
virtual Php::Iterator *getIterator() override
{
return new RecordIterator(this, _record);
}
};