Skip to content
Snippets Groups Projects
Select Git revision
  • ec4cd3baca13f0337bf1532200b9baf54a154d50
  • develop default protected
  • feature/variational-hydro
  • origin/stage/bouguettaia
  • feature/gmsh-reader
  • feature/reconstruction
  • save_clemence
  • feature/kinetic-schemes
  • feature/local-dt-fsi
  • feature/composite-scheme-sources
  • feature/composite-scheme-other-fluxes
  • 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
  • master protected
  • 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

FunctionArgumentConverter.hpp

Blame
  • CastArray.hpp 2.47 KiB
    #ifndef CAST_ARRAY_HPP
    #define CAST_ARRAY_HPP
    
    #include <Array.hpp>
    #include <PastisTraits.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:
      PASTIS_INLINE
      const size_t& size() const
      {
        return m_size;
      }
    
      PASTIS_INLINE
      CastDataType& operator[](const size_t& i) const
      {
        Assert(i<m_size);
        return m_values[i];
      }
    
      PASTIS_INLINE
      CastArray& operator=(const CastArray&) = default;
    
      PASTIS_INLINE
      CastArray& operator=(CastArray&&) = default;
    
      PASTIS_INLINE
      CastArray()
          : m_size(0),
            m_values(nullptr)
      {
        ;
      }
    
      PASTIS_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)) {
          std::cerr << "cannot cast array to the chosen data type\n";
          std::exit(1);
        }
      }
    
      PASTIS_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");
      }
    
      PASTIS_INLINE
      CastArray(DataType&& value) = delete;
    
      PASTIS_INLINE
      CastArray(const CastArray&) = default;
    
      PASTIS_INLINE
      CastArray(CastArray&&) = default;
    
      PASTIS_INLINE
      ~CastArray() = default;
    };
    
    template <typename CastDataType>
    struct cast_array_to
    {
      template <typename DataType>
      PASTIS_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>
      PASTIS_INLINE
      static CastArray<DataType, CastDataType>
      from(DataType& value)
      {
        return CastArray<DataType, CastDataType>(value);
      }
    };
    
    #endif // CAST_ARRAY_HPP