Skip to content

Commit d9af857

Browse files
committed
Use string_view for point fields
1 parent ee37a90 commit d9af857

File tree

8 files changed

+149
-127
lines changed

8 files changed

+149
-127
lines changed

libs/maps/include/mrpt/maps/CGenericPointsMap.h

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ class CGenericPointsMap : public CPointsMap
5050
@{ */
5151

5252
// see docs in parent class
53-
bool registerField_float(const std::string& fieldName) override;
53+
bool registerField_float(const std::string_view& fieldName) override;
5454

5555
// see docs in parent class
56-
bool registerField_uint16(const std::string& fieldName) override;
56+
bool registerField_uint16(const std::string_view& fieldName) override;
5757

5858
/** Removes a data channel.
5959
* \return True if the field existed and was removed, false otherwise.
6060
*/
61-
bool unregisterField(const std::string& fieldName);
61+
bool unregisterField(const std::string_view& fieldName);
6262

6363
/** Returns the map of float fields: map<field_name, vector_of_data> */
64-
const std::map<std::string, mrpt::aligned_std_vector<float>>& float_fields() const
64+
const std::map<std::string_view, mrpt::aligned_std_vector<float>>& float_fields() const
6565
{
6666
return m_float_fields;
6767
}
6868
/** Returns the map of uint16_t fields: map<field_name, vector_of_data> */
69-
const std::map<std::string, mrpt::aligned_std_vector<uint16_t>>& uint16_fields() const
69+
const std::map<std::string_view, mrpt::aligned_std_vector<uint16_t>>& uint16_fields() const
7070
{
7171
return m_uint16_fields;
7272
}
@@ -111,35 +111,36 @@ class CGenericPointsMap : public CPointsMap
111111

112112
/** @name String-keyed field access virtual interface implementation
113113
@{ */
114-
bool hasPointField(const std::string& fieldName) const override;
115-
std::vector<std::string> getPointFieldNames_float() const override;
116-
std::vector<std::string> getPointFieldNames_uint16() const override;
114+
bool hasPointField(const std::string_view& fieldName) const override;
115+
std::vector<std::string_view> getPointFieldNames_float() const override;
116+
std::vector<std::string_view> getPointFieldNames_uint16() const override;
117117

118-
float getPointField_float(size_t index, const std::string& fieldName) const override;
119-
uint16_t getPointField_uint16(size_t index, const std::string& fieldName) const override;
118+
float getPointField_float(size_t index, const std::string_view& fieldName) const override;
119+
uint16_t getPointField_uint16(size_t index, const std::string_view& fieldName) const override;
120120

121-
void setPointField_float(size_t index, const std::string& fieldName, float value) override;
122-
void setPointField_uint16(size_t index, const std::string& fieldName, uint16_t value) override;
121+
void setPointField_float(size_t index, const std::string_view& fieldName, float value) override;
122+
void setPointField_uint16(
123+
size_t index, const std::string_view& fieldName, uint16_t value) override;
123124

124125
/** Appends a value to the given field.
125126
* The field must be registered.
126127
* Asserts that the field vector's size is exactly `this->size() - 1`
127128
* (i.e. you just called `insertPointFast()`).
128129
*/
129-
void insertPointField_float(const std::string& fieldName, float value) override;
130+
void insertPointField_float(const std::string_view& fieldName, float value) override;
130131
/** Appends a value to the given field.
131132
* The field must be registered.
132133
* Asserts that the field vector's size is exactly `this->size() - 1`
133134
* (i.e. you just called `insertPointFast()`).
134135
*/
135-
void insertPointField_uint16(const std::string& fieldName, uint16_t value) override;
136+
void insertPointField_uint16(const std::string_view& fieldName, uint16_t value) override;
136137

137-
void reserveField_float(const std::string& fieldName, size_t n) override;
138-
void reserveField_uint16(const std::string& fieldName, size_t n) override;
139-
void resizeField_float(const std::string& fieldName, size_t n) override;
140-
void resizeField_uint16(const std::string& fieldName, size_t n) override;
138+
void reserveField_float(const std::string_view& fieldName, size_t n) override;
139+
void reserveField_uint16(const std::string_view& fieldName, size_t n) override;
140+
void resizeField_float(const std::string_view& fieldName, size_t n) override;
141+
void resizeField_uint16(const std::string_view& fieldName, size_t n) override;
141142

142-
auto getPointsBufferRef_float_field(const std::string& fieldName) const
143+
auto getPointsBufferRef_float_field(const std::string_view& fieldName) const
143144
-> const mrpt::aligned_std_vector<float>* override
144145
{
145146
if (auto* f = CPointsMap::getPointsBufferRef_float_field(fieldName); f)
@@ -152,7 +153,7 @@ class CGenericPointsMap : public CPointsMap
152153
}
153154
return nullptr;
154155
}
155-
auto getPointsBufferRef_uint_field(const std::string& fieldName) const
156+
auto getPointsBufferRef_uint_field(const std::string_view& fieldName) const
156157
-> const mrpt::aligned_std_vector<uint16_t>* override
157158
{
158159
if (auto it = m_uint16_fields.find(fieldName); it != m_uint16_fields.end())
@@ -162,7 +163,7 @@ class CGenericPointsMap : public CPointsMap
162163
return nullptr;
163164
}
164165

