visitor-pattern

Forward Declaration of Template Class (Visitor Design Pattern)

≡放荡痞女 提交于 2020-01-06 05:08:28
问题 I am trying to forward declare a templated class A<T> for use in a class Visitor . It would suffice for my purposes to declare the int instance A<int> of the class A . I have tried two approaches but both give different errors, and I don't know how to proceed. Here is a MWE of my error: namespace visitor{ class Visitor{ public: virtual void visit(nsp::A<int>*) = 0; }; } namespace nsp{ template <class T> class A{ A(); T t_attribute; void accept(visitor::Visitor*); }; void A<int>::accept

Visitor Pattern + Open/Closed Principle

馋奶兔 提交于 2019-12-31 10:36:16
问题 Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle, but still be able to add new visitable classes? The Open/Closed Principle states that "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification". struct ConcreteVisitable1; struct ConcreteVisitable2; struct AbstractVisitor { virtual void visit(ConcreteVisitable1& concrete1) = 0; virtual void visit(ConcreteVisitable2& concrete2) = 0; }; struct

Java Enums - Switch statements vs Visitor Pattern on Enums - Performance benefits?

这一生的挚爱 提交于 2019-12-30 06:25:08
问题 I have been searching around for days to find an answer to this performance based issue. After digging the Internet so far I have learned that there are couple of ways to use the Enums in java, well documented in here. Well, definitely as a starter one would like use Enums in a switch-case statement, which provides clarity and better understanding of the code. But on the other hand we have a Visitor pattern style implementation of the Enums as well, which ensures type safety and extensibility

Java Enums - Switch statements vs Visitor Pattern on Enums - Performance benefits?

二次信任 提交于 2019-12-30 06:24:41
问题 I have been searching around for days to find an answer to this performance based issue. After digging the Internet so far I have learned that there are couple of ways to use the Enums in java, well documented in here. Well, definitely as a starter one would like use Enums in a switch-case statement, which provides clarity and better understanding of the code. But on the other hand we have a Visitor pattern style implementation of the Enums as well, which ensures type safety and extensibility

Hirerate trough an heterogeneous and type-safe dictionary

烈酒焚心 提交于 2019-12-25 19:05:14
问题 I need a container that works like a ditionary but where the type of data ( TValue ) change from one key to the other. I also need to iterate trough it. 回答1: For the heterogeneous and type-safe dictionary part Wilka response is a good start. The trick is to put the type in the key. /// <summary> /// Base class for all dictionary key. /// /// <remarks>The key name is REALLY usefull for debug purpose.</remarks> /// </summary> abstract class HeterogeneousDictionaryKeyBase { readonly string _name

Parser in C# and printing AST

左心房为你撑大大i 提交于 2019-12-25 07:09:28
问题 I am implementing an AST (Abstract Syntax Tree) in C# for a complex grammar, however, to make this question simple, I will use a very simple grammar. Consider this grammar: rules Expr ::= Term "+" Term | Term ; rules Term ::= Ident | Integer ; I have used bnfc and generated the parser/lexer and got to the point that I can parse a piece of code and can print the parse tree. Now I want to map it to AST, and print the Abstract Syntax Tree. here is what I have done so far in a sample project.

Parser in C# and printing AST

依然范特西╮ 提交于 2019-12-25 07:07:20
问题 I am implementing an AST (Abstract Syntax Tree) in C# for a complex grammar, however, to make this question simple, I will use a very simple grammar. Consider this grammar: rules Expr ::= Term "+" Term | Term ; rules Term ::= Ident | Integer ; I have used bnfc and generated the parser/lexer and got to the point that I can parse a piece of code and can print the parse tree. Now I want to map it to AST, and print the Abstract Syntax Tree. here is what I have done so far in a sample project.

How to avoid downcast?

允我心安 提交于 2019-12-22 05:07:14
问题 I have an implementation of a State Pattern where each state handles events it gets from a event queue. Base State class therefore has a pure virtual method void handleEvent(const Event*) . Events inherit base Event class but each event contains its data that can be of a different type (e.g. int, string...or whatever). handleEvent has to determine the runtime type of the received event and then perform downcast in order to extract event data. Events are dynamically created and stored in a

What is Single and Double Dispatch?

扶醉桌前 提交于 2019-12-19 05:44:08
问题 i have wrote the visitor pattern as follow but i don't understand what is single and double dispatch. AFAIK, single dispatch is invoke a method based on caller type where double dispatch is invoke a method based on caller type and argument type. I guess double dispatch is happen in single class hierarchy but why visitor class has two class hierarchy but it still considered as double dispatch. void floppyDisk::accept(equipmentVisitor* visitor) { visitor->visitFloppyDisk(this); } void processor

QVariant's Visitor pattern (without manual type testing and casting)

耗尽温柔 提交于 2019-12-18 05:20:32
问题 Does Qt's QVariant class has any existing (and convenient) Visitor pattern implementation? If not, is it possible to achieve something similar to boost::apply_visitor() , i.e. minimize the the duplication in regard to testing the type and casting? I want to achieve something along the following lines: /* I have a QVariant that can contain anything, including user types */ QVariant variant; /* But in my Visitor I'm interested only in ints and QStrings (for the sake of the example) */ struct