Skip to content

Commit a9630ba

Browse files
committed
fix: effective passing Point by value (#197)
1 parent 5aa8be3 commit a9630ba

11 files changed

+163
-80
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## [unreleased]
3+
### Fixes
4+
- [198](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/198) - Effective passing Point by value
5+
26
## 3.12.1 [2022-08-29]
37
### Fixes
48
- [193](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/193) - Automatically adjusting point timestamp according to the setting of write precision.

src/InfluxData.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
void InfluxData::setTimestamp(long int seconds)
3131
{
32-
_timestamp = timeStampToString(seconds,9);
33-
strcat(_timestamp, "000000000");
32+
_data->timestamp = timeStampToString(seconds,9);
33+
strcat(_data->timestamp, "000000000");
3434
}
3535

3636
String InfluxData::toString() const {

src/InfluxData.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828

2929
class InfluxData : public Point {
3030
public:
31-
InfluxData(String measurement) : Point(measurement) {}
31+
InfluxData(const String &measurement) : Point(measurement) {}
3232

33-
void addValue(String key, float value) { addField(key, value); }
34-
void addValueString(String key, String value) { addField(key, value); }
33+
void addValue(const String &key, float value) { addField(key, value); }
34+
void addValueString(const String &key, String value) { addField(key, value); }
3535
void setTimestamp(long int seconds);
3636
String toString() const;
3737
};

src/InfluxDbClient.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ void InfluxDBClient::reserveBuffer(int size) {
286286
}
287287

288288
void InfluxDBClient::addZerosToTimestamp(Point &point, int zeroes) {
289-
char *ts = point._timestamp, *s;
290-
point._timestamp = new char[strlen(point._timestamp) + 1 + zeroes];
291-
strcpy(point._timestamp, ts);
292-
s = point._timestamp+strlen(ts);
289+
char *ts = point._data->timestamp, *s;
290+
point._data->timestamp = new char[strlen(point._data->timestamp) + 1 + zeroes];
291+
strcpy(point._data->timestamp, ts);
292+
s = point._data->timestamp+strlen(ts);
293293
for(int i=0;i<zeroes;i++) {
294294
*s++ = '0';
295295
}
@@ -302,17 +302,17 @@ void InfluxDBClient::checkPrecisions(Point & point) {
302302
if(!point.hasTime()) {
303303
point.setTime(_writeOptions._writePrecision);
304304
// Check different write precisions
305-
} else if(point._tsWritePrecision != WritePrecision::NoTime && point._tsWritePrecision != _writeOptions._writePrecision) {
306-
int diff = int(point._tsWritePrecision) - int(_writeOptions._writePrecision);
305+
} else if(point._data->tsWritePrecision != WritePrecision::NoTime && point._data->tsWritePrecision != _writeOptions._writePrecision) {
306+
int diff = int(point._data->tsWritePrecision) - int(_writeOptions._writePrecision);
307307
if(diff > 0) { //point has higher precision, cut
308-
point._timestamp[strlen(point._timestamp)-diff*3] = 0;
308+
point._data->timestamp[strlen(point._data->timestamp)-diff*3] = 0;
309309
} else { //point has lower precision, add zeroes
310310
addZerosToTimestamp(point, diff*-3);
311311
}
312312
}
313313
// check someone set WritePrecision on point and not on client. NS precision is ok, cause it is default on server
314-
} else if(point.hasTime() && point._tsWritePrecision != WritePrecision::NoTime && point._tsWritePrecision != WritePrecision::NS) {
315-
int diff = int(WritePrecision::NS) - int(point._tsWritePrecision);
314+
} else if(point.hasTime() && point._data->tsWritePrecision != WritePrecision::NoTime && point._data->tsWritePrecision != WritePrecision::NS) {
315+
int diff = int(WritePrecision::NS) - int(point._data->tsWritePrecision);
316316
addZerosToTimestamp(point, diff*3);
317317
}
318318
}

src/Point.cpp

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,46 @@
3030

3131
Point::Point(const String & measurement)
3232
{
33-
_measurement = escapeKey(measurement, false);
34-
_timestamp = nullptr;
35-
_tsWritePrecision = WritePrecision::NoTime;
33+
_data = std::make_shared<Data>(escapeKey(measurement, false));
3634
}
3735

3836
Point::~Point() {
39-
delete [] _measurement;
40-
delete [] _timestamp;
37+
}
38+
39+
Point::Data::Data(char * measurement) {
40+
this->measurement = measurement;
41+
timestamp = nullptr;
42+
tsWritePrecision = WritePrecision::NoTime;
43+
}
44+
45+
Point::Data::~Data() {
46+
delete [] measurement;
47+
delete [] timestamp;
48+
}
49+
50+
Point::Point(const Point &other) {
51+
*this = other;
52+
}
53+
54+
Point& Point::operator=(const Point &other) {
55+
if(this != &other) {
56+
Serial.printf("Point::operator= of %x\n", this);
57+
this->_data = other._data;
58+
}
59+
return *this;
4160
}
4261

4362
void Point::addTag(const String &name, String value) {
44-
if(_tags.length() > 0) {
45-
_tags += ',';
46-
}
47-
char *s = escapeKey(name);
48-
_tags += s;
49-
delete [] s;
50-
_tags += '=';
51-
s = escapeKey(value);
52-
_tags += s;
53-
delete [] s;
63+
if(_data->tags.length() > 0) {
64+
_data->tags += ',';
65+
}
66+
char *s = escapeKey(name);
67+
_data->tags += s;
68+
delete [] s;
69+
_data->tags += '=';
70+
s = escapeKey(value);
71+
_data->tags += s;
72+
delete [] s;
5473
}
5574

5675
void Point::addField(const String &name, long long value) {
@@ -114,14 +133,14 @@ void Point::addField(const String &name, const String &value) {
114133
}
115134

116135
void Point::putField(const String &name, const String &value) {
117-
if(_fields.length() > 0) {
118-
_fields += ',';
136+
if(_data->fields.length() > 0) {
137+
_data->fields += ',';
119138
}
120139
char *s = escapeKey(name);
121-
_fields += s;
140+
_data->fields += s;
122141
delete [] s;
123-
_fields += '=';
124-
_fields += value;
142+
_data->fields += '=';
143+
_data->fields += value;
125144
}
126145

127146
String Point::toLineProtocol(const String &includeTags) const {
@@ -130,23 +149,23 @@ String Point::toLineProtocol(const String &includeTags) const {
130149

131150
String Point::createLineProtocol(const String &incTags) const {
132151
String line;
133-
line.reserve(strLen(_measurement) + 1 + incTags.length() + 1 + _tags.length() + 1 + _fields.length() + 1 + strLen(_timestamp));
134-
line += _measurement;
152+
line.reserve(strLen(_data->measurement) + 1 + incTags.length() + 1 + _data->tags.length() + 1 + _data->fields.length() + 1 + strLen(_data->timestamp));
153+
line += _data->measurement;
135154
if(incTags.length()>0) {
136155
line += ",";
137156
line += incTags;
138157
}
139158
if(hasTags()) {
140159
line += ",";
141-
line += _tags;
160+
line += _data->tags;
142161
}
143162
if(hasFields()) {
144163
line += " ";
145-
line += _fields;
164+
line += _data->fields;
146165
}
147166
if(hasTime()) {
148167
line += " ";
149-
line += _timestamp;
168+
line += _data->timestamp;
150169
}
151170
return line;
152171
}
@@ -172,7 +191,7 @@ void Point::setTime(WritePrecision precision) {
172191
setTime((char *)nullptr);
173192
break;
174193
}
175-
_tsWritePrecision = precision;
194+
_data->tsWritePrecision = precision;
176195
}
177196

178197
void Point::setTime(unsigned long long timestamp) {
@@ -188,16 +207,16 @@ void Point::setTime(const char *timestamp) {
188207
}
189208

190209
void Point::setTime(char *timestamp) {
191-
delete [] _timestamp;
192-
_timestamp = timestamp;
210+
delete [] _data->timestamp;
211+
_data->timestamp = timestamp;
193212
}
194213

195214
void Point::clearFields() {
196-
_fields = (char *)nullptr;
197-
delete [] _timestamp;
198-
_timestamp = nullptr;
215+
_data->fields = (char *)nullptr;
216+
delete [] _data->timestamp;
217+
_data->timestamp = nullptr;
199218
}
200219

201220
void Point:: clearTags() {
202-
_tags = (char *)nullptr;
221+
_data->tags = (char *)nullptr;
203222
}

src/Point.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <Arduino.h>
3131
#include "WritePrecision.h"
3232
#include "util/helpers.h"
33+
#include <memory>
3334

3435
/**
3536
* Class Point represents InfluxDB point in line protocol.
@@ -39,6 +40,8 @@ class Point {
3940
friend class InfluxDBClient;
4041
public:
4142
Point(const String &measurement);
43+
Point(const Point &other);
44+
Point& operator=(const Point &other);
4245
virtual ~Point();
4346
// Adds string tag
4447
void addTag(const String &name, String value);
@@ -70,21 +73,27 @@ friend class InfluxDBClient;
7073
// Clear tags
7174
void clearTags();
7275
// True if a point contains at least one field. Points without a field cannot be written to db
73-
bool hasFields() const { return _fields.length() > 0; }
76+
bool hasFields() const { return _data->fields.length() > 0; }
7477
// True if a point contains at least one tag
75-
bool hasTags() const { return _tags.length() > 0; }
78+
bool hasTags() const { return _data->tags.length() > 0; }
7679
// True if a point contains timestamp
77-
bool hasTime() const { return strLen(_timestamp) > 0; }
80+
bool hasTime() const { return strLen(_data->timestamp) > 0; }
7881
// Creates line protocol with optionally added tags
7982
String toLineProtocol(const String &includeTags = "") const;
8083
// returns current timestamp
81-
String getTime() const { return _timestamp; }
84+
String getTime() const { return _data->timestamp; }
8285
protected:
83-
char *_measurement;
84-
String _tags;
85-
String _fields;
86-
char *_timestamp;
87-
WritePrecision _tsWritePrecision;
86+
class Data {
87+
public:
88+
Data(char *measurement);
89+
~Data();
90+
char *measurement;
91+
String tags;
92+
String fields;
93+
char *timestamp;
94+
WritePrecision tsWritePrecision;
95+
};
96+
std::shared_ptr<Data> _data;
8897
protected:
8998
// method for formating field into line protocol
9099
void putField(const String &name, const String &value);

0 commit comments

Comments
 (0)