Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add convenience casting for NodeWalker

Summary:
It can be a pain to have to cast subclasses of NodeWalker all the time using static_cast and dynamic_cast, especially with auto_ptr's. This adds an overloaded template cast() function for use in subclasses of NodeWalker; so you can do `cast(MyNodeWalkerClass).some_var` to cast your class. It will throw if you pass an invalid class so you can just all NodeWalker* instances without error-checking.

Test Plan:
Compiles

Reviewed By: epriestley
  • Loading branch information...
commit 1c11917a514e98d34b24c1c57b1344b0a7891169 1 parent 9a77508
@laverdet laverdet authored
Showing with 17 additions and 0 deletions.
  1. +17 −0 walker.hpp
View
17 walker.hpp
@@ -46,6 +46,23 @@ namespace fbjs {
}
protected:
+ template<class T>
+ static T& cast(NodeWalker& node) {
+ T* tmp = dynamic_cast<T*>(&node);
+ if (tmp) {
+ return *tmp;
+ } else {
+ throw std::runtime_error("invalid NodeWalker");
+ }
+ }
+
+ template<class T>
+ static std::auto_ptr<T> cast(NodeWalker::ptr node) {
+ std::auto_ptr<T> casted(&cast<T>(*node));
+ node.release();
+ return casted;
+ }
+
void remove(bool skip_delete = false) {
_remove = true;
_skip_delete = skip_delete;
Please sign in to comment.
Something went wrong with that request. Please try again.