From b3ab699cee458693d18d7b9b0c25526e97b5d156 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 30 Mar 2019 09:59:44 +0000 Subject: [PATCH] Added C++11 variadic template version of etl::visiable and etl::visitor --- include/etl/visitor.h | 72 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/include/etl/visitor.h b/include/etl/visitor.h index 70e58252..3273eb68 100644 --- a/include/etl/visitor.h +++ b/include/etl/visitor.h @@ -40,16 +40,45 @@ SOFTWARE. /// 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 +/// 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 +/// virtual function. The visitor takes the instance as input, and implements /// the goal through double dispatch.
/// \ingroup patterns //***************************************************************************** namespace etl { +#if ETL_CPP11_SUPPORTED && !defined(ETL_VISITOR_FORCE_CPP03) + + //***************************************************************** + /// 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 void accept(T1&) = 0; + }; + +#else + //***************************************************************** /// The visitable base class for four visitor types. /// Derive visitable classes from this. @@ -108,6 +137,37 @@ namespace etl virtual void accept(T1&) = 0; }; +#endif + +#if ETL_CPP11_SUPPORTED && !defined(ETL_VISITOR_FORCE_CPP03) + + //***************************************************************** + /// 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 void visit(T1&) = 0; + }; + +#else + //***************************************************************** /// The visitor base class for sixteen types. /// Derive visitors from this. @@ -115,7 +175,7 @@ namespace etl //***************************************************************** template class visitor { @@ -323,7 +383,7 @@ namespace etl virtual void visit(T8&) = 0; virtual void visit(T9&) = 0; }; - + //***************************************************************** /// The visitor base class for eight types. /// Derive visitors from this. @@ -459,6 +519,8 @@ namespace etl virtual void visit(T1&) = 0; }; + +#endif } #endif