Skip to content

Commit

Permalink
Refactor linecache traits to avoid unnecessary boilerplate code when …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
ds283 committed Jul 23, 2017
1 parent b24e536 commit 3849785
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<twopf_kconfig> >() { return(sizeof(twopf_kconfig)); }

template<>
unsigned int elementsof_container(const std::vector<twopf_kconfig>& container) { return(container.size()); }

template<>
unsigned int sizeof_container_element< std::vector<threepf_kconfig> >() { return(sizeof(threepf_kconfig)); }

template<>
unsigned int elementsof_container(const std::vector<threepf_kconfig>& container) { return(container.size()); }

template<>
unsigned int sizeof_container_element< std::vector<time_config> >() { return(sizeof(time_config)); }

template<>
unsigned int elementsof_container(const std::vector<time_config>& container) { return(container.size()); }

template<>
unsigned int sizeof_container_element< std::vector<kconfiguration_statistics> >() { return(sizeof(kconfiguration_statistics)); }

template<>
unsigned int elementsof_container(const std::vector<kconfiguration_statistics>& container) { return(container.size()); }

template<>
unsigned int sizeof_container_element< std::vector<double> >() { return(sizeof(double)); }

template<>
unsigned int elementsof_container(const std::vector<double>& container) { return(container.size()); }

template<>
unsigned int sizeof_container_element< std::vector<float> >() { return(sizeof(float)); }

template<>
unsigned int elementsof_container(const std::vector<float>& container) { return(container.size()); }

template<>
unsigned int sizeof_container_element< std::vector<long double> >() { return(sizeof(long double)); }

template<>
unsigned int elementsof_container(const std::vector<long double>& container) { return(container.size()); }

// template for any container implementing the STL container interface
template <typename Container>
unsigned int elementsof_container(const Container& c) { return c.size(); }

// template for any container providing a value_type type
template <typename Container>
unsigned int sizeof_container_element() { return sizeof(typename Container::value_type); }

} // namespace linecache -- specializations

Expand Down
8 changes: 5 additions & 3 deletions CppTransport/transport-runtime/utilities/linecache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Container> unsigned int sizeof_container_element();
template <typename Container> 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 <typename Container> unsigned int elementsof_container(const Container& c);

// forward declare constituent classes
template <typename DataContainer, typename DataTag, typename QueryObject, unsigned int HashSize>
Expand Down

0 comments on commit 3849785

Please sign in to comment.