Skip to content
Snippets Groups Projects
Select Git revision
  • c64d0a0bdd3abb378d000d9f378f306598e478b9
  • develop default protected
  • feature/advection
  • feature/composite-scheme-other-fluxes
  • origin/stage/bouguettaia
  • save_clemence
  • feature/local-dt-fsi
  • feature/variational-hydro
  • feature/gmsh-reader
  • feature/reconstruction
  • feature/kinetic-schemes
  • feature/composite-scheme-sources
  • feature/serraille
  • feature/composite-scheme
  • hyperplastic
  • feature/polynomials
  • feature/gks
  • feature/implicit-solver-o2
  • feature/coupling_module
  • feature/implicit-solver
  • feature/merge-local-dt-fsi
  • v0.5.0 protected
  • v0.4.1 protected
  • v0.4.0 protected
  • v0.3.0 protected
  • v0.2.0 protected
  • v0.1.0 protected
  • Kidder
  • v0.0.4 protected
  • v0.0.3 protected
  • v0.0.2 protected
  • v0 protected
  • v0.0.1 protected
33 results

CastArray.hpp

Blame
  • Stephane Del Pino's avatar
    Stéphane Del Pino authored
    Remove almost all std::exit calls.
    
    Now, one should use one of the provided exceptions.
    
    - NormalError: an error which is related to the use of the code. It is
    not related to a bug
    
    - NotImplementedError: an error related to a functionnality that is
    not avaliable yet
    
    - UnexpectedError: an error related to some unexpected state. This
    is related to a bug
    
    Closes #13
    c64d0a0b
    History
    CastArray.hpp 2.50 KiB
    #ifndef CAST_ARRAY_HPP
    #define CAST_ARRAY_HPP
    
    #include <Array.hpp>
    #include <PugsTraits.hpp>
    
    #include <iostream>
    
    #include <Exceptions.hpp>
    
    template <typename DataType, typename CastDataType>
    class CastArray
    {
     public:
      using data_type = CastDataType;
    
     private:
      const Array<DataType> m_array;
      const size_t m_size;
      CastDataType* const m_values;
    
     public:
      PUGS_INLINE
      const size_t&
      size() const
      {
        return m_size;
      }
    
      PUGS_INLINE
      CastDataType& operator[](const size_t& i) const
      {
        Assert(i < m_size);
        return m_values[i];
      }
    
      PUGS_INLINE
      CastArray& operator=(const CastArray&) = default;
    
      PUGS_INLINE
      CastArray& operator=(CastArray&&) = default;
    
      PUGS_INLINE
      CastArray() : m_size(0), m_values(nullptr)
      {
        ;
      }
    
      PUGS_INLINE
      CastArray(const Array<DataType>& array)
        : m_array(array),
          m_size(sizeof(DataType) * array.size() / sizeof(CastDataType)),
          m_values((array.size() == 0) ? nullptr : reinterpret_cast<CastDataType*>(&(array[0])))
      {
        static_assert((std::is_const_v<CastDataType> and std::is_const_v<DataType>) or (not std::is_const_v<DataType>),
                      "CastArray cannot remove const attribute");
    
        if (sizeof(DataType) * array.size() % sizeof(CastDataType)) {
          throw UnexpectedError("cannot cast array to the chosen data type");
        }
      }
    
      PUGS_INLINE
      CastArray(DataType& value)
        : m_size(sizeof(DataType) / sizeof(CastDataType)), m_values(reinterpret_cast<CastDataType*>(&(value)))
      {
        static_assert((std::is_const_v<CastDataType> and std::is_const_v<DataType>) or (not std::is_const_v<DataType>),
                      "CastArray cannot remove const attribute");
        static_assert(is_trivially_castable<DataType>, "Defining CastArray from non trivially castable type is not "
                                                       "allowed");
      }
    
      PUGS_INLINE
      CastArray(DataType&& value) = delete;
    
      PUGS_INLINE
      CastArray(const CastArray&) = default;
    
      PUGS_INLINE
      CastArray(CastArray&&) = default;
    
      PUGS_INLINE
      ~CastArray() = default;
    };
    
    template <typename CastDataType>
    struct cast_array_to
    {
      template <typename DataType>
      PUGS_INLINE static CastArray<DataType, CastDataType>
      from(const Array<DataType>& array)
      {
        return CastArray<DataType, CastDataType>(array);
      }
    };
    
    template <typename CastDataType>
    struct cast_value_to
    {
      template <typename DataType>
      PUGS_INLINE static CastArray<DataType, CastDataType>
      from(DataType& value)
      {
        return CastArray<DataType, CastDataType>(value);
      }
    };
    
    #endif   // CAST_ARRAY_HPP