From 38497859a8852142d1581fee292045f89033e689 Mon Sep 17 00:00:00 2001 From: ds283 Date: Sun, 23 Jul 2017 21:39:08 +0100 Subject: [PATCH] Refactor linecache traits to avoid unnecessary boilerplate code when using non-standard data types - linecache requires specializations for sizeof_container_element<>() and elementsof_container<>() for each container type in use - previously this was done by writing out template specializations longhand, but this does not seem to be required. For any container implementing the STL container interface, a size() method will be available and we can provide a template to use it. Also, most containers will provide a value_type type, so we can provide a template to extract sizeof() from this --- .../data/datapipe/linecache_specializations.h | 50 +++---------------- .../transport-runtime/utilities/linecache.h | 8 +-- 2 files changed, 13 insertions(+), 45 deletions(-) diff --git a/CppTransport/transport-runtime/data/datapipe/linecache_specializations.h b/CppTransport/transport-runtime/data/datapipe/linecache_specializations.h index 376f1d607..2626c25de 100644 --- a/CppTransport/transport-runtime/data/datapipe/linecache_specializations.h +++ b/CppTransport/transport-runtime/data/datapipe/linecache_specializations.h @@ -35,48 +35,14 @@ namespace transport // Provide specializations for the size methods used in linecache to compute the size of data elements namespace linecache { - - template<> - unsigned int sizeof_container_element< std::vector >() { return(sizeof(twopf_kconfig)); } - - template<> - unsigned int elementsof_container(const std::vector& container) { return(container.size()); } - - template<> - unsigned int sizeof_container_element< std::vector >() { return(sizeof(threepf_kconfig)); } - - template<> - unsigned int elementsof_container(const std::vector& container) { return(container.size()); } - - template<> - unsigned int sizeof_container_element< std::vector >() { return(sizeof(time_config)); } - - template<> - unsigned int elementsof_container(const std::vector& container) { return(container.size()); } - - template<> - unsigned int sizeof_container_element< std::vector >() { return(sizeof(kconfiguration_statistics)); } - - template<> - unsigned int elementsof_container(const std::vector& container) { return(container.size()); } - - template<> - unsigned int sizeof_container_element< std::vector >() { return(sizeof(double)); } - - template<> - unsigned int elementsof_container(const std::vector& container) { return(container.size()); } - - template<> - unsigned int sizeof_container_element< std::vector >() { return(sizeof(float)); } - - template<> - unsigned int elementsof_container(const std::vector& container) { return(container.size()); } - - template<> - unsigned int sizeof_container_element< std::vector >() { return(sizeof(long double)); } - - template<> - unsigned int elementsof_container(const std::vector& container) { return(container.size()); } + + // template for any container implementing the STL container interface + template + unsigned int elementsof_container(const Container& c) { return c.size(); } + + // template for any container providing a value_type type + template + unsigned int sizeof_container_element() { return sizeof(typename Container::value_type); } } // namespace linecache -- specializations diff --git a/CppTransport/transport-runtime/utilities/linecache.h b/CppTransport/transport-runtime/utilities/linecache.h index 912b4a8b6..551d573b5 100644 --- a/CppTransport/transport-runtime/utilities/linecache.h +++ b/CppTransport/transport-runtime/utilities/linecache.h @@ -50,10 +50,12 @@ namespace transport namespace linecache { - // template functions, which must be specialized later, used to obtain the size of a - // container in bytes, and the number of elements in the container + // template function, which must be specialized later, used to obtain the size of an element + // held by a container (measured in bytes) template unsigned int sizeof_container_element(); - template unsigned int elementsof_container(const Container& c); + + // template function, which must be specialized later, used to obtain the number of elements in a container + template unsigned int elementsof_container(const Container& c); // forward declare constituent classes template