/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include #include #include #include #include "paddle/fluid/framework/ir/node.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/variant.h" namespace paddle { namespace framework { class Graph; template struct AnyAttr { public: explicit AnyAttr(AttrType* attr) : attr_(attr) {} AttrType& Get() { return *boost::any_cast(attr_); } private: friend Graph; AttrType* Release() { released_ = true; return boost::any_cast(attr_); } void Delete() { if (!released_) { delete boost::any_cast(attr_); } } bool released_ = false; boost::any attr_; }; class Graph { public: virtual ~Graph() { for (auto& attr : attrs) { attr_dels[attr.first](); } attrs.clear(); attr_dels.clear(); } template AttrType& Get(const std::string& attr_name) { return boost::any_cast>(attrs[attr_name]).Get(); } template void Set(const std::string& attr_name, AttrType* attr) { AnyAttr any_attr = AnyAttr(attr); attrs[attr_name] = any_attr; attr_dels[attr_name] = [&any_attr]() { any_attr.Delete(); }; } template AttrType* Erase(const std::string& attr_name) { AnyAttr attr_type = boost::any_cast>(attrs[attr_name]); attrs.erase(attr_name); attr_dels.erase(attr_name); return attr_type.Release(); } std::vector inputs; std::vector outputs; std::vector> nodes; std::map attrs; std::map> attr_dels; private: }; } // namespace framework } // namespace paddle