From 4c0502a53f5ca96d40d9c2b6c36ebeaf1b6057bf Mon Sep 17 00:00:00 2001 From: David Wells Date: Fri, 2 Oct 2015 14:27:59 -0400 Subject: [PATCH] Make memory_consumption(int) et al more generic. I used SFINAE to write one implementation for memory_consumption() for fundamental types, instead of the dozen or so present previously. --- include/deal.II/base/memory_consumption.h | 206 ++++--------------- include/deal.II/base/std_cxx11/type_traits.h | 6 +- 2 files changed, 40 insertions(+), 172 deletions(-) diff --git a/include/deal.II/base/memory_consumption.h b/include/deal.II/base/memory_consumption.h index 68e6d091c0..d9bf795b7a 100644 --- a/include/deal.II/base/memory_consumption.h +++ b/include/deal.II/base/memory_consumption.h @@ -19,8 +19,13 @@ #include #include +#include #include +// unfortunately, boost::enable_if_c, not boost::enable_if, is equivalent to +// std::enable_if +#include + #include #include #include @@ -45,11 +50,11 @@ template class VectorizedArray; * are several modes of operation: * *
    - *
  1. The argument is a standard C++ data type, namely, bool, - * float, double or any of the integer types. In that case, - * memory_consumption() simple returns sizeof of its argument. The + *
  2. If the argument is a fundamental C++ data type (such as bool, + * float, double or any of the integer types), then + * memory_consumption() just returns sizeof of its argument. The * library also provides an estimate for the amount of memory occupied by a - * std::string this way. + * std::string. * *
  3. For objects, which are neither standard types, nor vectors, * memory_consumption() will simply call the member function of same name. It @@ -72,105 +77,42 @@ template class VectorizedArray; *

    Extending this namespace

    * * The function in this namespace and the functionality provided by it relies - * on the assumption that there is either a specialized function + * on the assumption that there is either a function * memory_consumption(T) in this namespace determining the amount of - * memory used by objects of type T, or that the class T has - * a member function of that name. While the latter is true for almost all + * memory used by objects of type T or that the class T has + * a member function of that name. While the latter is true for almost all * classes in deal.II, we have only implemented the first kind of functions - * for the most common data types, such as atomic types, strings, C++ vectors, - * C-style arrays, and C++ pairs. These functions therefore do not cover, for - * example, C++ maps, lists, etc. If you need such functions feel free to - * implement them and send them to us for inclusion. + * for the most common data types, such as fundamental types, strings, C++ + * vectors, C-style arrays, and C++ pairs. These functions therefore do not + * cover, for example, C++ maps, lists, etc. If you need such functions feel + * free to implement them and send them to us for inclusion. * * @ingroup memory - * @author Wolfgang Bangerth, documentation updated by Guido Kanschat - * @date 2000 + * @author Wolfgang Bangerth, documentation updated by Guido Kanschat, David Wells + * @date 2000, 2015 */ namespace MemoryConsumption { - /** - * This function is the generic interface for determining the memory used by - * an object. If no specialization for the type T is specified, it - * will call the member function t.memory_consumption(). - * - * The library provides specializations for all basic C++ data types. Every - * additional type needs to have a member function memory_consumption() - * callable for constant objects to be used in this framework. + * Calculate the memory consumption of a fundamental type. See + * EnableIfScalar for a discussion on how this restriction (SFINAE) is + * implemented. */ template inline - std::size_t memory_consumption (const T &t); - - /** - * Determine the amount of memory in bytes consumed by a bool - * variable. - */ - inline - std::size_t memory_consumption (const bool); - - /** - * Determine the amount of memory in bytes consumed by a char - * variable. - */ - inline - std::size_t memory_consumption (const char); - - /** - * Determine the amount of memory in bytes consumed by a short int - * variable. - */ - inline - std::size_t memory_consumption (const short int); + typename boost::enable_if_c::value, std::size_t>::type + memory_consumption (const T &t); /** - * Determine the amount of memory in bytes consumed by a short unsigned - * int variable. - */ - inline - std::size_t memory_consumption (const short unsigned int); - - /** - * Determine the amount of memory in bytes consumed by a int - * variable. - */ - inline - std::size_t memory_consumption (const int); - - /** - * Determine the amount of memory in bytes consumed by a unsigned - * int variable. - */ - inline - std::size_t memory_consumption (const unsigned int); - - /** - * Determine the amount of memory in bytes consumed by a unsigned long - * long int variable. - */ - inline - std::size_t memory_consumption (const unsigned long long int); - - /** - * Determine the amount of memory in bytes consumed by a float - * variable. - */ - inline - std::size_t memory_consumption (const float); - - /** - * Determine the amount of memory in bytes consumed by a double - * variable. - */ - inline - std::size_t memory_consumption (const double); - - /** - * Determine the amount of memory in bytes consumed by a long - * double variable. + * Estimate the memory consumption of an object. If no further template + * specialization (past this one) is available for the type T, then + * this function returns the member function + * t.memory_consumption()'s value. */ + template inline - std::size_t memory_consumption (const long double); + typename boost::enable_if_c::value || std_cxx11::is_pointer::value), std::size_t>::type + memory_consumption (const T &t); /** * Determine the amount of memory consumed by a C-style string. The returned @@ -364,90 +306,12 @@ namespace MemoryConsumption namespace MemoryConsumption { + template inline - std::size_t memory_consumption (const bool) - { - return sizeof(bool); - } - - - - inline - std::size_t memory_consumption (const char) - { - return sizeof(char); - } - - - - inline - std::size_t memory_consumption (const short int) - { - return sizeof(short int); - } - - - - inline - std::size_t memory_consumption (const short unsigned int) - { - return sizeof(short unsigned int); - } - - - - inline - std::size_t memory_consumption (const int) - { - return sizeof(int); - } - - - - inline - std::size_t memory_consumption (const unsigned int) - { - return sizeof(unsigned int); - } - - - - inline - std::size_t memory_consumption (const unsigned long int) - { - return sizeof(unsigned long int); - } - - - - inline - std::size_t memory_consumption (const unsigned long long int) - { - return sizeof(unsigned long long int); - } - - - - inline - std::size_t memory_consumption (const float) - { - return sizeof(float); - } - - - - inline - std::size_t memory_consumption (const double) - { - return sizeof(double); - } - - - - inline - std::size_t memory_consumption (const long double) + typename boost::enable_if_c::value, std::size_t>::type + memory_consumption(const T &) { - return sizeof(long double); + return sizeof(T); } @@ -641,7 +505,7 @@ namespace MemoryConsumption template inline - std::size_t + typename boost::enable_if_c::value || std_cxx11::is_pointer::value), std::size_t>::type memory_consumption (const T &t) { return t.memory_consumption(); diff --git a/include/deal.II/base/std_cxx11/type_traits.h b/include/deal.II/base/std_cxx11/type_traits.h index ccb215d3f3..114485d1e9 100644 --- a/include/deal.II/base/std_cxx11/type_traits.h +++ b/include/deal.II/base/std_cxx11/type_traits.h @@ -1,6 +1,6 @@ // --------------------------------------------------------------------- // -// Copyright (C) 2012 - 2014 by the deal.II authors +// Copyright (C) 2012 - 2015 by the deal.II authors // // This file is part of the deal.II library. // @@ -27,7 +27,9 @@ namespace std_cxx11 { // TODO: could fill up with more types from // C++11 type traits + using std::is_fundamental; using std::is_pod; + using std::is_pointer; using std::is_standard_layout; using std::is_trivial; } @@ -39,7 +41,9 @@ DEAL_II_NAMESPACE_CLOSE DEAL_II_NAMESPACE_OPEN namespace std_cxx11 { + using boost::is_fundamental; using boost::is_pod; + using boost::is_pointer; // boost does not have is_standard_layout and // is_trivial, but those are both a subset of -- 2.39.5