• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_CORE_MINDAPI_BASE_BASE_H_
18 #define MINDSPORE_CORE_MINDAPI_BASE_BASE_H_
19 
20 #include <cstdint>
21 #include <string>
22 #include <memory>
23 #include "mindapi/base/macros.h"
24 #include "mindapi/base/type_traits.h"
25 #include "mindapi/base/shared_ptr.h"
26 
27 namespace mindspore {
28 class Base;
29 }
30 
31 namespace mindspore::api {
32 /// \brief Base is the base class of many api classes, which provides basic interfaces.
33 class MIND_API Base {
34  public:
35   /// \brief Create an instance from the given implementation object.
36   ///
37   /// \param[in] impl The shared_ptr to the implementation object.
38   explicit Base(const std::shared_ptr<mindspore::Base> &impl);
39 
40   /// \brief Destructor of Base.
41   virtual ~Base() = default;
42 
43   /// \brief Get the id of this class.
44   ///
45   /// \return The id of this class.
46   static uint32_t ClassId();
47 
48   /// \brief Get the shared_ptr to the underly implementation object.
49   ///
50   /// \return The shared_ptr to the underly implementation object.
impl()51   const std::shared_ptr<mindspore::Base> &impl() const { return impl_; }
52 
53   /// \brief Get the string representation of this object.
54   ///
55   /// \return The string representation.
56   std::string ToString() const;
57 
58   /// \brief Check whether this object is an instance of the given class.
59   ///
60   /// \return True if this object is an instance of the given class, false otherwise.
61   template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<Base, T>, T>>
isa()62   inline bool isa() const {
63     return IsFromClassId(T::ClassId());
64   }
65 
66   /// \brief Cast this object to a pointer with the given pointer class.
67   ///
68   /// \return A non-null pointer if cast success, nullptr otherwise.
69   template <typename T, typename U = typename std::enable_if_t<is_wrapper_ptr<T>::value, typename T::element_type>>
cast()70   inline T cast() {
71     if (isa<U>()) {
72       return MakeShared<U>(impl_);
73     }
74     return nullptr;
75   }
76 
77  protected:
78   bool IsFromClassId(uint32_t class_id) const;
79   const std::shared_ptr<mindspore::Base> impl_;
80 };
81 
82 #define MIND_API_BASE_MEMBER(current_class)                             \
83   explicit current_class(const std::shared_ptr<mindspore::Base> &impl); \
84   ~current_class() override = default;                                  \
85   static uint32_t ClassId()
86 
87 using BasePtr = SharedPtr<Base>;
88 }  // namespace mindspore::api
89 #endif  // MINDSPORE_CORE_MINDAPI_BASE_BASE_H_
90