From def502b1c9dba8d5e883e4a22c31a495b631e8a4 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 20 Apr 2015 10:34:06 +0200 Subject: Use C++11 feature to implement lengthof Also disable warning about auto_ptr until it is changed. --- build/top.mk | 2 +- ucoo/common.hh | 11 ++++++++--- ucoo/hal/uart/test/test_uart.cc | 2 +- ucoo/utils/test/test_crc.cc | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/build/top.mk b/build/top.mk index 4de2438..385df77 100644 --- a/build/top.mk +++ b/build/top.mk @@ -18,7 +18,7 @@ CPPFLAGS := $(DEFS) $(INCLUDES) -MP -MMD OPTIMIZE ?= -Os CFLAGS := -g -Wall -W -Wundef -Wno-unused-parameter \ -fno-exceptions $(OPTIMIZE) -CXXFLAGS := $(CFLAGS) -fno-rtti -fno-threadsafe-statics +CXXFLAGS := $(CFLAGS) -fno-rtti -fno-threadsafe-statics -std=gnu++11 -Wno-deprecated-declarations LDFLAGS := LDLIBS := $(LIBS) diff --git a/ucoo/common.hh b/ucoo/common.hh index 282041f..505cf60 100644 --- a/ucoo/common.hh +++ b/ucoo/common.hh @@ -83,9 +83,14 @@ assert_perror (bool condition) halt_perror (); } -} // namespace ucoo +/// Get array length at compile time. +template +constexpr int +lengthof (T (&)[N]) +{ + return N; +} -/// Get array length at compile time, must be a macro until c++11. -#define lengthof(array) (sizeof (array) / sizeof((array)[0])) +} // namespace ucoo #endif // ucoo_common_h diff --git a/ucoo/hal/uart/test/test_uart.cc b/ucoo/hal/uart/test/test_uart.cc index c1b111c..2c326f3 100644 --- a/ucoo/hal/uart/test/test_uart.cc +++ b/ucoo/hal/uart/test/test_uart.cc @@ -56,7 +56,7 @@ main (int argc, const char **argv) char buf[64]; while (1) { - for (int i = 0; i < (int) lengthof (u); i++) + for (int i = 0; i < (int) ucoo::lengthof (u); i++) { if (u[i]->poll ()) { diff --git a/ucoo/utils/test/test_crc.cc b/ucoo/utils/test/test_crc.cc index c20600d..219c38b 100644 --- a/ucoo/utils/test/test_crc.cc +++ b/ucoo/utils/test/test_crc.cc @@ -35,7 +35,7 @@ main (int argc, const char **argv) { ucoo::Test test (tsuite, "crc8 test vector"); static const uint8_t test_vector[] = { 0x02, 0x1c, 0xb8, 0x01, 0, 0, 0, 0xa2 }; - if (ucoo::crc8_compute (test_vector, lengthof (test_vector)) != 0) + if (ucoo::crc8_compute (test_vector, ucoo::lengthof (test_vector)) != 0) test.fail (); } { -- cgit v1.2.3