1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
#ifndef Key_h
#define Key_h
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 2 of the License, or (at your option) any later
* version.
*
* This software 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
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include <QTextStream>
#include <QSet>
#include <algorithm>
//* base namespace
namespace Base
{
class Key
{
public:
//* constructor
explicit Key():
key_( _counter()++ )
{}
//* \brief copy constructor
/**
copy constructor.
The set of associated keys is copied as is.
The new key gets a new unique id. It gets associated to all objects
in the list
*/
explicit Key( const Key& key ):
key_( _counter()++ ),
associatedKeys_( key.associatedKeys_ )
{
for( const auto& associate:associatedKeys_ )
{ associate->_associate( this ); }
}
//* destructor
inline virtual ~Key() = 0;
//* shortcut for key unique id
using Type = quint32;
//* used to find keys of matching id
class SameKeyFTor
{
public:
//* constructor
explicit SameKeyFTor( Type key ):
key_( key )
{}
//* predicate
bool operator() (const Key* key ) const
{ return key->key() == key_; }
private:
//* predicted key
Type key_;
};
//* used to find keys of matching id
class IsAssociatedFTor
{
public:
//* constructor
explicit IsAssociatedFTor( const Key* key ):
key_( key )
{}
//* predicate
bool operator() (const Key* key ) const
{ return key_->isAssociated( key ); }
private:
//* predicted key
const Key* key_;
};
//*@name accessors
//@{
//* retrieve key
Type key() const
{ return key_; }
//* shortcut for set of Key
using Set = QSet< Key* >;
//* retrieve all associated keys
const Set& associatedKeys() const
{ return associatedKeys_; }
//* return true if keys are associated
bool isAssociated( const Key* key ) const
{
return std::find_if(
associatedKeys_.begin(),
associatedKeys_.end(),
SameKeyFTor( key->key() ) ) != associatedKeys_.end();
}
//*@}
//*@name modifiers
//@{
//* clear associations for this key
void clearAssociations()
{
for( const auto& key:associatedKeys_ )
{ key->_disassociate( this ); }
associatedKeys_.clear();
}
//* clear associations of a given type for this key
template<typename T> inline void clearAssociations();
//* remove all associated keys
/** warning: this is non reflexive, unlike clear associations */
void removeAssociatedKeys()
{ associatedKeys_.clear(); }
//* remove all associated keys of a given type
/** warning: this is non reflexive, unlike clear associations */
template<typename T> inline void removeAssociatedKeys();
//* associated two Keys
static void associate( Key* first, Key* second )
{
Q_ASSERT( first->key() != second->key() );
first->_associate( second );
second->_associate( first );
}
//* associated two Keys
static void associate( Key& first, Key& second )
{
if( first.key() != second.key() )
{
first._associate( &second );
second._associate( &first );
}
}
//* disassociate two Keys
static void disassociate( Key* first, Key* second )
{
first->_disassociate( second );
second->_disassociate( first );
}
//* disassociated two Keys
static void disassociate( Key& first, Key& second )
{
first._disassociate( &second );
second._disassociate( &first );
}
//@}
private:
//* add a key to associates
void _associate( Key* key )
{ associatedKeys_.insert( key ); }
//* remove a key from associates
void _disassociate( Key* key )
{ associatedKeys_.remove( key ); }
//* unique id
Type key_;
//* associated keys
Set associatedKeys_;
//* unique id counter
static Type& _counter();
//* to dump Key and associations
friend QTextStream &operator << (QTextStream &out,const Key &key)
{
//* dump this key uid
out << "key=" << key.key();
// dump associated key uid
if( key.associatedKeys_.size() )
{
out << " associations:";
for( const auto& associate:key.associatedKeys_ )
{ out << " " << associate->key(); }
}
out << endl;
return out;
}
};
/** \brief
templatized unsorted set of casted keys.
Is constructed from a pointer to a Key;
Contains all objects of type T associated to the Key.
*/
template<typename T>
class KeySet final
{
private:
QSet<T*> set_;
public:
//* default constructor
explicit KeySet()
{}
/**
\brief constructor
fill the set with all objects of type T associated with the key
*/
explicit KeySet( const Key* key )
{
for( const auto& associate:key->associatedKeys() )
{
auto t( dynamic_cast<T*>( associate ) );
if(t) set_.insert(t);
}
}
/**
\brief constructor
fill the set with all objects of type T associated with the key
*/
explicit KeySet( const Key& key )
{
for( const auto& associate:key.associatedKeys() )
{
auto t( dynamic_cast<T*>( associate ) );
if(t) set_.insert(t);
}
}
//*@name accessors
//@{
using iterator = typename QSet<T*>::iterator;
using const_iterator = typename QSet<T*>::const_iterator;
const_iterator begin() const { return set_.begin(); }
const_iterator end() const { return set_.end(); }
iterator begin() { return set_.begin(); }
iterator end() { return set_.end(); }
const QSet<T*>& get() const { return set_; }
int size() const { return set_.size(); }
bool empty() const { return set_.empty(); }
QList<T*> toList() const { return set_.toList(); }
//@}
//*@name modifiers
//@{
//* mutable accessor
QSet<T*>& get() { return set_; }
//* insert
/** needs decltype because return type ofr set_.insert has changed with qt version */
auto insert( T* t ) -> decltype(set_.insert( t ))
{ return set_.insert( t ); }
//* remove
bool remove( T* t ) { return set_.remove( t ); }
//* find
iterator find( T* t ) { return set_.find( t ); }
//* unite too keysets
KeySet& unite( const KeySet& other )
{
set_.unite( other.set_ );
return *this;
}
//@}
};
template<typename T>
using KeySetIterator = QSetIterator<T*>;
//* equal to operator
inline bool operator == (const Key& first, const Key& second )
{ return first.key() == second.key(); }
//* lower than operator
inline bool operator < (const Key& first, const Key& second )
{ return first.key() < second.key(); }
//______________________________________________________________
Key::~Key() { clearAssociations(); }
//______________________________________________________________
template<typename T> void Key::clearAssociations()
{
for( const auto& key:KeySet<T>(this) )
{
key->_disassociate( this );
_disassociate( key );
}
}
//______________________________________________________________
template<typename T> void Key::removeAssociatedKeys()
{
for( const auto& key:KeySet<T>(this) )
{ _disassociate( key ); }
}
}
//____________________________________________________
inline uint qHash( const Base::Key*& key )
{ return key->key(); }
#endif
|