165-
auto getPointsBufferRef_float_field(const std::string& fieldName)
166+
auto getPointsBufferRef_float_field(const std::string_view& fieldName)
166167
-> mrpt::aligned_std_vector<float>* override
167168
{
168169
if (auto* f = CPointsMap::getPointsBufferRef_float_field(fieldName); f)
@@ -175,7 +176,7 @@ class CGenericPointsMap : public CPointsMap
175176
}
176177
return nullptr;
177178
}
178-
auto getPointsBufferRef_uint_field(const std::string& fieldName)
179+
auto getPointsBufferRef_uint_field(const std::string_view& fieldName)
179180
-> mrpt::aligned_std_vector<uint16_t>* override
180181
{
181182
if (auto it = m_uint16_fields.find(fieldName); it != m_uint16_fields.end())
@@ -189,9 +190,9 @@ class CGenericPointsMap : public CPointsMap
189190

190191
protected:
191192
/** Map from field name to data vector */
192-
std::map<std::string, mrpt::aligned_std_vector<float>> m_float_fields;
193+
std::map<std::string_view, mrpt::aligned_std_vector<float>> m_float_fields;
193194
/** Map from field name to data vector */
194-
std::map<std::string, mrpt::aligned_std_vector<uint16_t>> m_uint16_fields;
195+
std::map<std::string_view, mrpt::aligned_std_vector<uint16_t>> m_uint16_fields;
195196

196197
/** Clear the map, erasing all the points and all fields */
197198
void internal_clear() override;

libs/maps/include/mrpt/maps/CPointsMap.h

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ class CPointsMap :
365365
* \return true if the field could effectively be added to the underlying point map class.
366366
* \sa hasPointField(), getPointFieldNames_float()
367367
*/
368-
virtual bool registerField_float(const std::string& fieldName)
368+
virtual bool registerField_float(const std::string_view& fieldName)
369369
{
370370
return fieldName == "x" || fieldName == "y" || fieldName == "z";
371371
}
@@ -376,7 +376,7 @@ class CPointsMap :
376376
* \return true if the field could effectively be added to the underlying point map class.
377377
* \sa hasPointField(), getPointFieldNames_uint16()
378378
*/
379-
virtual bool registerField_uint16(const std::string& fieldName) { return false; }
379+
virtual bool registerField_uint16(const std::string_view& fieldName) { return false; }
380380

381381
/** @} */
382382

@@ -580,22 +580,22 @@ class CPointsMap :
580580
/** Returns true if the map has a data channel with the given name.
581581
* \sa getPointField_float, getPointField_uint16
582582
*/
583-
virtual bool hasPointField(const std::string& fieldName) const
583+
virtual bool hasPointField(const std::string_view& fieldName) const
584584
{
585585
return (fieldName == "x") || (fieldName == "y") || (fieldName == "z");
586586
}
587587

588588
/** Get list of all float channel names */
589-
virtual std::vector<std::string> getPointFieldNames_float() const { return {"x", "y", "z"}; }
589+
virtual std::vector<std::string_view> getPointFieldNames_float() const { return {"x", "y", "z"}; }
590590
/** Get list of all uint16_t channel names */
591-
virtual std::vector<std::string> getPointFieldNames_uint16() const { return {}; }
591+
virtual std::vector<std::string_view> getPointFieldNames_uint16() const { return {}; }
592592

593593
/** Read the value of a float channel for a given point.
594594
* Returns 0 if field does not exist.
595595
* \exception std::exception on index out of bounds or if field exists but
596596
* is not float.
597597
*/
598-
virtual float getPointField_float(size_t index, const std::string& fieldName) const
598+
virtual float getPointField_float(size_t index, const std::string_view& fieldName) const
599599
{
600600
if (fieldName == "x") return m_x.at(index);
601601
if (fieldName == "y") return m_y.at(index);
@@ -608,7 +608,7 @@ class CPointsMap :
608608
* \exception std::exception on index out of bounds or if field exists but
609609
* is not uint16_t.
610610
*/
611-
virtual uint16_t getPointField_uint16(size_t index, const std::string& fieldName) const
611+
virtual uint16_t getPointField_uint16(size_t index, const std::string_view& fieldName) const
612612
{
613613
return 0;
614614
}
@@ -617,7 +617,7 @@ class CPointsMap :
617617
* \exception std::exception on index out of bounds or if field does not
618618
* exist or is not float.
619619
*/
620-
virtual void setPointField_float(size_t index, const std::string& fieldName, float value)
620+
virtual void setPointField_float(size_t index, const std::string_view& fieldName, float value)
621621
{
622622
if (fieldName == "x")
623623
m_x.at(index) = value;
@@ -630,41 +630,43 @@ class CPointsMap :
630630
* \exception std::exception on index out of bounds or if field does not
631631
* exist or is not uint16_t.
632632
*/
633-
virtual void setPointField_uint16(size_t index, const std::string& fieldName, uint16_t value) {}
633+
virtual void setPointField_uint16(size_t index, const std::string_view& fieldName, uint16_t value)
634+
{
635+
}
634636

635637
/** Appends a value to a float channel (for use after insertPointFast()) */
636-
virtual void insertPointField_float(const std::string& fieldName, float value) {}
638+
virtual void insertPointField_float(const std::string_view& fieldName, float value) {}
637639
/** Appends a value to a uint16_t channel (for use after insertPointFast()) */
638-
virtual void insertPointField_uint16(const std::string& fieldName, uint16_t value) {}
640+
virtual void insertPointField_uint16(const std::string_view& fieldName, uint16_t value) {}
639641

640-
virtual void reserveField_float(const std::string& fieldName, size_t n) {}
641-
virtual void reserveField_uint16(const std::string& fieldName, size_t n) {}
642-
virtual void resizeField_float(const std::string& fieldName, size_t n) {}
643-
virtual void resizeField_uint16(const std::string& fieldName, size_t n) {}
642+
virtual void reserveField_float(const std::string_view& fieldName, size_t n) {}
643+
virtual void reserveField_uint16(const std::string_view& fieldName, size_t n) {}
644+
virtual void resizeField_float(const std::string_view& fieldName, size_t n) {}
645+
virtual void resizeField_uint16(const std::string_view& fieldName, size_t n) {}
644646

645-
virtual auto getPointsBufferRef_float_field(const std::string& fieldName) const
647+
virtual auto getPointsBufferRef_float_field(const std::string_view& fieldName) const
646648
-> const mrpt::aligned_std_vector<float>*
647649
{
648650
if (fieldName == "x") return &m_x;
649651
if (fieldName == "y") return &m_y;
650652
if (fieldName == "z") return &m_z;
651653
return nullptr;
652654
}
653-
virtual auto getPointsBufferRef_uint_field([[maybe_unused]] const std::string& fieldName) const
654-
-> const mrpt::aligned_std_vector<uint16_t>*
655+
virtual auto getPointsBufferRef_uint_field([[maybe_unused]] const std::string_view& fieldName)
656+
const -> const mrpt::aligned_std_vector<uint16_t>*
655657
{
656658
return nullptr;
657659
}
658660

659-
virtual auto getPointsBufferRef_float_field(const std::string& fieldName)
661+
virtual auto getPointsBufferRef_float_field(const std::string_view& fieldName)
660662
-> mrpt::aligned_std_vector<float>*
661663
{
662664
if (fieldName == "x") return &m_x;
663665
if (fieldName == "y") return &m_y;
664666
if (fieldName == "z") return &m_z;
665667
return nullptr;
666668
}
667-
virtual auto getPointsBufferRef_uint_field([[maybe_unused]] const std::string& fieldName)
669+
virtual auto getPointsBufferRef_uint_field([[maybe_unused]] const std::string_view& fieldName)
668670
-> mrpt::aligned_std_vector<uint16_t>*
669671
{
670672
return nullptr;

libs/maps/include/mrpt/maps/CPointsMapXYZI.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,19 @@ class CPointsMapXYZI : public CPointsMap
175175

176176
/** @name String-keyed field access virtual interface implementation
177177
@{ */
178-
constexpr static const char* POINT_FIELD_INTENSITY = "intensity";
178+
constexpr static std::string_view POINT_FIELD_INTENSITY = "intensity";
179179

180-
bool hasPointField(const std::string& fieldName) const override;
181-
std::vector<std::string> getPointFieldNames_float() const override;
180+
bool hasPointField(const std::string_view& fieldName) const override;
181+
std::vector<std::string_view> getPointFieldNames_float() const override;
182182

183-
float getPointField_float(size_t index, const std::string& fieldName) const override;
184-
void setPointField_float(size_t index, const std::string& fieldName, float value) override;
183+
float getPointField_float(size_t index, const std::string_view& fieldName) const override;
184+
void setPointField_float(size_t index, const std::string_view& fieldName, float value) override;
185185

186-
void insertPointField_float(const std::string& fieldName, float value) override;
187-
void reserveField_float(const std::string& fieldName, size_t n) override;
188-
void resizeField_float(const std::string& fieldName, size_t n) override;
186+
void insertPointField_float(const std::string_view& fieldName, float value) override;
187+
void reserveField_float(const std::string_view& fieldName, size_t n) override;
188+
void resizeField_float(const std::string_view& fieldName, size_t n) override;
189189

190-
auto getPointsBufferRef_float_field(const std::string& fieldName) const
190+
auto getPointsBufferRef_float_field(const std::string_view& fieldName) const
191191
-> const mrpt::aligned_std_vector<float>* override
192192
{
193193
if (auto* f = CPointsMap::getPointsBufferRef_float_field(fieldName); f)
@@ -197,7 +197,7 @@ class CPointsMapXYZI : public CPointsMap
197197
if (fieldName == POINT_FIELD_INTENSITY) return &m_intensity;
198198
return nullptr;
199199
}
200-
auto getPointsBufferRef_float_field(const std::string& fieldName)
200+
auto getPointsBufferRef_float_field(const std::string_view& fieldName)
201201
-> mrpt::aligned_std_vector<float>* override
202202
{
203203
if (auto* f = CPointsMap::getPointsBufferRef_float_field(fieldName); f)

libs/maps/include/mrpt/maps/CPointsMapXYZIRT.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class CPointsMapXYZIRT : public CPointsMap
4545
/** @name Pure virtual interfaces to be implemented by any class derived from CPointsMap
4646
@{ */
4747

48-
constexpr static const char* POINT_FIELD_INTENSITY = "intensity";
49-
constexpr static const char* POINT_FIELD_RING_ID = "ring";
50-
constexpr static const char* POINT_FIELD_TIMESTAMP = "t";
48+
constexpr static std::string_view POINT_FIELD_INTENSITY = "intensity";
49+
constexpr static std::string_view POINT_FIELD_RING_ID = "ring";
50+
constexpr static std::string_view POINT_FIELD_TIMESTAMP = "t";
5151

5252
// By default, these method will grow all fields XYZIRT. See other methods
5353
// below.
@@ -223,25 +223,25 @@ class CPointsMapXYZIRT : public CPointsMap
223223

224224
/** @name String-keyed field access virtual interface implementation
225225
@{ */
226-
bool hasPointField(const std::string& fieldName) const override;
227-
std::vector<std::string> getPointFieldNames_float() const override;
228-
std::vector<std::string> getPointFieldNames_uint16() const override;
226+
bool hasPointField(const std::string_view& fieldName) const override;
227+
std::vector<std::string_view> getPointFieldNames_float() const override;
228+
std::vector<std::string_view> getPointFieldNames_uint16() const override;
229229

230-
float getPointField_float(size_t index, const std::string& fieldName) const override;
231-
uint16_t getPointField_uint16(size_t index, const std::string& fieldName) const override;
230+
float getPointField_float(size_t index, const std::string_view& fieldName) const override;
231+
uint16_t getPointField_uint16(size_t index, const std::string_view& fieldName) const override;
232232

233-
void setPointField_float(size_t index, const std::string& fieldName, float value) override;
234-
void setPointField_uint16(size_t index, const std::string& fieldName, uint16_t value) override;
233+
void setPointField_float(size_t index, const std::string_view& fieldName, float value) override;
234+
void setPointField_uint16(size_t index, const std::string_view& fieldName, uint16_t value) override;
235235

236-
void insertPointField_float(const std::string& fieldName, float value) override;
237-
void insertPointField_uint16(const std::string& fieldName, uint16_t value) override;
236+
void insertPointField_float(const std::string_view& fieldName, float value) override;
237+
void insertPointField_uint16(const std::string_view& fieldName, uint16_t value) override;
238238

239-
void reserveField_float(const std::string& fieldName, size_t n) override;
240-
void reserveField_uint16(const std::string& fieldName, size_t n) override;
241-
void resizeField_float(const std::string& fieldName, size_t n) override;
242-
void resizeField_uint16(const std::string& fieldName, size_t n) override;
239+
void reserveField_float(const std::string_view& fieldName, size_t n) override;
240+
void reserveField_uint16(const std::string_view& fieldName, size_t n) override;
241+
void resizeField_float(const std::string_view& fieldName, size_t n) override;
242+
void resizeField_uint16(const std::string_view& fieldName, size_t n) override;
243243

244-
auto getPointsBufferRef_float_field(const std::string& fieldName) const
244+
auto getPointsBufferRef_float_field(const std::string_view& fieldName) const
245245
-> const mrpt::aligned_std_vector<float>* override
246246
{
247247
if (auto* f = CPointsMap::getPointsBufferRef_float_field(fieldName); f)
@@ -252,14 +252,14 @@ class CPointsMapXYZIRT : public CPointsMap
252252
if (fieldName == POINT_FIELD_TIMESTAMP) return &m_time;
253253
return nullptr;
254254
}
255-
auto getPointsBufferRef_uint_field(const std::string& fieldName) const
255+
auto getPointsBufferRef_uint_field(const std::string_view& fieldName) const
256256
-> const mrpt::aligned_std_vector<uint16_t>* override
257257
{
258258
if (fieldName == POINT_FIELD_RING_ID) return &m_ring;
259259
return nullptr;
260260
}
261261

262-
auto getPointsBufferRef_float_field(const std::string& fieldName)
262+
auto getPointsBufferRef_float_field(const std::string_view& fieldName)
263263
-> mrpt::aligned_std_vector<float>* override
264264
{
265265
if (auto* f = CPointsMap::getPointsBufferRef_float_field(fieldName); f)
@@ -270,7 +270,7 @@ class CPointsMapXYZIRT : public CPointsMap
270270
if (fieldName == POINT_FIELD_TIMESTAMP) return &m_time;
271271
return nullptr;
272272
}
273-
auto getPointsBufferRef_uint_field(const std::string& fieldName)
273+
auto getPointsBufferRef_uint_field(const std::string_view& fieldName)
274274
-> mrpt::aligned_std_vector<uint16_t>* override
275275
{
276276
if (fieldName == POINT_FIELD_RING_ID) return &m_ring;

0 commit comments

Comments
 (0)