Review fixups

This commit is contained in:
Tim Wojtulewicz 2020-05-07 17:25:26 -07:00
parent 499a3353b5
commit 690061b01c
3 changed files with 9 additions and 23 deletions

View file

@ -23,4 +23,4 @@ class Attr;
typedef PList<Attr> attr_list;
class Timer;
typedef PList<Timer, LIST_UNORDERED> timer_list;
typedef PList<Timer, ListOrder::UNORDERED> timer_list;

View file

@ -117,7 +117,7 @@ TEST_CASE("plists")
TEST_CASE("unordered list operation")
{
List<int, LIST_UNORDERED> list({1, 2, 3, 4});
List<int, ListOrder::UNORDERED> list({1, 2, 3, 4});
CHECK(list.size() == 4);
// An unordered list doesn't maintain the ordering of the elements when

View file

@ -29,9 +29,9 @@
// TODO: this can be removed in v3.1 when List::sort() is removed
typedef int (*list_cmp_func)(const void* v1, const void* v2);
enum list_order { LIST_ORDERED, LIST_UNORDERED };
enum class ListOrder : int { ORDERED, UNORDERED };
template<typename T, list_order Order = LIST_ORDERED>
template<typename T, ListOrder Order = ListOrder::ORDERED>
class List {
public:
@ -216,7 +216,7 @@ public:
// For data where we don't care about ordering, we don't care about keeping
// the list in the same order when removing an element. Just swap the last
// element with the element being removed.
if ( Order == LIST_ORDERED )
if constexpr ( Order == ListOrder::ORDERED )
{
--num_entries;
@ -252,16 +252,16 @@ public:
T replace(int ent_index, const T& new_ent) // replace entry #i with a new value
{
if ( ent_index < 0 )
return get_default_value();
return T{};
T old_ent = get_default_value();
T old_ent{};
if ( ent_index > num_entries - 1 )
{ // replacement beyond the end of the list
resize(ent_index + 1);
for ( int i = num_entries; i < max_entries; ++i )
entries[i] = get_default_value();
entries[i] = T{};
num_entries = max_entries;
}
else
@ -299,20 +299,6 @@ public:
protected:
T get_default_value()
{
if constexpr ( std::is_pointer_v<T> )
return nullptr;
else if constexpr ( std::is_integral_v<T> )
return 0;
else if constexpr ( std::is_floating_point_v<T> )
return 0.0;
else
// TODO: Should this return an error at compile time that we don't
// know what this type is?
return T();
}
// This could essentially be an std::vector if we wanted. Some
// reasons to maybe not refactor to use std::vector ?
//
@ -344,7 +330,7 @@ protected:
// Specialization of the List class to store pointers of a type.
template<typename T, list_order Order = LIST_ORDERED>
template<typename T, ListOrder Order = ListOrder::ORDERED>
using PList = List<T*, Order>;
// Popular type of list: list of strings.