Skip to content
Snippets Groups Projects
Commit 084ca5c5 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

feature cast between groups

parent 21e02bca
No related branches found
No related tags found
No related merge requests found
......@@ -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:
 
};
......
......@@ -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);
......
......@@ -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());
}
 
......@@ -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);
};
......@@ -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());
}
 
......@@ -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);
};
......@@ -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());
}
 
......@@ -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);
};
......@@ -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());
}
 
......@@ -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);
};
/*
* 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());
}
/*
* 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);
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment