SPH
Class library

The code contains a number of useful classes and functions that improve the type safety, memory management and overall design of the code. They are ofter inspired by classes of standard C++ library, either those already introduced in C++11 and C++14 (std::tuple, std::unique_ptr, ...) or classes planned for C++17 or future standards (std::array_view, std::optional).

The reasons to use custom classes instead of std:: ones are:

  • we can modify, extend and improve the functionality as needed, without any restrictions
  • safer behavior of classes; for example Sph::Array is noncopyable to avoid accidental copies of large arrays, while std::vector defines a copy constructor / operator
  • we don't have to relly on capabilities of standard library on any given compiler, or wait for future standards
  • improved error reporting and other debug checks using custom assert
  • it is fun to implement them :)

List of containers, class templates, ...

The following incomplete list briefly describes classes used in the code. They are located in folder objects/wrappers and objects/containers. While the behavior of the classes is set up as needed by SPH code, they are generic and can be used in SPH-unrelated code.

  • Sph::Array

    Generic dynamic array, storing individual elements in continuous memory. Can be resized or shrinked as needed. Similar to std::vector, but it is noncopyable (only move constructor/operator is defined). It also allows to store references. Keep in mind that any pointer or iterator to the element of the array is invalidated when the array is extended. In the code, it is used everywhere we need to stored a large number of elements, for example quantity values of each particle.

  • Sph::StaticArray

    Array with static maximum size, somewhat similar to std::array. It can contain fewer elements or be empty, elements can be pushed into the array or be removed, provided the number of elements does not exceed the maximum size. As Sph::Array, it is noncopyable and can store references. Used to store a small number of elements in cases where the maximum number is known. It is useful if one wants to avoid overhead (and potential badalloc exception) of dynamical allocation.

  • Sph::ArrayView

    Simple wrapper of pointer and number of elements, inspired by a proposal for std::array_view (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0122r0.pdf). It is a non-owning view over a continuous block of memory. Both Sph::Array and Sph::ArrayView are implicitly convertible to Sph::ArrayView. It allows to access the elements of (static) array with bounds checking. Just like pointers or iterators, Sph::ArrayView is also invalidated when Sph::Array is resized. In the code, it is mainly used to view particle quantities from different objects and different threads. As quantities are stored in one shared location, Sph::ArrayView is a useful tool for this.

  • Sph::List

    Straight-forward re-implementation of std::list, i.e. a doubly-linked list. Similarly to other containers in the code, it is non-copyable; creating copies is only possibly by explicitly calling List::clone function. It does not store elements consecutively in memory and thus cannot be converted to ArrayView. Can store references.

  • Sph::Tuple

    A slightly-modified version of std::tuple. It is a heterogeneous container, capable of storing a fixed number of elements with different types. Can store references (like std::tuple), allows to iterate over the elements, etc. Useful as a return type of function in case we want to return more than one value (which is not normally possible in C++).

  • Sph::Variant

    Similar to std::variant TODO

  • Sph::Any

    Similar to std::any TODO

  • Sph::Optional

    Similar to std::optional TODO

  • Sph::Expected
  • Sph::Outcome
  • Sph::Range
  • Sph::AutoPtr

    Similar to std::unique_ptr TODO

  • Sph::SharedPtr

    Similar to std::shared_ptr

  • Sph::ClonePtr

    Another smart pointer with copy operator that creates a new object. Unlike Sph::AutoPtr that does no define a copy operator or Sph::SharedPtr that copies pointer and shares ownership of the resource, each Sph::ClonePtr owns only a single resource and when copied, it clones the resource using its copy operator and returns the new instance. Useful when we want to store copyable objects without sharing ownership between the copies.

  • Sph::LockingPtr

    Extension of Sph::SharedPtr, useful for thread-safe access to the resource. Beside the resource, Sph::LockingPtr also shares a std::mutex and locks it whenever the resource is accessed.