Remove wpi::ArrayRef std::initializer_list constructor (#1745)

This can be dangerous as it refers to a temporary, and GCC 9.0 warns about
its use.  Instead add std::initializer_list overloads to common places it
was used in an initializer_list sense.
This commit is contained in:
Peter Johnson
2019-06-29 23:54:02 -07:00
committed by GitHub
parent 9e19b29c31
commit 60dce66a4f
17 changed files with 482 additions and 59 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -10,6 +10,7 @@
#include <stdint.h>
#include <initializer_list>
#include <memory>
#include <string>
#include <vector>
@@ -176,6 +177,23 @@ class NetworkTableEntry final {
*/
std::vector<int> GetBooleanArray(ArrayRef<int> defaultValue) const;
/**
* Gets the entry's value as a boolean array. If the entry does not exist
* or is of different type, it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*
* @note This makes a copy of the array. If the overhead of this is a
* concern, use GetValue() instead.
*
* @note The returned array is std::vector<int> instead of std::vector<bool>
* because std::vector<bool> is special-cased in C++. 0 is false, any
* non-zero value is true.
*/
std::vector<int> GetBooleanArray(
std::initializer_list<int> defaultValue) const;
/**
* Gets the entry's value as a double array. If the entry does not exist
* or is of different type, it will return the default value.
@@ -188,6 +206,19 @@ class NetworkTableEntry final {
*/
std::vector<double> GetDoubleArray(ArrayRef<double> defaultValue) const;
/**
* Gets the entry's value as a double array. If the entry does not exist
* or is of different type, it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*
* @note This makes a copy of the array. If the overhead of this is a
* concern, use GetValue() instead.
*/
std::vector<double> GetDoubleArray(
std::initializer_list<double> defaultValue) const;
/**
* Gets the entry's value as a string array. If the entry does not exist
* or is of different type, it will return the default value.
@@ -201,6 +232,19 @@ class NetworkTableEntry final {
std::vector<std::string> GetStringArray(
ArrayRef<std::string> defaultValue) const;
/**
* Gets the entry's value as a string array. If the entry does not exist
* or is of different type, it will return the default value.
*
* @param defaultValue the value to be returned if no value is found
* @return the entry's value or the given default value
*
* @note This makes a copy of the array. If the overhead of this is a
* concern, use GetValue() instead.
*/
std::vector<std::string> GetStringArray(
std::initializer_list<std::string> defaultValue) const;
/**
* Sets the entry's value if it does not exist.
*
@@ -249,6 +293,14 @@ class NetworkTableEntry final {
*/
bool SetDefaultBooleanArray(ArrayRef<int> defaultValue);
/**
* Sets the entry's value if it does not exist.
*
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultBooleanArray(std::initializer_list<int> defaultValue);
/**
* Sets the entry's value if it does not exist.
*
@@ -257,6 +309,14 @@ class NetworkTableEntry final {
*/
bool SetDefaultDoubleArray(ArrayRef<double> defaultValue);
/**
* Sets the entry's value if it does not exist.
*
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultDoubleArray(std::initializer_list<double> defaultValue);
/**
* Sets the entry's value if it does not exist.
*
@@ -265,6 +325,14 @@ class NetworkTableEntry final {
*/
bool SetDefaultStringArray(ArrayRef<std::string> defaultValue);
/**
* Sets the entry's value if it does not exist.
*
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
bool SetDefaultStringArray(std::initializer_list<std::string> defaultValue);
/**
* Sets the entry's value.
*
@@ -305,6 +373,22 @@ class NetworkTableEntry final {
*/
bool SetRaw(StringRef value);
/**
* Sets the entry's value.
*
* @param value the value to set
* @return False if the entry exists with a different type
*/
bool SetBooleanArray(ArrayRef<bool> value);
/**
* Sets the entry's value.
*
* @param value the value to set
* @return False if the entry exists with a different type
*/
bool SetBooleanArray(std::initializer_list<bool> value);
/**
* Sets the entry's value.
*
@@ -313,6 +397,14 @@ class NetworkTableEntry final {
*/
bool SetBooleanArray(ArrayRef<int> value);
/**
* Sets the entry's value.
*
* @param value the value to set
* @return False if the entry exists with a different type
*/
bool SetBooleanArray(std::initializer_list<int> value);
/**
* Sets the entry's value.
*
@@ -321,6 +413,14 @@ class NetworkTableEntry final {
*/
bool SetDoubleArray(ArrayRef<double> value);
/**
* Sets the entry's value.
*
* @param value the value to set
* @return False if the entry exists with a different type
*/
bool SetDoubleArray(std::initializer_list<double> value);
/**
* Sets the entry's value.
*
@@ -329,6 +429,14 @@ class NetworkTableEntry final {
*/
bool SetStringArray(ArrayRef<std::string> value);
/**
* Sets the entry's value.
*
* @param value the value to set
* @return False if the entry exists with a different type
*/
bool SetStringArray(std::initializer_list<std::string> value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
@@ -369,6 +477,22 @@ class NetworkTableEntry final {
*/
void ForceSetRaw(StringRef value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
*
* @param value the value to set
*/
void ForceSetBooleanArray(ArrayRef<bool> value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
*
* @param value the value to set
*/
void ForceSetBooleanArray(std::initializer_list<bool> value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
@@ -377,6 +501,14 @@ class NetworkTableEntry final {
*/
void ForceSetBooleanArray(ArrayRef<int> value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
*
* @param value the value to set
*/
void ForceSetBooleanArray(std::initializer_list<int> value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
@@ -385,6 +517,14 @@ class NetworkTableEntry final {
*/
void ForceSetDoubleArray(ArrayRef<double> value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
*
* @param value the value to set
*/
void ForceSetDoubleArray(std::initializer_list<double> value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
@@ -393,6 +533,14 @@ class NetworkTableEntry final {
*/
void ForceSetStringArray(ArrayRef<std::string> value);
/**
* Sets the entry's value. If the value is of different type, the type is
* changed to match the new value.
*
* @param value the value to set
*/
void ForceSetStringArray(std::initializer_list<std::string> value);
/**
* Sets flags.
*

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. All Rights Reserved. */
/* Copyright (c) FIRST 2017-2019. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -76,6 +76,12 @@ inline std::vector<int> NetworkTableEntry::GetBooleanArray(
return value->GetBooleanArray();
}
inline std::vector<int> NetworkTableEntry::GetBooleanArray(
std::initializer_list<int> defaultValue) const {
return GetBooleanArray(
wpi::makeArrayRef(defaultValue.begin(), defaultValue.end()));
}
inline std::vector<double> NetworkTableEntry::GetDoubleArray(
ArrayRef<double> defaultValue) const {
auto value = GetEntryValue(m_handle);
@@ -83,6 +89,12 @@ inline std::vector<double> NetworkTableEntry::GetDoubleArray(
return value->GetDoubleArray();
}
inline std::vector<double> NetworkTableEntry::GetDoubleArray(
std::initializer_list<double> defaultValue) const {
return GetDoubleArray(
wpi::makeArrayRef(defaultValue.begin(), defaultValue.end()));
}
inline std::vector<std::string> NetworkTableEntry::GetStringArray(
ArrayRef<std::string> defaultValue) const {
auto value = GetEntryValue(m_handle);
@@ -90,6 +102,12 @@ inline std::vector<std::string> NetworkTableEntry::GetStringArray(
return value->GetStringArray();
}
inline std::vector<std::string> NetworkTableEntry::GetStringArray(
std::initializer_list<std::string> defaultValue) const {
return GetStringArray(
wpi::makeArrayRef(defaultValue.begin(), defaultValue.end()));
}
inline bool NetworkTableEntry::SetDefaultValue(std::shared_ptr<Value> value) {
return SetDefaultEntryValue(m_handle, value);
}
@@ -145,18 +163,42 @@ inline bool NetworkTableEntry::SetRaw(StringRef value) {
return SetEntryValue(m_handle, Value::MakeRaw(value));
}
inline bool NetworkTableEntry::SetBooleanArray(ArrayRef<bool> value) {
return SetEntryValue(m_handle, Value::MakeBooleanArray(value));
}
inline bool NetworkTableEntry::SetBooleanArray(
std::initializer_list<bool> value) {
return SetEntryValue(m_handle, Value::MakeBooleanArray(value));
}
inline bool NetworkTableEntry::SetBooleanArray(ArrayRef<int> value) {
return SetEntryValue(m_handle, Value::MakeBooleanArray(value));
}
inline bool NetworkTableEntry::SetBooleanArray(
std::initializer_list<int> value) {
return SetEntryValue(m_handle, Value::MakeBooleanArray(value));
}
inline bool NetworkTableEntry::SetDoubleArray(ArrayRef<double> value) {
return SetEntryValue(m_handle, Value::MakeDoubleArray(value));
}
inline bool NetworkTableEntry::SetDoubleArray(
std::initializer_list<double> value) {
return SetEntryValue(m_handle, Value::MakeDoubleArray(value));
}
inline bool NetworkTableEntry::SetStringArray(ArrayRef<std::string> value) {
return SetEntryValue(m_handle, Value::MakeStringArray(value));
}
inline bool NetworkTableEntry::SetStringArray(
std::initializer_list<std::string> value) {
return SetEntryValue(m_handle, Value::MakeStringArray(value));
}
inline void NetworkTableEntry::ForceSetValue(std::shared_ptr<Value> value) {
SetEntryTypeValue(m_handle, value);
}
@@ -177,19 +219,43 @@ inline void NetworkTableEntry::ForceSetRaw(StringRef value) {
SetEntryTypeValue(m_handle, Value::MakeRaw(value));
}
inline void NetworkTableEntry::ForceSetBooleanArray(ArrayRef<bool> value) {
SetEntryTypeValue(m_handle, Value::MakeBooleanArray(value));
}
inline void NetworkTableEntry::ForceSetBooleanArray(
std::initializer_list<bool> value) {
SetEntryTypeValue(m_handle, Value::MakeBooleanArray(value));
}
inline void NetworkTableEntry::ForceSetBooleanArray(ArrayRef<int> value) {
SetEntryTypeValue(m_handle, Value::MakeBooleanArray(value));
}
inline void NetworkTableEntry::ForceSetBooleanArray(
std::initializer_list<int> value) {
SetEntryTypeValue(m_handle, Value::MakeBooleanArray(value));
}
inline void NetworkTableEntry::ForceSetDoubleArray(ArrayRef<double> value) {
SetEntryTypeValue(m_handle, Value::MakeDoubleArray(value));
}
inline void NetworkTableEntry::ForceSetDoubleArray(
std::initializer_list<double> value) {
SetEntryTypeValue(m_handle, Value::MakeDoubleArray(value));
}
inline void NetworkTableEntry::ForceSetStringArray(
ArrayRef<std::string> value) {
SetEntryTypeValue(m_handle, Value::MakeStringArray(value));
}
inline void NetworkTableEntry::ForceSetStringArray(
std::initializer_list<std::string> value) {
SetEntryTypeValue(m_handle, Value::MakeStringArray(value));
}
inline void NetworkTableEntry::SetFlags(unsigned int flags) {
SetEntryFlags(m_handle, GetFlags() | flags);
}

View File

@@ -11,6 +11,7 @@
#include <stdint.h>
#include <cassert>
#include <initializer_list>
#include <memory>
#include <string>
#include <type_traits>
@@ -374,6 +375,20 @@ class Value final {
static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<bool> value,
uint64_t time = 0);
/**
* Creates a boolean array entry value.
*
* @param value the value
* @param time if nonzero, the creation time to use (instead of the current
* time)
* @return The entry value
*/
static std::shared_ptr<Value> MakeBooleanArray(
std::initializer_list<bool> value, uint64_t time = 0) {
return MakeBooleanArray(wpi::makeArrayRef(value.begin(), value.end()),
time);
}
/**
* Creates a boolean array entry value.
*
@@ -385,6 +400,20 @@ class Value final {
static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<int> value,
uint64_t time = 0);
/**
* Creates a boolean array entry value.
*
* @param value the value
* @param time if nonzero, the creation time to use (instead of the current
* time)
* @return The entry value
*/
static std::shared_ptr<Value> MakeBooleanArray(
std::initializer_list<int> value, uint64_t time = 0) {
return MakeBooleanArray(wpi::makeArrayRef(value.begin(), value.end()),
time);
}
/**
* Creates a double array entry value.
*
@@ -396,6 +425,19 @@ class Value final {
static std::shared_ptr<Value> MakeDoubleArray(ArrayRef<double> value,
uint64_t time = 0);
/**
* Creates a double array entry value.
*
* @param value the value
* @param time if nonzero, the creation time to use (instead of the current
* time)
* @return The entry value
*/
static std::shared_ptr<Value> MakeDoubleArray(
std::initializer_list<double> value, uint64_t time = 0) {
return MakeDoubleArray(wpi::makeArrayRef(value.begin(), value.end()), time);
}
/**
* Creates a string array entry value.
*
@@ -407,6 +449,19 @@ class Value final {
static std::shared_ptr<Value> MakeStringArray(ArrayRef<std::string> value,
uint64_t time = 0);
/**
* Creates a string array entry value.
*
* @param value the value
* @param time if nonzero, the creation time to use (instead of the current
* time)
* @return The entry value
*/
static std::shared_ptr<Value> MakeStringArray(
std::initializer_list<std::string> value, uint64_t time = 0) {
return MakeStringArray(wpi::makeArrayRef(value.begin(), value.end()), time);
}
/**
* Creates a string array entry value.
*