diff --git a/alib2aux/src/stats/SizeStat.cpp b/alib2aux/src/stats/SizeStat.cpp index 64272f3ad1cdc71b106b42639c5a551832091893..7b84220a5d9de192b660d3a9ad82d88f11a70328 100644 --- a/alib2aux/src/stats/SizeStat.cpp +++ b/alib2aux/src/stats/SizeStat.cpp @@ -6,14 +6,12 @@ */ #include "SizeStat.h" - -#include <container/ObjectsSet.h> - #include <registration/AlgoRegistration.hpp> namespace { -auto SizeStatObjectsSet = registration::AbstractRegister < stats::SizeStat, unsigned, const container::ObjectsSet < > & > ( stats::SizeStat::stat ); -auto SizeStatSetUnsigned = registration::AbstractRegister < stats::SizeStat, unsigned, const std::set < unsigned > & > ( stats::SizeStat::stat ); +auto SizeStatObjectsSet = registration::AbstractRegister < stats::SizeStat, unsigned, const object::AnyObject < ext::set < object::Object > > & > ( stats::SizeStat::stat ); +auto SizeStatSetUnsigned = registration::AbstractRegister < stats::SizeStat, unsigned, const ext::set < unsigned > & > ( stats::SizeStat::stat ); +auto SizeStatObjectsSet2 = registration::AbstractRegister < stats::SizeStat, unsigned, const ext::set < object::Object > & > ( stats::SizeStat::stat ); } diff --git a/alib2aux/src/stats/SizeStat.h b/alib2aux/src/stats/SizeStat.h index a914674c67115b3cb6b79680277d4ebcfca2079b..f7c6b9b74fa9d638aeb9b77d5e7ca2b7eff0fa28 100644 --- a/alib2aux/src/stats/SizeStat.h +++ b/alib2aux/src/stats/SizeStat.h @@ -8,19 +8,30 @@ #ifndef _SIZE_STAT_H_ #define _SIZE_STAT_H_ +#include <alib/set> +#include <object/AnyObject.h> + namespace stats { class SizeStat { public: template < class T > - static unsigned stat ( const T & object ); + static unsigned stat ( const ext::set < T > & object ); + + template < class T > + static unsigned stat ( const object::AnyObject < ext::set < T > > & object ); }; template < class T > -unsigned SizeStat::stat ( const T & object ) { +unsigned SizeStat::stat ( const ext::set < T > & object ) { return object.size ( ); } +template < class T > +unsigned SizeStat::stat ( const object::AnyObject < ext::set < T > > & object ) { + return stat ( object.getData ( ) ); +} + } #endif /* _SIZE_STAT_H_ */ diff --git a/alib2common/src/container/ObjectsSet.cpp b/alib2common/src/container/ObjectsSet.cpp deleted file mode 100644 index e2b7f57e08a22a604203bc87f274e0b8d5c0a383..0000000000000000000000000000000000000000 --- a/alib2common/src/container/ObjectsSet.cpp +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Set.cpp - * - * Created on: Apr 1, 2013 - * Author: Jan Travnicek - */ - -#include "ObjectsSet.h" -#include <registration/ValuePrinterRegistration.hpp> - -namespace { - -static auto valuePrinter = registration::ValuePrinterRegister < container::ObjectsSet < > > ( ); - -} /* namespace */ diff --git a/alib2common/src/container/ObjectsSet.h b/alib2common/src/container/ObjectsSet.h deleted file mode 100644 index 0c4fcc2d815bd10efae1cc575e66ad6d1d52bb2e..0000000000000000000000000000000000000000 --- a/alib2common/src/container/ObjectsSet.h +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Set.h - * - * This file is part of Algorithms library toolkit. - * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) - - * Algorithms library toolkit is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - - * Algorithms library toolkit is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. - * - * Created on: Apr 1, 2013 - * Author: Jan Travnicek - */ - -#ifndef OBJECTS_SET_H_ -#define OBJECTS_SET_H_ - -#include <alib/set> -#include <alib/string> -#include <sstream> - -#include "ContainerBase.h" - -#include <object/UniqueObject.h> -#include <object/ObjectFactory.h> - -#include <core/normalize.hpp> - -namespace container { - -/** - * \brief - * Represents an adaptor of a set container from the stl extensions provided by the algorithms library. - * - * The set is an associative container. - * - * \tparam ElementType the type of values stored in the container. - */ -template < class ElementType = object::Object > -class ObjectsSet final : public ext::set < ElementType >, public ContainerBase { -public: - /** - * \brief - * Creates a new instance of the container based on the extended version of the stl set container. - * - * \param raw the extended set contaier - */ - explicit ObjectsSet ( ext::set < ElementType > raw ); - - /** - * \brief - * Creates a new instance of the contaier based on defaultly construted underlying container. - */ - explicit ObjectsSet ( ); - - /** - * @copydoc alphabet::ContainerBase::clone ( ) const & - */ - virtual ContainerBase * clone ( ) const & override; - - /** - * @copydoc alphabet::ContainerBase::clone ( ) && - */ - virtual ContainerBase * clone ( ) && override; - - /** - * @copydoc base::CommonBase < ObjectBase >::compare ( const ObjectBase & ) const - */ - virtual int compare ( const ObjectBase & other ) const override { - if ( ext::type_index ( typeid ( * this ) ) == ext::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); - - return ext::type_index ( typeid ( * this ) ) - ext::type_index ( typeid ( other ) ); - } - - /** - * The actual compare method - * - * \param other the other instance - * - * \returns the actual relation between two by type same containers - */ - int compare ( const ObjectsSet & other ) const; - - /** - * @copydoc base::CommonBase < ObjectBase >::operator >> ( std::ostream & ) const - */ - virtual void operator >>( std::ostream & os ) const override; - - /** - * @copydoc base::CommonBase < ObjectBase >::operator std::string ( ) const - */ - virtual explicit operator std::string ( ) const override; - - /** - * @copydoc object::ObjectBase::inc ( ) && - */ - virtual object::ObjectBase * inc ( ) && override; -}; - -template < class ElementType > -ObjectsSet < ElementType >::ObjectsSet(ext::set<ElementType> raw) : ext::set<ElementType>(std::move(raw)) { - -} - -template < class ElementType > -ObjectsSet < ElementType >::ObjectsSet() : ext::set<ElementType>() { - -} - -template < class ElementType > -ContainerBase* ObjectsSet < ElementType >::clone ( ) const & { - return new ObjectsSet(*this); -} - -template < class ElementType > -ContainerBase* ObjectsSet < ElementType >::clone() && { - return new ObjectsSet(std::move(*this)); -} - -template < class ElementType > -int ObjectsSet < ElementType >::compare(const ObjectsSet& other) const { - static ext::compare<ext::set<ElementType>> comp; - return comp ( static_cast < const ext::set < ElementType > & > ( * this ), static_cast < const ext::set < ElementType > & > ( other ) ); -} - -template < class ElementType > -void ObjectsSet < ElementType >::operator>>(std::ostream& os) const { - os << "(ObjectsSet " << static_cast < const ext::set < ElementType > & > ( * this ) << ")"; -} - -template < class ElementType > -ObjectsSet < ElementType >::operator std::string() const { - std::stringstream ss; - ss << *this; - return std::move(ss).str(); -} - -template < class ElementType > -object::ObjectBase* ObjectsSet < ElementType >::inc() && { - return new object::UniqueObject(object::Object(std::move(*this)), primitive::Integer(0)); -} - -} /* namespace container */ - -namespace core { - -template < class ElementType > -struct normalize < container::ObjectsSet < ElementType > > { - static ext::set < object::Object > raw ( ext::set < ElementType > && source ) { - ext::set < object::Object > res; - - for ( ElementType && element : ext::make_mover ( source ) ) - res.insert ( object::ObjectFactory::make ( std::move ( element ) ) ); - - return res; - } - - static container::ObjectsSet < > eval ( container::ObjectsSet < ElementType > && value ) { - return container::ObjectsSet < > ( raw ( std::move ( value ) ) ); - } -}; - -} /* namespace core */ - -#endif /* OBJECTS_SET_H_ */ diff --git a/alib2common/test-src/container/ContainerTest.cpp b/alib2common/test-src/container/ContainerTest.cpp index 2ffbb9d5959b685ab8719f3efb6cf5b61e3b16b0..5a9a10439b6d1a28bb045f311de817617fc1cc96 100644 --- a/alib2common/test-src/container/ContainerTest.cpp +++ b/alib2common/test-src/container/ContainerTest.cpp @@ -1,7 +1,6 @@ #include "ContainerTest.h" #include "object/Object.h" -#include "container/ObjectsSet.h" #include "container/ObjectsDeque.h" #include "container/ObjectsVector.h" #include "container/ObjectsMap.h" @@ -19,10 +18,6 @@ void ContainerTest::setUp ( ) { void ContainerTest::tearDown ( ) { } -void ContainerTest::testProperties ( ) { - CPPUNIT_ASSERT ( std::is_nothrow_move_constructible < container::ObjectsSet < object::Object > >::value ); -} - void ContainerTest::testNormalize ( ) { ext::deque < int > dint; ext::deque < object::Object > dnor = core::normalize < container::ObjectsDeque < int > >::raw ( std::move ( dint ) ); diff --git a/alib2common/test-src/container/ContainerTest.h b/alib2common/test-src/container/ContainerTest.h index 0a83808293d89513b756f9915fc6bf6cab64d391..a8ce770b03ddc73d554ade12d7db9db3d1a79803 100644 --- a/alib2common/test-src/container/ContainerTest.h +++ b/alib2common/test-src/container/ContainerTest.h @@ -5,7 +5,6 @@ class ContainerTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE ( ContainerTest ); - CPPUNIT_TEST ( testProperties ); CPPUNIT_TEST ( testNormalize ); CPPUNIT_TEST_SUITE_END ( ); @@ -13,7 +12,6 @@ public: void setUp ( ); void tearDown ( ); - void testProperties ( ); void testNormalize ( ); }; diff --git a/alib2data/src/PrimitiveRegistrator.cpp b/alib2data/src/PrimitiveRegistrator.cpp index 25b0232c34cb9a9606b7a06a8c5049e64ec79a6f..a4b58831675fb9358bbe578a699da1ed7abd95c3 100644 --- a/alib2data/src/PrimitiveRegistrator.cpp +++ b/alib2data/src/PrimitiveRegistrator.cpp @@ -74,7 +74,6 @@ public: core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::pair < object::Object, ext::variant < string::Epsilon < object::Object >, object::Object > > > > ( ); core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::vector < object::Object > > > ( ); core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::vector < ext::variant < object::Object, object::Object > > > > ( ); - core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::set < object::Object > > > ( ); core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < unsigned int > > ( ); core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < common::ranked_symbol < object::Object, primitive::Unsigned > > > ( ); core::xmlApi < object::Object >::template registerXmlWriter < object::AnyObject < ext::pair < unsigned int, unsigned int > > > ( ); diff --git a/alib2data/src/indexes/arbology/CompressedBitParallelTreeIndex.h b/alib2data/src/indexes/arbology/CompressedBitParallelTreeIndex.h index efc5f167885ebe8f957003fd6e9f6e6dbd2c30b8..87f2d14b07aff729af99c54ccfc8a4bbdbe05d21 100644 --- a/alib2data/src/indexes/arbology/CompressedBitParallelTreeIndex.h +++ b/alib2data/src/indexes/arbology/CompressedBitParallelTreeIndex.h @@ -38,7 +38,6 @@ #include <object/UniqueObject.h> #include <object/ObjectBase.h> -#include <container/ObjectsSet.h> #include <container/ObjectsMap.h> #include <container/ObjectsVector.h> #include <common/SparseBoolVector.hpp> diff --git a/alib2data/src/indexes/arbology/NonlinearCompressedBitParallelTreeIndex.h b/alib2data/src/indexes/arbology/NonlinearCompressedBitParallelTreeIndex.h index 01c4cb7d60df74096930d9a425d7ca5c4605909f..d328b07a3e7d83e0723f1bd707d63ebebc444941 100644 --- a/alib2data/src/indexes/arbology/NonlinearCompressedBitParallelTreeIndex.h +++ b/alib2data/src/indexes/arbology/NonlinearCompressedBitParallelTreeIndex.h @@ -37,7 +37,6 @@ #include <object/UniqueObject.h> #include <object/ObjectBase.h> -#include <container/ObjectsSet.h> #include <container/ObjectsMap.h> #include <container/ObjectsVector.h> #include <common/SparseBoolVector.hpp> diff --git a/alib2data/src/indexes/stringology/BitParallelIndex.h b/alib2data/src/indexes/stringology/BitParallelIndex.h index b88f73f36819efdc352d7184b9921ffa7c3301e8..38eb4d4a91f798c93221c799da44c150c1675b03 100644 --- a/alib2data/src/indexes/stringology/BitParallelIndex.h +++ b/alib2data/src/indexes/stringology/BitParallelIndex.h @@ -37,7 +37,6 @@ #include <object/UniqueObject.h> #include <object/ObjectBase.h> -#include <container/ObjectsSet.h> #include <container/ObjectsMap.h> #include <container/ObjectsVector.h> diff --git a/alib2data/src/indexes/stringology/CompressedBitParallelIndex.h b/alib2data/src/indexes/stringology/CompressedBitParallelIndex.h index abec9616c2f3e7b2d380e5a997b0e5509a98f72a..d446c97e0fd56de74ed5fb43513d317f75e62ff8 100644 --- a/alib2data/src/indexes/stringology/CompressedBitParallelIndex.h +++ b/alib2data/src/indexes/stringology/CompressedBitParallelIndex.h @@ -37,7 +37,6 @@ #include <object/UniqueObject.h> #include <object/ObjectBase.h> -#include <container/ObjectsSet.h> #include <container/ObjectsMap.h> #include <common/SparseBoolVector.hpp> diff --git a/alib2data/src/indexes/stringology/SuffixTrie.h b/alib2data/src/indexes/stringology/SuffixTrie.h index 9c7de1c29c99be0e46ee859a97f94cfd0af4029a..e94fd4e2820a1c4f53c66a4983eb418a66634a82 100644 --- a/alib2data/src/indexes/stringology/SuffixTrie.h +++ b/alib2data/src/indexes/stringology/SuffixTrie.h @@ -41,7 +41,6 @@ #include <object/UniqueObject.h> #include <object/ObjectBase.h> -#include <container/ObjectsSet.h> #include <container/ObjectsTrie.h> #include <object/Void.h> diff --git a/alib2data/src/string/LinearString.cpp b/alib2data/src/string/LinearString.cpp index 86f1ecdd864173b3c9896cbd59727a6e552590c4..c164f62c62428b355fd25031de0c971c9daa03f2 100644 --- a/alib2data/src/string/LinearString.cpp +++ b/alib2data/src/string/LinearString.cpp @@ -12,8 +12,6 @@ #include <registration/SetRegistration.hpp> #include <registration/ComponentRegistration.hpp> -#include <container/ObjectsSet.h> - namespace { static auto components = registration::ComponentRegister < string::LinearString < > > ( ); diff --git a/alib2data/test-src/string/StringTest.cpp b/alib2data/test-src/string/StringTest.cpp index 8824ad930b73e5fd0015a399f06ae433b7d6c62f..7a5e31eaf967b5a8a38998af1fd7d7c9a5a115e7 100644 --- a/alib2data/test-src/string/StringTest.cpp +++ b/alib2data/test-src/string/StringTest.cpp @@ -19,7 +19,6 @@ #include <primitive/Character.h> #include <primitive/xml/Character.h> -#include <container/ObjectsSet.h> #include <container/xml/ObjectsSet.h> CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( StringTest, "string" ); @@ -145,7 +144,7 @@ void StringTest::testNormalize3 ( ) { CPPUNIT_ASSERT ( s1x == s2x ); - string::LinearString < object::Object > ref ( ext::vector < object::Object > { object::Object ( container::ObjectsSet < object::Object > ( ext::set < object::Object > { object::Object ( primitive::Character ( 'a' ) ) } ) ), object::Object ( container::ObjectsSet < object::Object > ( ext::set < object::Object > { object::Object ( primitive::Character ( 'b' ) ) } ) ) } ); + string::LinearString < object::Object > ref ( ext::vector < object::Object > { object::Object ( object::AnyObject < ext::set < object::Object > > ( ext::set < object::Object > { object::Object ( primitive::Character ( 'a' ) ) } ) ), object::Object ( object::AnyObject < ext::set < object::Object > > ( ext::set < object::Object > { object::Object ( primitive::Character ( 'b' ) ) } ) ) } ); std::cout << s1x << std::endl; std::cout << ref << std::endl; diff --git a/alib2str/src/container/string/ObjectsSet.cpp b/alib2str/src/container/string/ObjectsSet.cpp index 8b95f241652c2dace6aee35ee9dad9d6b7bb3564..bbb2124ca54a5ab76cf2aaed615a9618bbfef1c1 100644 --- a/alib2str/src/container/string/ObjectsSet.cpp +++ b/alib2str/src/container/string/ObjectsSet.cpp @@ -12,10 +12,10 @@ namespace { -static auto stringWrite = registration::StringWriterRegister < container::ObjectsSet < > > ( ); -static auto stringReaded = registration::StringReaderRegister < object::Object, container::ObjectsSet < > > ( ); +static auto stringWrite = registration::StringWriterRegister < ext::set < object::Object > > ( ); +static auto stringReaded = registration::StringReaderRegister < object::Object, ext::set < object::Object > > ( ); -static auto stringWriteGroup = registration::StringWriterRegisterTypeInGroup < object::Object, container::ObjectsSet < > > ( ); -static auto stringReadedGroup = registration::StringReaderRegisterTypeInGroup < object::Object, container::ObjectsSet < > > ( ); +static auto stringWriteGroup = registration::StringWriterRegisterTypeInGroup < object::Object, ext::set < object::Object > > ( ); +static auto stringReadedGroup = registration::StringReaderRegisterTypeInGroup < object::Object, ext::set < object::Object > > ( ); } /* namespace */ diff --git a/alib2str/src/container/string/ObjectsSet.h b/alib2str/src/container/string/ObjectsSet.h index a5b370e4f9cfbd0b4a3cd4af554914e941453f93..2882ab0d299019424924b084eedff9f3b3eff1b4 100644 --- a/alib2str/src/container/string/ObjectsSet.h +++ b/alib2str/src/container/string/ObjectsSet.h @@ -8,7 +8,7 @@ #ifndef _STRING_OBJECTS_SET_H_ #define _STRING_OBJECTS_SET_H_ -#include <container/ObjectsSet.h> +#include <alib/set> #include <core/stringApi.hpp> #include <container/ContainerFromStringLexer.h> @@ -70,28 +70,6 @@ void stringApi < ext::set < ValueType > >::compose ( std::ostream & output, cons output << '}'; } -template<class ValueType > -struct stringApi < container::ObjectsSet < ValueType > > { - static container::ObjectsSet < ValueType > parse ( std::istream & input ); - static bool first ( std::istream & input ); - static void compose ( std::ostream & output, const container::ObjectsSet < ValueType > & container ); -}; - -template<class ValueType > -container::ObjectsSet < ValueType > stringApi < container::ObjectsSet < ValueType > >::parse ( std::istream & input ) { - return container::ObjectsSet < ValueType > ( stringApi < ext::set < ValueType > >::parse ( input ) ); -} - -template<class ValueType > -bool stringApi < container::ObjectsSet < ValueType > >::first ( std::istream & input ) { - return stringApi < ext::set < ValueType > >::first ( input ); -} - -template<class ValueType > -void stringApi < container::ObjectsSet < ValueType > >::compose ( std::ostream & output, const container::ObjectsSet < ValueType > & container ) { - stringApi < ext::set < ValueType > >::compose ( output, container ); -} - } /* namespace core */ #endif /* _STRING_OBJECTS_SET_H_ */ diff --git a/alib2str/src/core/stringApi.hpp b/alib2str/src/core/stringApi.hpp index fe06be7e69d9a2f941c44c80890496c88e4a52c7..45bf8e733e6059fe96f6023cb9664f239cdbe1b2 100644 --- a/alib2str/src/core/stringApi.hpp +++ b/alib2str/src/core/stringApi.hpp @@ -16,6 +16,7 @@ #include <alib/algorithm> #include <object/Object.h> +#include <object/AnyObject.h> #include "exception/CommonException.h" @@ -40,8 +41,11 @@ private: return res; } + template < class Type, typename Enable = void > + class ReaderRegister; + template < class Type > - class ReaderRegister : public GroupReader { + class ReaderRegister < Type, typename std::enable_if < std::is_base_of < base::CommonBaseBase, Type >::value >::type > : public GroupReader { std::function < Type ( std::istream & ) > parseFunction; public: @@ -56,6 +60,22 @@ private: } }; + template < class Type > + class ReaderRegister < Type, typename std::enable_if < ! std::is_base_of < base::CommonBaseBase, Type >::value >::type > : public GroupReader { + std::function < Type ( std::istream & ) > parseFunction; + + public: + ReaderRegister( ) : parseFunction ( stringApi < Type >::parse ) { + } + + virtual ~ReaderRegister ( ) { + } + + virtual object::Object parse ( std::istream & input ) { + return object::Object ( object::AnyObject < Type > ( parseFunction ( input ) ) ); + } + }; + class GroupWriter { public: virtual void compose ( std::ostream & output, const object::Object & group ) = 0; @@ -70,8 +90,11 @@ private: return res; } + template < class Type, typename Enable = void > + class WriterRegister; + template < class Type > - class WriterRegister : public GroupWriter { + class WriterRegister < Type, typename std::enable_if < std::is_base_of < base::CommonBaseBase, Type >::value >::type > : public GroupWriter { std::function < void ( std::ostream &, const Type & ) > composeFunction; public: @@ -86,13 +109,29 @@ private: } }; + template < class Type > + class WriterRegister < Type, typename std::enable_if < ! std::is_base_of < base::CommonBaseBase, Type >::value >::type > : public GroupWriter { + std::function < void ( std::ostream &, const Type & ) > composeFunction; + + public: + WriterRegister( ) : composeFunction ( stringApi < Type >::compose ) { + } + + virtual ~WriterRegister ( ) { + } + + virtual void compose ( std::ostream & output, const object::Object & group ) { + composeFunction ( output, static_cast < const object::AnyObject < Type > & > ( group.getData ( ) ).getData ( ) ); + } + }; + public: template < class Type > static void registerStringReader ( ) { parseFunctions ( ).push_back ( std::make_pair ( stringApi < Type >::first, std::unique_ptr < GroupReader > ( new ReaderRegister < Type > ( ) ) ) ); } - template < class Type > + template < class Type, typename std::enable_if < std::is_base_of < base::CommonBaseBase, Type >::value >::type * = nullptr > static void registerStringWriter ( ) { bool res = composeFunctions ( ).insert ( std::make_pair ( ext::to_string < Type > ( ), std::unique_ptr < GroupWriter > ( new WriterRegister < Type > ( ) ) ) ).second; if ( ! res ) { @@ -103,6 +142,17 @@ public: } } + template < class Type, typename std::enable_if < ! std::is_base_of < base::CommonBaseBase, Type >::value >::type * = nullptr > + static void registerStringWriter ( ) { + bool res = composeFunctions ( ).insert ( std::make_pair ( ext::to_string < object::AnyObject < Type > > ( ), std::unique_ptr < GroupWriter > ( new WriterRegister < Type > ( ) ) ) ).second; + if ( ! res ) { + std::string groupName = ext::to_string < object::Object > ( ); + std::string typeName = ext::to_string < Type > ( ); + + throw::exception::CommonException ( "Parse callback of " + typeName + " already registered in group " + groupName + "." ); + } + } + static object::Object parse ( std::istream & input ) { auto lambda = [ & ] ( const std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < GroupReader > > & entry ) { diff --git a/alib2str/src/object/string/AnyObject.cpp b/alib2str/src/object/string/AnyObject.cpp index 7d62f1971bb9c2f2bd4fec3b09ee791456f1923d..6716f58e792eabb6c313706da064cd7627655fde 100644 --- a/alib2str/src/object/string/AnyObject.cpp +++ b/alib2str/src/object/string/AnyObject.cpp @@ -9,7 +9,6 @@ #include <object/Object.h> #include <primitive/string/Unsigned.h> #include <container/string/ObjectsPair.h> -#include <container/string/ObjectsSet.h> #include <container/string/ObjectsVector.h> #include <registration/StringRegistration.hpp> @@ -17,7 +16,6 @@ namespace { static auto stringWriteGroup0 = registration::StringWriterRegisterTypeInGroup < object::Object, object::AnyObject < ext::vector < object::Object > > > ( ); -static auto stringWriteGroup1 = registration::StringWriterRegisterTypeInGroup < object::Object, object::AnyObject < ext::set < object::Object > > > ( ); static auto stringWriteGroup2 = registration::StringWriterRegisterTypeInGroup < object::Object, object::AnyObject < ext::pair < object::Object, object::Object > > > ( ); static auto stringWriteGroup3 = registration::StringWriterRegisterTypeInGroup < object::Object, object::AnyObject < unsigned > > ( ); static auto stringWriteGroup4 = registration::StringWriterRegisterTypeInGroup < object::Object, object::AnyObject < ext::pair < unsigned, unsigned > > > ( ); diff --git a/alib2xml/src/container/xml/ObjectsSet.cpp b/alib2xml/src/container/xml/ObjectsSet.cpp index 9ecd0e4dab2b1cb1455fd91af98b09529a0375be..32d045ca1f497fcf1f895383a6992f00a5d8b37f 100644 --- a/alib2xml/src/container/xml/ObjectsSet.cpp +++ b/alib2xml/src/container/xml/ObjectsSet.cpp @@ -11,9 +11,9 @@ namespace { -static auto xmlWrite = registration::XmlWriterRegister < container::ObjectsSet < > > ( ); -static auto xmlRead = registration::XmlReaderRegister < container::ObjectsSet < > > ( ); +static auto xmlWrite = registration::XmlWriterRegister < ext::set < object::Object > > ( ); +static auto xmlRead = registration::XmlReaderRegister < ext::set < object::Object > > ( ); -static auto xmlGroup = registration::XmlRegisterTypeInGroup < object::Object, container::ObjectsSet < > > ( ); +static auto xmlGroup = registration::XmlRegisterTypeInGroup < object::Object, ext::set < object::Object > > ( ); } /* namespace */ diff --git a/alib2xml/src/container/xml/ObjectsSet.h b/alib2xml/src/container/xml/ObjectsSet.h index 8a0475ad49beabafe1bd5ac1255bd3ffe24cbbbf..f9c1db173ffd39aadea7e2406010f692c5cd77ac 100644 --- a/alib2xml/src/container/xml/ObjectsSet.h +++ b/alib2xml/src/container/xml/ObjectsSet.h @@ -8,7 +8,7 @@ #ifndef _XML_OBJECTS_SET_H_ #define _XML_OBJECTS_SET_H_ -#include <container/ObjectsSet.h> +#include <alib/set> #include <core/xmlApi.hpp> namespace core { @@ -56,34 +56,6 @@ void xmlApi < ext::set < T > >::compose ( ext::deque < sax::Token > & output, co output.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT ); } -template < typename T > -struct xmlApi < container::ObjectsSet < T > > { - static container::ObjectsSet < T > parse ( ext::deque < sax::Token >::iterator & input ); - static bool first ( const ext::deque < sax::Token >::const_iterator & input ); - static const std::string & xmlTagName ( ); - static void compose ( ext::deque < sax::Token > & output, const container::ObjectsSet < T > & data ); -}; - -template < typename T > -container::ObjectsSet < T > xmlApi < container::ObjectsSet < T > >::parse ( ext::deque < sax::Token >::iterator & input ) { - return container::ObjectsSet < T > ( xmlApi < ext::set < T > >::parse ( input ) ); -} - -template < typename T > -bool xmlApi < container::ObjectsSet < T > >::first ( const ext::deque < sax::Token >::const_iterator & input ) { - return xmlApi < ext::set < T > >::first ( input ); -} - -template < typename T > -const std::string & xmlApi < container::ObjectsSet < T > >::xmlTagName ( ) { - return xmlApi < ext::set < T > >::xmlTagName ( ); -} - -template < typename T > -void xmlApi < container::ObjectsSet < T > >::compose ( ext::deque < sax::Token > & output, const container::ObjectsSet < T > & input ) { - xmlApi < ext::set < T > >::compose ( output, input ); -} - } /* namespace core */ #endif /* _XML_OBJECTS_SET_H_ */ diff --git a/alib2xml/src/core/xmlApi.hpp b/alib2xml/src/core/xmlApi.hpp index 6959f95087dfc868b90b71b3d877f86ccad0298b..568fc65a6eaf444320cf60025746923bd57962b9 100644 --- a/alib2xml/src/core/xmlApi.hpp +++ b/alib2xml/src/core/xmlApi.hpp @@ -18,6 +18,7 @@ #include "base/CommonBase.hpp" #include "object/Object.h" +#include "object/AnyObject.h" #include <global/GlobalData.h> @@ -76,8 +77,11 @@ private: return res; } + template < class Type, typename Enable = void > + class ParserRegister; + template < class Type > - class ParserRegister : public GroupParser { + class ParserRegister < Type, typename std::enable_if < std::is_base_of < base::CommonBaseBase, Type >::value >::type > : public GroupParser { std::function < Type ( ext::deque < sax::Token >::iterator & ) > parseFunction; public: @@ -93,6 +97,23 @@ private: }; + template < class Type > + class ParserRegister < Type, typename std::enable_if < ! std::is_base_of < base::CommonBaseBase, Type >::value >::type > : public GroupParser { + std::function < Type ( ext::deque < sax::Token >::iterator & ) > parseFunction; + + public: + ParserRegister( ) : parseFunction ( xmlApi < Type >::parse ) { + } + + virtual ~ParserRegister( ) { + } + + virtual object::Object parse ( ext::deque < sax::Token >::iterator & input ) { + return object::Object ( object::AnyObject < Type > ( parseFunction ( input ) ) ); + } + + }; + class GroupComposer { public: virtual void compose ( ext::deque < sax::Token > & input, const object::Object & group ) = 0; @@ -107,8 +128,11 @@ private: return res; } + template < class Type, typename Enable = void > + class ComposerRegister; + template < class Type > - class ComposerRegister : public GroupComposer { + class ComposerRegister < Type, typename std::enable_if < std::is_base_of < base::CommonBaseBase, Type >::value >::type > : public GroupComposer { std::function < void ( ext::deque < sax::Token > &, const Type & ) > composeFunction; public: @@ -124,6 +148,23 @@ private: }; + template < class Type > + class ComposerRegister < Type, typename std::enable_if < ! std::is_base_of < base::CommonBaseBase, Type >::value >::type > : public GroupComposer { + std::function < void ( ext::deque < sax::Token > &, const Type & ) > composeFunction; + + public: + ComposerRegister( ) : composeFunction ( xmlApi < Type >::compose ) { + } + + virtual ~ComposerRegister ( ) { + } + + virtual void compose ( ext::deque < sax::Token > & input, const object::Object & group ) { + composeFunction ( input, static_cast < const object::AnyObject < Type > & > ( group.getData ( ) ).getData ( ) ); + } + + }; + public: template < class Type > static void registerXmlReader ( ) { @@ -136,7 +177,7 @@ public: } } - template < class Type > + template < class Type, typename std::enable_if < std::is_base_of < base::CommonBaseBase, Type >::value >::type * = nullptr > static void registerXmlWriter ( ) { bool res = composeFunctions ( ).insert ( std::make_pair ( ext::to_string < Type > ( ), std::unique_ptr < GroupComposer > ( new ComposerRegister < Type > ( ) ) ) ).second; if ( ! res ) { @@ -147,6 +188,17 @@ public: } } + template < class Type, typename std::enable_if < ! std::is_base_of < base::CommonBaseBase, Type >::value >::type * = nullptr > + static void registerXmlWriter ( ) { + bool res = composeFunctions ( ).insert ( std::make_pair ( ext::to_string < object::AnyObject < Type > > ( ), std::unique_ptr < GroupComposer > ( new ComposerRegister < Type > ( ) ) ) ).second; + if ( ! res ) { + std::string groupName = ext::to_string < object::Object > ( ); + std::string typeName = ext::to_string < Type > ( ); + + throw::exception::CommonException ( "Parse callback of " + typeName + " already registered in group " + groupName + "." ); + } + } + static object::Object parse ( ext::deque < sax::Token >::iterator & data ) { xmlApiInputContext & input = ( xmlApiInputContext & ) data; sax::FromXMLParserHelper::skipAttributes ( input, sax::Token::TokenType::START_ELEMENT ); diff --git a/alib2xml/test-src/container/ContainerTest.cpp b/alib2xml/test-src/container/ContainerTest.cpp index 71e40d76e367340e001c37e64973f2c438c83337..ddc4886b5a9e92df32fbcfe79981840d9accf38f 100644 --- a/alib2xml/test-src/container/ContainerTest.cpp +++ b/alib2xml/test-src/container/ContainerTest.cpp @@ -24,11 +24,11 @@ void ContainerTest::testXMLParser ( ) { object::Object tmp ( primitive::String ( "1" ) ); - container::ObjectsSet < > set; + ext::set < object::Object > set; set.insert ( tmp ); - object::Object object ( set ); + object::Object object { object::AnyObject < ext::set < object::Object > > ( set ) }; { std::string tmp2 = factory::XmlDataFactory::toString ( object );