//---------------------------------------------------------------------------- // EgalTech 2015-2015 //---------------------------------------------------------------------------- // File : EGkSelection.h Data : 11.06.15 Versione : 1.6f2 // Contenuto : Strutture e raccolte per indici di selezione, // con funzioni di conversione da e verso stringhe. // // // Modifiche : 11.06.15 DS Creazione modulo. // // //---------------------------------------------------------------------------- #pragma once #include "/EgtDev/Include/EGkGdbConst.h" #include "/EgtDev/Include/EGnStringUtils.h" #include #include //---------------------------------------------------------------------------- // costante per indicare che è selezionato tutto l'oggetto (le parti hanno indice 0-based) const int SEL_SUB_ALL = - 1 ; // Struttura per selezione di oggetto o di sua parte struct SelData { union { struct { int nId ; int nSub ; } ; int v[2] ; } ; SelData( void) : nId( GDB_ID_NULL), nSub( SEL_SUB_ALL) {} SelData( int nI) : nId( nI), nSub( SEL_SUB_ALL) {} SelData( int nI, int nS) : nId( nI), nSub( nS) {} bool operator == ( const SelData& other) const { return ( nId == other.nId && nSub == other.nSub) ; } } ; //---------------------------------------------------------------------------- // Raccolte di SelData typedef std::vector SELVECTOR ; // vettore di SelData typedef std::list SELLIST ; // lista di SelData //---------------------------------------------------------------------------- // Conversione di SelData da e verso stringhe (Id,Sub) inline bool FromString( const std::string& sVal, SelData& Val) { return FromString( sVal, Val.v) ; } inline const std::string ToString( const SelData& Val, int nPrec = 1) { return ToString( Val.v, nPrec) ; } //---------------------------------------------------------------------------- // Conversione di SelData da e verso stringhe utente (Id:Sub oppure Id) inline bool FromUiString( const std::string& sVal, SelData& Val) { std::string sId , sSub ; SplitFirst( sVal, ":", sId, sSub) ; Val = { GDB_ID_NULL, SEL_SUB_ALL} ; FromString( sSub, Val.nSub) ; return FromString( sId, Val.nId) ; } inline const std::string ToUiString( const SelData& Val, int nPrec = 1) { if ( Val.nSub != SEL_SUB_ALL) return ToString( Val.nId, nPrec) + ":" + ToString( Val.nSub, nPrec) ; else return ToString( Val.nId, nPrec) ; } //---------------------------------------------------------------------------- // Conversione di vettori di SelData da e verso stringhe (Id1,Sub1,Id2,Sub2,...) inline bool FromString( const std::string& sVal, SELVECTOR& vVal) { INTVECTOR vI ; if ( ! FromString( sVal, vI) || ( vI.size() % 2) != 0) return false ; vVal.reserve( vI.size() / 2) ; for ( size_t i = 0 ; i < vI.size() ; i += 2) vVal.emplace_back( vI[i], vI[i+1]) ; return true ; } inline const std::string ToString( const SELVECTOR& vVal, int nPrec = 1) { std::string sDest ; sDest.reserve( 2 * 8 * vVal.size()) ; for ( const auto& Val : vVal) sDest += ToString( Val.nId, nPrec) + "," + ToString( Val.nSub, nPrec) + "," ; if ( ! sDest.empty()) sDest.pop_back() ; return sDest ; } //---------------------------------------------------------------------------- // Conversione di vettori di SelData da e verso stringhe utente (Id1:Sub1,Id2,...) inline bool FromUiString( const std::string& sVal, SELVECTOR& vVal) { STRVECTOR vsTokens ; Tokenize( sVal, ",", vsTokens) ; vVal.reserve( vsTokens.size()) ; for ( const auto& sToken : vsTokens) { SelData Val ; if ( FromUiString( sToken, Val)) vVal.emplace_back( Val) ; } return true ; } inline const std::string ToUiString( const SELVECTOR& vVal, int nPrec = 1) { std::string sDest ; sDest.reserve( 2 * 8 * vVal.size()) ; for ( const auto& Val : vVal) sDest += ToUiString( Val, nPrec) + "," ; if ( ! sDest.empty()) sDest.pop_back() ; return sDest ; }