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

allow parsing of variant

parent a1c939e4
No related branches found
No related tags found
No related merge requests found
......@@ -11,8 +11,6 @@
#include <alib/variant>
#include <core/stringApi.hpp>
 
#include <container/ContainerFromStringLexer.h>
namespace core {
 
template < class ... Types >
......@@ -38,16 +36,29 @@ private:
m_out << "void";
}
};
template < class T, class R, class ... Ts >
static ext::variant < Types ... > parse ( std::istream & input ) {
if ( stringApi < T >::first ( input ) )
return ext::variant < Types ... > ( stringApi < T >::parse ( input ) );
else
return parse < R, Ts ... > ( input );
}
template < class T >
static ext::variant < Types ... > parse ( std::istream & input ) {
return ext::variant < Types ... > ( stringApi < T >::parse ( input ) );
}
};
 
template < class ... Types >
ext::variant < Types ... > stringApi < ext::variant < Types ... > >::parse ( std::istream & /* input */ ) {
throw exception::CommonException("parsing BarSymbol from string not implemented");
ext::variant < Types ... > stringApi < ext::variant < Types ... > >::parse ( std::istream & input ) {
return parse < Types ... > ( input );
}
 
template < class ... Types >
bool stringApi < ext::variant < Types ... > >::first ( std::istream & /* input */ ) {
return false;
bool stringApi < ext::variant < Types ... > >::first ( std::istream & input ) {
return ext::andAll ( stringApi < Types >::first ( input ) ... );
}
 
template < class ... Types >
......
#include "ContainerTest.h"
#include "factory/StringDataFactory.hpp"
#include <container/string/ObjectsVariant.h>
#include <primitive/string/Integer.h>
#include <primitive/string/String.h>
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ContainerTest, "automaton" );
CPPUNIT_TEST_SUITE_REGISTRATION( ContainerTest );
void ContainerTest::setUp() {
}
void ContainerTest::tearDown() {
}
void ContainerTest::VariantTest() {
std::string input1 = "1";
ext::variant < int, std::string > variant1 = factory::StringDataFactory::fromString ( input1 );
std::string output1 = factory::StringDataFactory::toString ( variant1 );
std::cout << "\"" << input1 << "\"" << std::endl << std::endl << "\"" << output1 << "\"" << std::endl;
CPPUNIT_ASSERT( input1 == output1 );
ext::variant < int, std::string > variant2 = factory::StringDataFactory::fromString ( output1 );
CPPUNIT_ASSERT( variant1 == variant2 );
CPPUNIT_ASSERT ( variant2.template get < int > ( ) == 1 );
std::string input2 = "aa";
ext::variant < int, std::string > variant3 = factory::StringDataFactory::fromString ( input2 );
CPPUNIT_ASSERT ( variant3.template get < std::string > ( ) == "aa" );
}
#ifndef CONTAINER_TEST_H_
#define CONTAINER_TEST_H_
#include <cppunit/extensions/HelperMacros.h>
class ContainerTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( ContainerTest );
CPPUNIT_TEST( VariantTest );
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void VariantTest();
};
#endif // CONTAINER_TEST_H_
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