factory_adapter.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 Intel Corporation
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 #pragma once
18 
19 #include "ngraph/attribute_adapter.hpp"
20 #include "ngraph/attribute_visitor.hpp"
21 #include "ngraph/factory.hpp"
22 
23 namespace ngraph
24 {
25  template <typename BASE_TYPE>
27  {
28  public:
29  FactoryAttributeAdapter(std::shared_ptr<BASE_TYPE>& ref)
30  : m_ref(ref)
31  {
32  }
33 
34  /// \brief Hook for extra processing before other attributes
35  virtual bool on_start(AttributeVisitor& /* visitor */) { return true; }
36  /// \brief Hook for extra processing after other attributes
37  virtual bool on_finish(AttributeVisitor& /* visitor */) { return true; }
38  bool visit_attributes(AttributeVisitor& visitor) override
39  {
40  if (on_start(visitor))
41  {
42  std::string type_info_name;
43  uint64_t type_info_version;
44  if (m_ref)
45  {
46  auto& type_info = m_ref->get_type_info();
47  type_info_name = type_info.name;
48  type_info_version = type_info.version;
49  }
50  visitor.on_attribute("name", type_info_name);
51  visitor.on_attribute("version", type_info_version);
52  if (m_ref)
53  {
54  visitor.start_structure("value");
55  m_ref->visit_attributes(visitor);
56  visitor.finish_structure();
57  }
58  on_finish(visitor);
59  }
60  return true;
61  }
62 
63  protected:
64  std::shared_ptr<BASE_TYPE>& m_ref;
65  };
66 }
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:71
virtual void start_structure(const std::string &name)
Start visiting a nested structure.
void on_attribute(const std::string &name, AT &value)
Definition: attribute_visitor.hpp:129
virtual std::string finish_structure()
Finish visiting a nested structure.
Definition: factory_adapter.hpp:27
virtual bool on_start(AttributeVisitor &)
Hook for extra processing before other attributes.
Definition: factory_adapter.hpp:35
virtual bool on_finish(AttributeVisitor &)
Hook for extra processing after other attributes.
Definition: factory_adapter.hpp:37
Adapters will see visitor.
Definition: attribute_adapter.hpp:194
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28