alib2dummy: put algorithms into namespace, some comments (#112)

parent f41811c1
......@@ -2,5 +2,5 @@
category: library
[Dependencies]
project: alib2xml alib2common alib2abstraction alib2measure alib2std
project: alib2xml alib2common alib2abstraction alib2measure alib2std alib2data alib2algo
system: xml2
/*
* CreateDataType.cpp
*
* Created on: Dec 7, 2017
* Author: Jan Travnicek
*/
#include "DataType.h"
#include <registration/AlgoRegistration.hpp>
#include <alib/iostream>
class CreateDataType {
public:
static DataType create ( ) {
return DataType ( 0 );
}
};
class CreateDataType2 {
public:
template < typename T >
static DataType create ( T a ) {
std::cout << "Here" << std::endl;
return DataType ( a );
}
static DataType create ( int a ) {
return DataType ( a );
}
};
namespace {
auto create1 = registration::AbstractRegister < CreateDataType, DataType > ( CreateDataType::create );
auto create2 = registration::AbstractRegister < CreateDataType2, DataType, int > ( CreateDataType2::create );
auto create3 = registration::AbstractRegister < CreateDataType2, DataType, double > ( CreateDataType2::create );
}
/*
* CreateDataType.cpp
*
* Created on: Dec 7, 2017
* Author: Jan Travnicek
*/
#include "DataType.h"
#include "CreateDataType.h"
#include <registration/AlgoRegistration.hpp>
#include <alib/iostream>
namespace example {
DataType CreateDataType::create ( ) {
return DataType ( 0 );
}
DataType CreateDataType2::create ( int a ) {
return DataType ( a );
}
} /* namespace example */
namespace {
/* registration of an algorithm inside ALT. This is used e.g. in AQL. */
auto create1 = registration::AbstractRegister <
example::CreateDataType, // namespace-qualified name of the algorithm, corresponds to the algorithm class
example::DataType // return value followed by 0 or more parameters
> ( example::CreateDataType::create ); // algorithm entry point, corresponding static function to call
auto create2 = registration::AbstractRegister < example::CreateDataType2, example::DataType, int /* parameters */ > ( example::CreateDataType2::create );
auto create3 = registration::AbstractRegister < example::CreateDataType2, example::DataType, double > ( example::CreateDataType2::create, "val" ).setDocumentation ( "Creates an object of DataType class with specified value\n\n@param val value passed to DataType\n@return DataType object with its value equal to val" );
}
/*
* CreateDataType.h
*
* Created on: Dec 7, 2017
* Author: Jan Travnicek
*/
#ifndef CREATE_DATA_TYPE_H_
#define CREATE_DATA_TYPE_H_
#include "DataType.h"
namespace example {
class CreateDataType {
public:
static DataType create ( );
};
class CreateDataType2 {
public:
template < typename T >
static DataType create ( T a ) {
return DataType ( a );
}
static DataType create ( int a );
};
} /* namespace example */
#endif /* CREATE_DATA_TYPE_H_ */
......@@ -11,6 +11,7 @@
namespace {
auto valuePrinter = registration::ValuePrinterRegister < DataType > ( );
// registration of a ValuePrinter. This "printer" is used for instance in AQL when the instance is returned
auto valuePrinter = registration::ValuePrinterRegister < example::DataType > ( );
}
......@@ -5,8 +5,16 @@
* Author: Jan Travnicek
*/
#ifndef DATATYPE_H_
#define DATATYPE_H_
#include <ostream>
namespace example {
/**
* Example of a simple datatype
*/
class DataType {
int m_a;
......@@ -18,8 +26,12 @@ public:
return m_a;
}
/* For ValuePrinter (see cpp) */
friend std::ostream & operator << ( std::ostream & out, const DataType & type ) {
return out << type.getA ( );
return out << "(DataType " << type.getA ( ) << ")";
}
};
} /* namespace example */
#endif /* DATATYPE_H_ */
......@@ -13,22 +13,22 @@
namespace core {
DataType xmlApi < DataType >::parse ( ext::deque < sax::Token >::iterator & input ) {
example::DataType xmlApi < example::DataType >::parse ( ext::deque < sax::Token >::iterator & input ) {
sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, xmlTagName ( ) );
int value = xmlApi < int >::parse ( input );
sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, xmlTagName ( ) );
return DataType ( value );
return example::DataType ( value );
}
bool xmlApi < DataType >::first ( const ext::deque < sax::Token >::const_iterator & input ) {
bool xmlApi < example::DataType >::first ( const ext::deque < sax::Token >::const_iterator & input ) {
return sax::FromXMLParserHelper::isToken ( input, sax::Token::TokenType::START_ELEMENT, xmlTagName ( ) );
}
std::string xmlApi < DataType >::xmlTagName ( ) {
std::string xmlApi < example::DataType >::xmlTagName ( ) {
return "DataType";
}
void xmlApi < DataType >::compose ( ext::deque < sax::Token > & output, const DataType & data ) {
void xmlApi < example::DataType >::compose ( ext::deque < sax::Token > & output, const example::DataType & data ) {
output.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT);
xmlApi < int >::compose ( output, data.getA ( ) );
output.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT );
......@@ -38,7 +38,8 @@ void xmlApi < DataType >::compose ( ext::deque < sax::Token > & output, const Da
namespace {
auto xmlWrite = registration::XmlWriterRegister < DataType > ( );
auto xmlRead = registration::XmlReaderRegister < DataType > ( );
// registration of XML parser and writer.
auto xmlWrite = registration::XmlWriterRegister < example::DataType > ( );
auto xmlRead = registration::XmlReaderRegister < example::DataType > ( );
} /* namespace */
......@@ -8,17 +8,17 @@
#ifndef _XML_DATA_TYPE_H_
#define _XML_DATA_TYPE_H_
#include <DataType.h>
#include <example/DataType.h>
#include <core/xmlApi.hpp>
namespace core {
template < >
struct xmlApi < DataType > {
static DataType parse ( ext::deque < sax::Token >::iterator & input );
struct xmlApi < example::DataType > {
static example::DataType parse ( ext::deque < sax::Token >::iterator & input );
static bool first ( const ext::deque < sax::Token >::const_iterator & input );
static std::string xmlTagName ( );
static void compose ( ext::deque < sax::Token > & output, const DataType & data );
static void compose ( ext::deque < sax::Token > & output, const example::DataType & data );
};
} /* namespace core */
......
#include <catch2/catch.hpp>
#include "example/CreateDataType.h"
TEST_CASE ( "CreateDataType test", "[unit][dummy][example]" ) {
SECTION ( "no arg version" ) {
CHECK ( example::CreateDataType::create ( ).getA ( ) == 0 );
}
SECTION ( "int version" ) {
CHECK ( example::CreateDataType2::create ( 3 ).getA ( ) == 3 );
}
}
#include <catch2/catch.hpp>
#include <sstream>
#include <factory/XmlDataFactory.hpp>
#include "example/DataType.h"
#include "example/xml/DataType.h"
TEST_CASE ( "DataType test", "[unit][dummy][example]" ) {
SECTION ( "int constructor" ) {
example::DataType d ( 5 );
SECTION ( "holds correct value" ) {
CHECK ( d.getA ( ) == 5 );
}
SECTION ( "ostream operator" ) {
std::ostringstream oss;
oss << d;
CHECK ( oss.str ( ) == "(DataType 5)" );
}
}
SECTION ( "xml" ) {
SECTION ( "xml writer" ) {
example::DataType d ( 5 );
CHECK ( factory::XmlDataFactory::toString ( d ) == "<?xml version=\"1.0\"?>\n<DataType><Integer>5</Integer></DataType>\n" );
}
SECTION ( "xml parser" ) {
example::DataType d = factory::XmlDataFactory::fromString ( "<DataType><Integer>3</Integer></DataType>" );
CHECK ( d.getA ( ) == 3 );
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment