///\file /****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl https://www.etlcpp.com Copyright(c) 2014 John Wellbelove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #ifndef ETL_VISITOR_INCLUDED #define ETL_VISITOR_INCLUDED #include "platform.h" #include "type_traits.h" //***************************************************************************** ///\defgroup visitor visitor /// A set of template classes for easy implementation of the visitor pattern.
/// The visitor design pattern is a way of separating an algorithm from an object /// structure on which it operates. A practical result of this separation is the /// ability to add new operations to existing object structures without modifying /// those structures. It is one way to easily follow the open/closed principle. /// In essence, the visitor allows one to add new virtual functions to a family /// of classes without modifying the classes themselves; instead, one creates a /// visitor class that implements all of the appropriate specialisations of the /// virtual function. The visitor takes the instance as input, and implements /// the goal through double dispatch.
/// \ingroup patterns //***************************************************************************** namespace etl { #if ETL_USING_CPP11 && !defined(ETL_VISITOR_FORCE_CPP03_IMPLEMENTATION) //***************************************************************** /// The visitable class for N types. ///\ingroup visitor //***************************************************************** template class visitable : public visitable, public visitable { public: using visitable::accept; using visitable::accept; }; //***************************************************************** /// The specialised visitable class for 1 type. ///\ingroup visitor //***************************************************************** template class visitable { public: virtual ~visitable() = default; virtual void accept(T1&) = 0; }; #else //***************************************************************** /// The visitable base class for four visitor types. /// Derive visitable classes from this. ///\ingroup visitor //***************************************************************** template class visitable { public: virtual ~visitable() {} virtual void accept(T1&) = 0; virtual void accept(T2&) = 0; virtual void accept(T3&) = 0; virtual void accept(T4&) = 0; }; //***************************************************************** /// The visitable base class for three visitor types. /// Derive visitable classes from this. ///\ingroup visitor //***************************************************************** template class visitable { public: virtual ~visitable() {} virtual void accept(T1&) = 0; virtual void accept(T2&) = 0; virtual void accept(T3&) = 0; }; //***************************************************************** /// The visitable base class for two visitor types. /// Derive visitable classes from this. ///\ingroup visitor //***************************************************************** template class visitable { public: virtual ~visitable() {} virtual void accept(T1&) = 0; virtual void accept(T2&) = 0; }; //***************************************************************** /// The visitable base class for one visitor type. /// Derive visitable classes from this. ///\ingroup visitor //***************************************************************** template class visitable { public: virtual ~visitable() {} virtual void accept(T1&) = 0; }; #endif //***************************************************************** /// The tag to identify an etl::visitor. ///\ingroup visitor //***************************************************************** struct visitor_tag { }; #if ETL_USING_CPP11 && !defined(ETL_VISITOR_FORCE_CPP03_IMPLEMENTATION) //***************************************************************** /// The visitor class for N types. ///\ingroup visitor //***************************************************************** template class visitor : public visitor, public visitor { public: using visitor::visit; using visitor::visit; }; //***************************************************************** /// The specialised visitor class for 1 type. ///\ingroup visitor //***************************************************************** template class visitor : public virtual visitor_tag { public: virtual ~visitor() = default; virtual void visit(T1) = 0; }; #else //***************************************************************** /// The visitor base class for sixteen types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; virtual void visit(T9) = 0; virtual void visit(T10) = 0; virtual void visit(T11) = 0; virtual void visit(T12) = 0; virtual void visit(T13) = 0; virtual void visit(T14) = 0; virtual void visit(T15) = 0; virtual void visit(T16) = 0; }; //***************************************************************** /// The visitor base class for fifteen types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; virtual void visit(T9) = 0; virtual void visit(T10) = 0; virtual void visit(T11) = 0; virtual void visit(T12) = 0; virtual void visit(T13) = 0; virtual void visit(T14) = 0; virtual void visit(T15) = 0; }; //***************************************************************** /// The visitor base class for fourteen types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; virtual void visit(T9) = 0; virtual void visit(T10) = 0; virtual void visit(T11) = 0; virtual void visit(T12) = 0; virtual void visit(T13) = 0; virtual void visit(T14) = 0; }; //***************************************************************** /// The visitor base class for thirteen types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; virtual void visit(T9) = 0; virtual void visit(T10) = 0; virtual void visit(T11) = 0; virtual void visit(T12) = 0; virtual void visit(T13) = 0; }; //***************************************************************** /// The visitor base class for twelve types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; virtual void visit(T9) = 0; virtual void visit(T10) = 0; virtual void visit(T11) = 0; virtual void visit(T12) = 0; }; //***************************************************************** /// The visitor base class for eleven types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; virtual void visit(T9) = 0; virtual void visit(T10) = 0; virtual void visit(T11) = 0; }; //***************************************************************** /// The visitor base class for ten types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; virtual void visit(T9) = 0; virtual void visit(T10) = 0; }; //***************************************************************** /// The visitor base class for nine types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; virtual void visit(T9) = 0; }; //***************************************************************** /// The visitor base class for eight types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; virtual void visit(T8) = 0; }; //***************************************************************** /// The visitor base class for seven types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; virtual void visit(T7) = 0; }; //***************************************************************** /// The visitor base class for six types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; virtual void visit(T6) = 0; }; //***************************************************************** /// The visitor base class for five types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; virtual void visit(T5) = 0; }; //***************************************************************** /// The visitor base class for four types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; virtual void visit(T4) = 0; }; //***************************************************************** /// The visitor base class for three types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; virtual void visit(T3) = 0; }; //***************************************************************** /// The visitor base class for two types. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; virtual void visit(T2) = 0; }; //***************************************************************** /// The visitor base class for one type. /// Derive visitors from this. ///\ingroup visitor //***************************************************************** template class visitor : public visitor_tag { public: virtual ~visitor() {} virtual void visit(T1) = 0; }; #endif //***************************************************************** /// Is the type an etl::visitor? ///\ingroup visitor //***************************************************************** template struct is_visitor : public etl::bool_constant::value> { }; #if ETL_USING_CPP17 //***************************************************************** /// Is the type an etl::visitor? ///\ingroup visitor //***************************************************************** template constexpr bool is_visitor_v = etl::is_visitor::value; #endif } #endif