From 8d981b1ff9f7074ae4505915dfcf63f0751fc322 Mon Sep 17 00:00:00 2001
From: Oliver Sander <sander@dune-project.org>
Date: Thu, 11 Aug 2011 15:03:02 +0000
Subject: [PATCH] Add a mechanism for the transition from FieldVector::size as
 an enum member to FieldVector::size() as a method.

With this patch applied FieldVector::size remains an enum, hence all your
code compiles as before.  However you get a compiler warning explaining
the issue and suggesting to configure with --enable-fieldvector-size-is-method.
If you do so, then FieldVector::size becomes a method, and you may have
to go out and adjust your code.  The configure flag is transitional and
will be removed after dune 2.2 is out.

Remember that there is no smoother way to do the transition, as you can't
have FieldVector::size to be both an enum value and a method at the same
time.

This fixes FlySpray issue 819.


[[Imported from SVN: r6486]]
---
 dune/common/fvector.hh                | 19 +++++++++++++++++--
 dune/common/test/fvectortest.cc       |  6 ++++++
 m4/dune_common.m4                     |  5 +++++
 m4/dune_fieldvector_size_is_method.m4 | 17 +++++++++++++++++
 4 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 m4/dune_fieldvector_size_is_method.m4

diff --git a/dune/common/fvector.hh b/dune/common/fvector.hh
index 91cd24697..c61ea9cff 100644
--- a/dune/common/fvector.hh
+++ b/dune/common/fvector.hh
@@ -15,6 +15,15 @@
 #include "densevector.hh"
 #include "static_assert.hh"
 
+#if ! DUNE_COMMON_FIELDVECTOR_SIZE_IS_METHOD
+#warning The FieldVector class exports is size by the enum member 'size'.\
+  This behavior is deprecated.  In the future, 'size' will be a method,\
+  which puts it in compliance with the stl conventions.  To enable the new behavior\
+  (and get rid of this warning), build your Dune with --enable-fieldvector-size-is-method.\
+  If you do need the vector size as an enum, use 'dimension'.
+#endif
+
+
 namespace Dune {
 
   /** @addtogroup DenseMatVec
@@ -58,9 +67,12 @@ namespace Dune {
     //! export size
     enum {
       //! The size of this vector.
-      dimension = SIZE,
+      dimension = SIZE
+#if ! DUNE_COMMON_FIELDVECTOR_SIZE_IS_METHOD
+      ,
       //! The size of this vector.
       size = SIZE
+#endif
     };
 
     typedef typename Base::size_type size_type;
@@ -154,9 +166,12 @@ namespace Dune {
     //! export size
     enum {
       //! The size of this vector.
-      dimension = 1,
+      dimension = 1
+#if ! DUNE_COMMON_FIELDVECTOR_SIZE_IS_METHOD
+      ,
       //! The size of this vector.
       size = 1
+#endif
     };
 
     typedef typename Base::size_type size_type;
diff --git a/dune/common/test/fvectortest.cc b/dune/common/test/fvectortest.cc
index a86e294f3..8cce96e84 100644
--- a/dune/common/test/fvectortest.cc
+++ b/dune/common/test/fvectortest.cc
@@ -90,6 +90,12 @@ struct FieldVectorMainTest
     }
     s >> w;
     assert(v == w);
+
+    // test container methods
+    typename FieldVector<ft,d>::size_type size = FieldVector<ft,d>::dimension;
+#if DUNE_COMMON_FIELDVECTOR_SIZE_IS_METHOD
+    size = w.size();
+#endif
   }
 };
 
diff --git a/m4/dune_common.m4 b/m4/dune_common.m4
index c7ed80237..6ac48dcdd 100644
--- a/m4/dune_common.m4
+++ b/m4/dune_common.m4
@@ -31,6 +31,11 @@ AC_DEFUN([DUNE_COMMON_CHECKS],
   AC_REQUIRE([DUNE_EXPRTMPL])
   AC_REQUIRE([DUNE_TR1_HEADERS])
 
+  dnl Create the flag --enable-fieldvector-size-is-method
+  dnl This is to orchestrate the transition from the constant member FieldVector::size
+  dnl to the method FieldVector::size()
+  AC_REQUIRE([DUNE_FIELDVECTOR_SIZE_IS_METHOD])
+
   dnl check for programs
   AC_REQUIRE([AC_PROG_CC])
   # add -Wall if the compiler is gcc
diff --git a/m4/dune_fieldvector_size_is_method.m4 b/m4/dune_fieldvector_size_is_method.m4
new file mode 100644
index 000000000..695085947
--- /dev/null
+++ b/m4/dune_fieldvector_size_is_method.m4
@@ -0,0 +1,17 @@
+dnl This macro introduces a configure flag --enable-fieldvector-size-is method
+dnl that is used to orchestrate the transition from FieldVector::size as an enum
+dnl and FieldVector::size() as a method (see FS 819 for details).
+dnl If this flag is not set (the default), then FieldVector::size is an enum,
+dnl but a compiler warning appears.  If it is set then the warning disappears,
+dnl but FieldVector::size() becomes a method.
+dnl
+dnl This flag and the corresponding preprocessor directives should be removed
+dnl not earlier than after dune-2.2 has been released.
+
+AC_DEFUN([DUNE_FIELDVECTOR_SIZE_IS_METHOD],[
+  AC_ARG_ENABLE(fieldvector-size-is-method,
+    AS_HELP_STRING([--enable-fieldvector-size-is-method],[If this is set, the member 'size' of FieldVector is a method rather than an enum]))
+
+  AS_IF([test "x$enable_fieldvector_size_is_method" = "xyes"],
+    AC_DEFINE(DUNE_COMMON_FIELDVECTOR_SIZE_IS_METHOD, 1, [If this is set, the member 'size' of FieldVector is a method rather than an enum]))
+])
-- 
GitLab