diff --git a/acast2/src/CastVisitorBase.hpp b/acast2/src/CastVisitorBase.hpp index 3dd82f692b7b96a63c978ba540c54152acbccb43..38aa6ceb72ba2e53f06f0601cd421b312316694a 100644 --- a/acast2/src/CastVisitorBase.hpp +++ b/acast2/src/CastVisitorBase.hpp @@ -9,97 +9,104 @@ #include <factory/XmlDataFactory.hpp> #include <type_traits> -template<typename T, typename R, typename std::enable_if< std::is_constructible<T, R>::value >::type* = nullptr > -T cast(const R& orig) { - return T(orig); +template<typename ToType, typename FromType, typename std::enable_if< std::is_constructible<ToType, FromType>::value >::type* = nullptr > +ToType cast(const FromType& orig) { + return ToType(orig); } -template<typename T, typename R, typename std::enable_if< ! std::is_constructible<T, R>::value >::type* = nullptr > -T cast(const R& /* orig */) { - throw exception::AlibException(std::string("Invalid cast from ") + typeid(R).name() + " to " + typeid(T).name()); +template<typename ToType, typename FromType, typename std::enable_if< ! std::is_constructible<ToType, FromType>::value >::type* = nullptr > +ToType cast(const FromType& /* orig */) { + throw exception::AlibException(std::string("Invalid cast from ") + typeid(FromType).name() + " to " + typeid(ToType).name()); } // -------------------------------------------------------------------------------------------------------------- -template<typename BaseVisitor, typename R, typename ... Types> +template<typename FromBaseVisitor, typename ToType, typename ToWrapper, typename ... FromTypes> class cast_implementer_helper; -template<typename BaseVisitor, typename R> -class cast_implementer_helper< BaseVisitor, R > : public BaseVisitor { +template<typename FromBaseVisitor, typename ToType, typename ToWrapper> +class cast_implementer_helper< FromBaseVisitor, ToType, ToWrapper > : public FromBaseVisitor { }; -template<typename BaseVisitor, typename R, typename T, typename ... Types> -class cast_implementer_helper<BaseVisitor, R, T, Types ...> : public cast_implementer_helper<BaseVisitor, R, Types ...> { +template<typename FromBaseVisitor, typename ToType, typename ToWrapper, typename FromType, typename ... FromTypes> +class cast_implementer_helper<FromBaseVisitor, ToType, ToWrapper, FromType, FromTypes ...> : public cast_implementer_helper<FromBaseVisitor, ToType, ToWrapper, FromTypes ...> { public: #ifdef __llvm__ - using cast_implementer_helper<BaseVisitor, R, Types ...>::Visit; + using cast_implementer_helper<FromBaseVisitor, ToType, ToWrapper, FromTypes ...>::Visit; #endif - void Visit(void*, const T& orig) const; + void Visit(void*, const FromType& orig) const; }; -template<typename BaseVisitor, typename R, typename T, typename ... Types> -void cast_implementer_helper<BaseVisitor, R, T, Types ...>::Visit(void*, const T& orig) const { - alib::XmlDataFactory::toStdout(cast<R, T>(orig)); +template<typename FromBaseVisitor, typename ToType, typename ToWrapper, typename FromType, typename ... FromTypes> +void cast_implementer_helper<FromBaseVisitor, ToType, ToWrapper, FromType, FromTypes ...>::Visit(void* data, const FromType& orig) const { + ToWrapper* & out = *((ToWrapper**) data); + out = new ToWrapper(cast<ToType, FromType>(orig)); } // -------------------------------------------------------------------------------------------------------------------- -template<typename BaseVisitor, typename Base, typename R, typename Types> +template<typename FromBaseVisitor, typename FromBase, typename ToWrapper, typename ToType, typename FromTypes> class cast_implementer { }; -template<typename BaseVisitor, typename Base, typename R, typename ... Ts> -class cast_implementer< BaseVisitor, Base, R, std::tuple< Ts ...> > : public cast_implementer_helper< BaseVisitor, R, Ts ... > { +template<typename FromBaseVisitor, typename FromBase, typename ToWrapper, typename ToType, typename ... FromTypes> +class cast_implementer< FromBaseVisitor, FromBase, ToWrapper, ToType, std::tuple< FromTypes ... > > : public cast_implementer_helper< FromBaseVisitor, ToType, ToWrapper, FromTypes ... > { public: - using cast_implementer_helper<BaseVisitor, R, Ts ... >::Visit; + using cast_implementer_helper<FromBaseVisitor, ToType, ToWrapper, FromTypes ... >::Visit; - static void cast(const Base& obj); + static ToWrapper cast(const FromBase& obj); }; -template<typename BaseVisitor, typename Base, typename R, typename ... Ts> -void cast_implementer<BaseVisitor, Base, R, std::tuple< Ts ... > >::cast(const Base& obj) { - cast_implementer<BaseVisitor, Base, R, std::tuple< Ts ... > > tmp; - obj.Accept(NULL, tmp); +template<typename FromBaseVisitor, typename FromBase, typename ToWrapper, typename ToType, typename ... FromTypes> +ToWrapper cast_implementer<FromBaseVisitor, FromBase, ToWrapper, ToType, std::tuple< FromTypes ... > >::cast(const FromBase& obj) { + cast_implementer<FromBaseVisitor, FromBase, ToWrapper, ToType, std::tuple< FromTypes ... > > tmp; + ToWrapper* out = NULL; + obj.Accept((void*) &out, tmp); + ToWrapper res = std::move(*out); + delete out; + return res; } // ---------------------------------------------------------------------------------------- -template<typename BaseVisitor, typename Base, typename Types, typename ... Rs> +template<typename FromBaseVisitor, typename FromBase, typename FromTypes, typename ToWrapper, typename ... ToTypes > class cast_helper; -template<typename BaseVisitor, typename Base, typename Types> -class cast_helper<BaseVisitor, Base, Types> { +template<typename FromBaseVisitor, typename FromBase, typename FromTypes, typename ToWrapper > +class cast_helper<FromBaseVisitor, FromBase, FromTypes, ToWrapper> { public: - static void do_cast(const std::string&, const Base&/* obj*/) { + static ToWrapper do_cast(const std::string&, const FromBase&/* obj*/) { throw exception::AlibException("invalid type specified"); } }; -template<typename BaseVisitor, typename Base, typename Types, typename R, typename ... Rs > -class cast_helper< BaseVisitor, Base, Types, R, Rs ... > : public cast_helper< BaseVisitor, Base, Types, Rs ...> { +template<typename FromBaseVisitor, typename FromBase, typename FromTypes, typename ToWrapper, typename ToType, typename ... ToTypes > +class cast_helper< FromBaseVisitor, FromBase, FromTypes, ToWrapper, ToType, ToTypes ... > : public cast_helper< FromBaseVisitor, FromBase, FromTypes, ToWrapper, ToTypes ...> { public: - static void do_cast(const std::string& name, const Base& obj); + static ToWrapper do_cast(const std::string& name, const FromBase& obj); }; -template<typename BaseVisitor, typename Base, typename Types, typename R, typename ... Rs > -void cast_helper<BaseVisitor, Base, Types, R, Rs ...>::do_cast(const std::string& name, const Base& obj) { - if(std::is_same_type<R>(name.c_str())) { - cast_implementer<BaseVisitor, Base, R, Types>::cast(obj); +template<typename FromBaseVisitor, typename FromBase, typename FromTypes, typename ToWrapper, typename ToType, typename ... ToTypes > +ToWrapper cast_helper<FromBaseVisitor, FromBase, FromTypes, ToWrapper, ToType, ToTypes ...>::do_cast(const std::string& name, const FromBase& obj) { + if(std::is_same_type<ToType>(name.c_str())) { + return cast_implementer<FromBaseVisitor, FromBase, ToWrapper, ToType, FromTypes>::cast(obj); } else { - cast_helper<BaseVisitor, Base, Types, Rs ...>::do_cast(name, obj); + return cast_helper<FromBaseVisitor, FromBase, FromTypes, ToWrapper, ToTypes ...>::do_cast(name, obj); } } // ----------------------------------------------------------------------------------------- -template<typename BaseVisitor, typename Base, typename Types> +// visitor, fromType (base), toType (wrapper), possible from types, possible to types + +template<typename FromBaseVisitor, typename FromBase, typename FromTypes, typename ToWrapper, typename ToTypes> class cast_base_helper { }; -template<typename BaseVisitor, typename Base, typename ... Ts> -class cast_base_helper< BaseVisitor, Base, std::tuple< Ts ... > > : public cast_helper< BaseVisitor, Base, std::tuple<Ts ...>, Ts ... > { +template<typename FromBaseVisitor, typename FromBase, typename ... FromTypes, typename ToWrapper, typename ... ToTypes> +class cast_base_helper< FromBaseVisitor, FromBase, std::tuple<FromTypes ...>, ToWrapper, std::tuple< ToTypes ... > > : public cast_helper< FromBaseVisitor, FromBase, std::tuple< FromTypes ...>, ToWrapper, ToTypes ... > { public: }; diff --git a/acast2/src/acast.cpp b/acast2/src/acast.cpp index 706dd080c0cc42a2f244d9f7c4571afdd8b9a3d2..995427d2718b9b727809b1bb7a46b0b4b59f7736 100644 --- a/acast2/src/acast.cpp +++ b/acast2/src/acast.cpp @@ -17,6 +17,7 @@ #include "cast/GrammarCastVisitor.h" #include "cast/RegExpCastVisitor.h" #include "cast/TreeCastVisitor.h" +#include "cast/TreeToStringCastVisitor.h" // ----------------------------------------------------------------------------------------- @@ -30,8 +31,16 @@ int main(int argc, char** argv) { TCLAP::ValueArg<std::string> type( "t", "type", "Output type", true, "", "string"); cmd.add( type ); + TCLAP::SwitchArg measure( "m", "measure", "Measure times", false); + cmd.add( measure ); + + TCLAP::SwitchArg verbose( "v", "verbose", "Be verbose", false); + cmd.add( verbose ); + cmd.parse(argc, argv); + std::chrono::measurements::start("Overal", std::chrono::measurements::Type::OVERALL); + std::chrono::measurements::start("Input read", std::chrono::measurements::Type::AUXILARY); std::deque<sax::Token> tokens; if(input.isSet()) { if(input.getValue() == "-") { @@ -47,24 +56,74 @@ int main(int argc, char** argv) { CastBaseVisitor::do_cast(type.getValue(), object.getData());*/ if(alib::XmlDataFactory::first<tree::PrefixRankedTree>(tokens) && std::is_same_type<string::LinearString>(type.getValue().c_str())) { - tree::PrefixRankedTree tree = alib::XmlDataFactory::fromTokens<tree::PrefixRankedTree>(tokens); - alib::XmlDataFactory::toStdout(string::LinearString(tree)); + tree::Tree tree = alib::XmlDataFactory::fromTokens<tree::Tree>(tokens); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Cast", std::chrono::measurements::Type::MAIN); + + string::String res = TreeToStringCastVisitor::doCast(type.getValue(), tree); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Output write", std::chrono::measurements::Type::AUXILARY); + + alib::XmlDataFactory::toStdout(res); } else if(alib::XmlDataFactory::first<automaton::Automaton>(tokens)) { automaton::Automaton automaton = alib::XmlDataFactory::fromTokens<automaton::Automaton>(tokens); - AutomatonCastVisitor::doCast(type.getValue(), automaton); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Cast", std::chrono::measurements::Type::MAIN); + + automaton::Automaton res = AutomatonCastVisitor::doCast(type.getValue(), automaton); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Output write", std::chrono::measurements::Type::AUXILARY); + + alib::XmlDataFactory::toStdout(res); } else if(alib::XmlDataFactory::first<grammar::Grammar>(tokens)) { grammar::Grammar grammar = alib::XmlDataFactory::fromTokens<grammar::Grammar>(tokens); - GrammarCastVisitor::doCast(type.getValue(), grammar); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Cast", std::chrono::measurements::Type::MAIN); + + grammar::Grammar res = GrammarCastVisitor::doCast(type.getValue(), grammar); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Output write", std::chrono::measurements::Type::AUXILARY); + + alib::XmlDataFactory::toStdout(res); } else if(alib::XmlDataFactory::first<regexp::RegExp>(tokens)) { regexp::RegExp regexp = alib::XmlDataFactory::fromTokens<regexp::RegExp>(tokens); - RegExpCastVisitor::doCast(type.getValue(), regexp); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Cast", std::chrono::measurements::Type::MAIN); + + regexp::RegExp res = RegExpCastVisitor::doCast(type.getValue(), regexp); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Output write", std::chrono::measurements::Type::AUXILARY); + + alib::XmlDataFactory::toStdout(res); } else if(alib::XmlDataFactory::first<tree::Tree>(tokens)) { tree::Tree tree = alib::XmlDataFactory::fromTokens<tree::Tree>(tokens); - TreeCastVisitor::doCast(type.getValue(), tree); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Cast", std::chrono::measurements::Type::MAIN); + + tree::Tree res = TreeCastVisitor::doCast(type.getValue(), tree); + + std::chrono::measurements::end(); + std::chrono::measurements::start("Output write", std::chrono::measurements::Type::AUXILARY); + + alib::XmlDataFactory::toStdout(res); } else { throw exception::AlibException(std::string("Invalid cast from to ") + type.getValue()); } + std::chrono::measurements::end(); + std::chrono::measurements::end(); + + if(measure.getValue()) std::clog << std::chrono::measurements::results() << std::endl; + return 0; } catch(const exception::AlibException& exception) { alib::XmlDataFactory::toStdout(exception); diff --git a/acast2/src/cast/AutomatonCastVisitor.cpp b/acast2/src/cast/AutomatonCastVisitor.cpp index 8d54d08306fa82c83e685fe3e42fba68594a32dd..f86ba00e60ac93af8e4154c217621316eb07cfb2 100644 --- a/acast2/src/cast/AutomatonCastVisitor.cpp +++ b/acast2/src/cast/AutomatonCastVisitor.cpp @@ -6,11 +6,13 @@ */ #include "AutomatonCastVisitor.h" + #include "../CastVisitorBase.hpp" -typedef cast_base_helper< automaton::VisitableAutomatonBase::const_visitor_type, automaton::AutomatonBase, alib::AutomatonTypes > AutomatonCastVisitorType; +// fromVisitor, fromType (base), possible from types, toType (wrapper), possible to types +typedef cast_base_helper< automaton::VisitableAutomatonBase::const_visitor_type, automaton::AutomatonBase, alib::AutomatonTypes, automaton::Automaton, alib::AutomatonTypes > AutomatonCastVisitorType; -void AutomatonCastVisitor::doCast(const std::string& name, const automaton::Automaton& orig) { - AutomatonCastVisitorType::do_cast(name, orig.getData()); +automaton::Automaton AutomatonCastVisitor::doCast(const std::string& name, const automaton::Automaton& orig) { + return AutomatonCastVisitorType::do_cast(name, orig.getData()); } diff --git a/acast2/src/cast/AutomatonCastVisitor.h b/acast2/src/cast/AutomatonCastVisitor.h index 334e93709789cbcc005b88e9ad447fcf5e829f5f..7168ca422a4b0abfa4ef3c16e283024051b711fb 100644 --- a/acast2/src/cast/AutomatonCastVisitor.h +++ b/acast2/src/cast/AutomatonCastVisitor.h @@ -9,5 +9,5 @@ class AutomatonCastVisitor { public: - static void doCast(const std::string& name, const automaton::Automaton& automaton); + static automaton::Automaton doCast(const std::string& name, const automaton::Automaton& automaton); }; diff --git a/acast2/src/cast/GrammarCastVisitor.cpp b/acast2/src/cast/GrammarCastVisitor.cpp index af8a333f23bc166b8cf9a3f667f9a4e704f5cb93..295ceb6d7774573d9a4c8f07ee290cd48f8c6a36 100644 --- a/acast2/src/cast/GrammarCastVisitor.cpp +++ b/acast2/src/cast/GrammarCastVisitor.cpp @@ -8,9 +8,9 @@ #include "GrammarCastVisitor.h" #include "../CastVisitorBase.hpp" -typedef cast_base_helper< grammar::VisitableGrammarBase::const_visitor_type, grammar::GrammarBase, alib::GrammarTypes > GrammarCastVisitorType; +typedef cast_base_helper< grammar::VisitableGrammarBase::const_visitor_type, grammar::GrammarBase, alib::GrammarTypes, grammar::Grammar, alib::GrammarTypes > GrammarCastVisitorType; -void GrammarCastVisitor::doCast(const std::string& name, const grammar::Grammar& orig) { - GrammarCastVisitorType::do_cast(name, orig.getData()); +grammar::Grammar GrammarCastVisitor::doCast(const std::string& name, const grammar::Grammar& orig) { + return GrammarCastVisitorType::do_cast(name, orig.getData()); } diff --git a/acast2/src/cast/GrammarCastVisitor.h b/acast2/src/cast/GrammarCastVisitor.h index 106c5d41d316275f5181b2482b6e1a013531daef..72d043ed2f86a7654f6d1f505566d023d6adfe80 100644 --- a/acast2/src/cast/GrammarCastVisitor.h +++ b/acast2/src/cast/GrammarCastVisitor.h @@ -9,5 +9,5 @@ class GrammarCastVisitor { public: - static void doCast(const std::string& name, const grammar::Grammar& grammar); + static grammar::Grammar doCast(const std::string& name, const grammar::Grammar& grammar); }; diff --git a/acast2/src/cast/RegExpCastVisitor.cpp b/acast2/src/cast/RegExpCastVisitor.cpp index bc35062b4cbc73d860806569c9fed5f17f938bfd..262af9adac7635e3b2bbf81bc01d61c168e6064d 100644 --- a/acast2/src/cast/RegExpCastVisitor.cpp +++ b/acast2/src/cast/RegExpCastVisitor.cpp @@ -8,9 +8,9 @@ #include "RegExpCastVisitor.h" #include "../CastVisitorBase.hpp" -typedef cast_base_helper< regexp::VisitableRegExpBase::const_visitor_type, regexp::RegExpBase, alib::RegExpTypes > RegExpCastVisitorType; +typedef cast_base_helper< regexp::VisitableRegExpBase::const_visitor_type, regexp::RegExpBase, alib::RegExpTypes, regexp::RegExp, alib::RegExpTypes > RegExpCastVisitorType; -void RegExpCastVisitor::doCast(const std::string& name, const regexp::RegExp& orig) { - RegExpCastVisitorType::do_cast(name, orig.getData()); +regexp::RegExp RegExpCastVisitor::doCast(const std::string& name, const regexp::RegExp& orig) { + return RegExpCastVisitorType::do_cast(name, orig.getData()); } diff --git a/acast2/src/cast/RegExpCastVisitor.h b/acast2/src/cast/RegExpCastVisitor.h index 5ca7f25c5077dfa28a078846025c21d1420c445c..f815f9a64a5e186def156f68b0d7e17ffd9da76f 100644 --- a/acast2/src/cast/RegExpCastVisitor.h +++ b/acast2/src/cast/RegExpCastVisitor.h @@ -9,5 +9,5 @@ class RegExpCastVisitor { public: - static void doCast(const std::string& name, const regexp::RegExp& regexp); + static regexp::RegExp doCast(const std::string& name, const regexp::RegExp& regexp); }; diff --git a/acast2/src/cast/TreeCastVisitor.cpp b/acast2/src/cast/TreeCastVisitor.cpp index 7440be344e6d506c928b8a8933e19edbe8ae2687..07d59c107339b734db12ece23e4b1b42715df553 100644 --- a/acast2/src/cast/TreeCastVisitor.cpp +++ b/acast2/src/cast/TreeCastVisitor.cpp @@ -8,9 +8,9 @@ #include "TreeCastVisitor.h" #include "../CastVisitorBase.hpp" -typedef cast_base_helper< tree::VisitableTreeBase::const_visitor_type, tree::TreeBase, alib::TreeTypes > TreeCastVisitorType; +typedef cast_base_helper< tree::VisitableTreeBase::const_visitor_type, tree::TreeBase, alib::TreeTypes, tree::Tree, alib::TreeTypes > TreeCastVisitorType; -void TreeCastVisitor::doCast(const std::string& name, const tree::Tree& orig) { - TreeCastVisitorType::do_cast(name, orig.getData()); +tree::Tree TreeCastVisitor::doCast(const std::string& name, const tree::Tree& orig) { + return TreeCastVisitorType::do_cast(name, orig.getData()); } diff --git a/acast2/src/cast/TreeCastVisitor.h b/acast2/src/cast/TreeCastVisitor.h index a9503b96c79e5a82199e37459db73ddac6f07c7d..0266f313e9d39c0e4f036e4ea8128c782432bf1d 100644 --- a/acast2/src/cast/TreeCastVisitor.h +++ b/acast2/src/cast/TreeCastVisitor.h @@ -9,5 +9,5 @@ class TreeCastVisitor { public: - static void doCast(const std::string& name, const tree::Tree& tree); + static tree::Tree doCast(const std::string& name, const tree::Tree& tree); }; diff --git a/acast2/src/cast/TreeToStringCastVisitor.cpp b/acast2/src/cast/TreeToStringCastVisitor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fdb6033b4e09952022c517bacae4d0ecfd10c13 --- /dev/null +++ b/acast2/src/cast/TreeToStringCastVisitor.cpp @@ -0,0 +1,16 @@ +/* + * TreeCastVisitor.cpp + * + * Created on: 24. 2. 2014 + * Author: Jan Travnicek + */ + +#include "TreeToStringCastVisitor.h" +#include "../CastVisitorBase.hpp" + +typedef cast_base_helper< tree::VisitableTreeBase::const_visitor_type, tree::TreeBase, alib::TreeTypes, string::String, alib::StringTypes > TreeToStringCastVisitorType; + +string::String TreeToStringCastVisitor::doCast(const std::string& name, const tree::Tree& orig) { + return TreeToStringCastVisitorType::do_cast(name, orig.getData()); +} + diff --git a/acast2/src/cast/TreeToStringCastVisitor.h b/acast2/src/cast/TreeToStringCastVisitor.h new file mode 100644 index 0000000000000000000000000000000000000000..1e87230f06e661fade53d34306ace2392fe89088 --- /dev/null +++ b/acast2/src/cast/TreeToStringCastVisitor.h @@ -0,0 +1,14 @@ +/* + * TreeCastVisitor.h + * + * Created on: 24. 2. 2014 + * Author: Jan Travnicek + */ +#include <string> +#include <tree/Tree.h> +#include <string/String.h> + +class TreeToStringCastVisitor { +public: + static string::String doCast(const std::string& name, const tree::Tree& tree); +};