From 13b50b064b6b1db137939e36ae135781ef8ed3bf Mon Sep 17 00:00:00 2001 From: Tomas Pecka <tomas.pecka@fit.cvut.cz> Date: Sat, 11 Apr 2020 15:12:37 +0200 Subject: [PATCH] alib2dummy: put algorithms into namespace, some comments (#112) --- alib2dummy/project.conf | 2 +- alib2dummy/src/CreateDataType.cpp | 40 ------------------- alib2dummy/src/example/CreateDataType.cpp | 36 +++++++++++++++++ alib2dummy/src/example/CreateDataType.h | 31 ++++++++++++++ alib2dummy/src/{ => example}/DataType.cpp | 3 +- alib2dummy/src/{ => example}/DataType.h | 16 +++++++- alib2dummy/src/{ => example}/xml/DataType.cpp | 15 +++---- alib2dummy/src/{ => example}/xml/DataType.h | 8 ++-- .../test-src/example/CreateDataTypeTest.cpp | 12 ++++++ alib2dummy/test-src/example/DataTypeTest.cpp | 35 ++++++++++++++++ 10 files changed, 143 insertions(+), 55 deletions(-) delete mode 100644 alib2dummy/src/CreateDataType.cpp create mode 100644 alib2dummy/src/example/CreateDataType.cpp create mode 100644 alib2dummy/src/example/CreateDataType.h rename alib2dummy/src/{ => example}/DataType.cpp (51%) rename alib2dummy/src/{ => example}/DataType.h (55%) rename alib2dummy/src/{ => example}/xml/DataType.cpp (59%) rename alib2dummy/src/{ => example}/xml/DataType.h (70%) create mode 100644 alib2dummy/test-src/example/CreateDataTypeTest.cpp create mode 100644 alib2dummy/test-src/example/DataTypeTest.cpp diff --git a/alib2dummy/project.conf b/alib2dummy/project.conf index 407119daeb..f6e04b8c33 100644 --- a/alib2dummy/project.conf +++ b/alib2dummy/project.conf @@ -2,5 +2,5 @@ category: library [Dependencies] -project: alib2xml alib2common alib2abstraction alib2measure alib2std +project: alib2xml alib2common alib2abstraction alib2measure alib2std alib2data alib2algo system: xml2 diff --git a/alib2dummy/src/CreateDataType.cpp b/alib2dummy/src/CreateDataType.cpp deleted file mode 100644 index 95570da555..0000000000 --- a/alib2dummy/src/CreateDataType.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 ); - -} diff --git a/alib2dummy/src/example/CreateDataType.cpp b/alib2dummy/src/example/CreateDataType.cpp new file mode 100644 index 0000000000..2faef43eae --- /dev/null +++ b/alib2dummy/src/example/CreateDataType.cpp @@ -0,0 +1,36 @@ +/* + * 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" ); + +} diff --git a/alib2dummy/src/example/CreateDataType.h b/alib2dummy/src/example/CreateDataType.h new file mode 100644 index 0000000000..bb0cef89c5 --- /dev/null +++ b/alib2dummy/src/example/CreateDataType.h @@ -0,0 +1,31 @@ +/* + * 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_ */ diff --git a/alib2dummy/src/DataType.cpp b/alib2dummy/src/example/DataType.cpp similarity index 51% rename from alib2dummy/src/DataType.cpp rename to alib2dummy/src/example/DataType.cpp index abb0a78e69..fa030ca4f1 100644 --- a/alib2dummy/src/DataType.cpp +++ b/alib2dummy/src/example/DataType.cpp @@ -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 > ( ); } diff --git a/alib2dummy/src/DataType.h b/alib2dummy/src/example/DataType.h similarity index 55% rename from alib2dummy/src/DataType.h rename to alib2dummy/src/example/DataType.h index 6cd46fa109..04e46004ac 100644 --- a/alib2dummy/src/DataType.h +++ b/alib2dummy/src/example/DataType.h @@ -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_ */ diff --git a/alib2dummy/src/xml/DataType.cpp b/alib2dummy/src/example/xml/DataType.cpp similarity index 59% rename from alib2dummy/src/xml/DataType.cpp rename to alib2dummy/src/example/xml/DataType.cpp index 3b68b82b8f..d3e0d452b3 100644 --- a/alib2dummy/src/xml/DataType.cpp +++ b/alib2dummy/src/example/xml/DataType.cpp @@ -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 */ diff --git a/alib2dummy/src/xml/DataType.h b/alib2dummy/src/example/xml/DataType.h similarity index 70% rename from alib2dummy/src/xml/DataType.h rename to alib2dummy/src/example/xml/DataType.h index e9c5f01fe8..e1383d32a0 100644 --- a/alib2dummy/src/xml/DataType.h +++ b/alib2dummy/src/example/xml/DataType.h @@ -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 */ diff --git a/alib2dummy/test-src/example/CreateDataTypeTest.cpp b/alib2dummy/test-src/example/CreateDataTypeTest.cpp new file mode 100644 index 0000000000..e1c98bfbbb --- /dev/null +++ b/alib2dummy/test-src/example/CreateDataTypeTest.cpp @@ -0,0 +1,12 @@ +#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 ); + } +} diff --git a/alib2dummy/test-src/example/DataTypeTest.cpp b/alib2dummy/test-src/example/DataTypeTest.cpp new file mode 100644 index 0000000000..d72e090d44 --- /dev/null +++ b/alib2dummy/test-src/example/DataTypeTest.cpp @@ -0,0 +1,35 @@ +#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 ); + } + } +} -- GitLab