Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
IVectorConverter.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8
9#pragma once
10
11#include <alien/core/backend/BackEnd.h>
13#include <alien/utils/ObjectWithTrace.h>
14
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18namespace Alien
19{
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24/*!
25 * \ingroup core
26 * \brief Vectors converter
27 *
28 * Interface to convert a vector in another format, provided a converter between those two
29 * formats has been registered.
30 * If there is no direct converter between the source and target format, a third
31 * intermediate format will be used if available.
32 *
33 * \todo IVectorImpl.h is needed only for the template definition. Could be removed
34 */
35class IVectorConverter : public ObjectWithTrace
36{
37 public:
38 //! Free resources
39 virtual ~IVectorConverter() {}
40
41 public:
42 /*!
43 * \brief Get the source backend id
44 * \returns The source backend id
45 */
46 virtual BackEndId sourceBackend() const = 0;
47
48 /*!
49 * \brief Get the target backend id
50 * \returns The target backend id
51 */
52 virtual BackEndId targetBackend() const = 0;
53
54 /*!
55 * \brief Convert a vector from one format to another
56 * \param[in] sourceImpl Implementation of the source vector
57 * \param[in,out] targetImpl Implementation of the target vector
58 */
59 virtual void convert(const IVectorImpl* sourceImpl, IVectorImpl* targetImpl) const = 0;
60
61 protected:
62 /*!
63 * \brief Get the target backend id
64 * \returns The target backend id
65 */
66 template <typename T>
67 BackEndId backendId() const { return AlgebraTraits<T>::name(); }
68
69 /*!
70 * \brief Cast a vector implementation in its actual type
71 * \param[in] impl Vector implementation
72 * \param[in] backend Backend id
73 * \returns The actual vector implementation
74 *
75 * \todo Check backend using T
76 */
77 template <typename T>
78 static T& cast(IVectorImpl* impl, [[maybe_unused]] BackEndId backend)
79 {
80 ALIEN_ASSERT((impl != NULL), ("Null implementation"));
81 ALIEN_ASSERT((impl->backend() == backend), ("Bad backend"));
82 T* t = dynamic_cast<T*>(impl);
83 ALIEN_ASSERT((t != NULL), ("Bad dynamic cast"));
84 return *t;
85 }
86
87 /*!
88 * \brief Const cast a vector implementation in its actual type
89 * \param[in] impl Vector implemenantation
90 * \param[in] backend Backend id
91 * \returns The actual vector implementation
92 *
93 * \todo Check backend using T
94 */
95 template <typename T>
96 static const T& cast(const IVectorImpl* impl, [[maybe_unused]] BackEndId backend)
97 {
98 ALIEN_ASSERT((impl != NULL), ("Null implementation"));
99 ALIEN_ASSERT((impl->backend() == backend), ("Bad backend"));
100 const T* t = dynamic_cast<const T*>(impl);
101 ALIEN_ASSERT((t != NULL), ("Bad dynamic cast"));
102 return *t;
103 }
104};
105
106template<typename SourceTagT,
107 typename TargetTagT>
109{
110public:
111 using SourceVectorType = typename AlgebraTraits<SourceTagT>::vector_type;
112 using TargetVectorType = typename AlgebraTraits<TargetTagT>::vector_type;
113 //! Free resources
114 virtual ~VectorConverterT() {}
115
116 virtual void convert(SourceVectorType const& source, TargetVectorType& target) const = 0 ;
117};
118/*---------------------------------------------------------------------------*/
119/*---------------------------------------------------------------------------*/
120
121} // namespace Alien
122
123/*---------------------------------------------------------------------------*/
124/*---------------------------------------------------------------------------*/
IVectorImpl.h.
virtual ~IVectorConverter()
Free resources.
static T & cast(IVectorImpl *impl, BackEndId backend)
Cast a vector implementation in its actual type.
virtual void convert(const IVectorImpl *sourceImpl, IVectorImpl *targetImpl) const =0
Convert a vector from one format to another.
static const T & cast(const IVectorImpl *impl, BackEndId backend)
Const cast a vector implementation in its actual type.
virtual BackEndId targetBackend() const =0
Get the target backend id.
virtual BackEndId sourceBackend() const =0
Get the source backend id.
BackEndId backendId() const
Get the target backend id.
Interface to handle abstract vectors implementation.
Definition IVectorImpl.h:47
virtual BackEndId backend() const
Definition IVectorImpl.h:95
virtual ~VectorConverterT()
Free resources.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17