/usr/include/elektra/keyset.hpp is in libelektra-dev 0.8.14-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | #ifndef ELEKTRA_KEYSET_HPP
#define ELEKTRA_KEYSET_HPP
#include <string>
#include <key.hpp>
#include <kdb.h>
namespace kdb
{
#ifndef ELEKTRA_WITHOUT_ITERATOR
class KeySetIterator;
class KeySetReverseIterator;
#endif
/**
* @brief Needed to avoid constructor ambiguity
*
* when ... is same type as va_list
*/
struct Va
{
Va(){}
};
const Va va = Va();
/**
* @brief A keyset holds together a set of keys.
*
* @copydoc keyset
*
* \invariant always holds an underlying elektra keyset.
*
* \note that the cursor is mutable,
* so it might be changed even in const functions
* as described.
* */
class KeySet
{
public:
inline KeySet();
inline KeySet(ckdb::KeySet *k);
inline KeySet(const KeySet &other);
inline explicit KeySet(size_t alloc, ...);
inline explicit KeySet(Va va, size_t alloc, va_list ap);
inline ~KeySet ();
ckdb::KeySet * release();
ckdb::KeySet * getKeySet() const;
void setKeySet(ckdb::KeySet * k);
KeySet& operator=(KeySet const& other);
ssize_t size() const;
ckdb::KeySet* dup() const;
void copy(const KeySet &other);
void clear();
ssize_t append(const Key &toAppend);
ssize_t append(const KeySet &toAppend);
Key head() const;
Key tail() const;
void rewind() const;
Key next() const;
Key current() const;
void setCursor(cursor_t cursor) const;
cursor_t getCursor() const;
Key pop();
KeySet cut (Key k);
Key lookup (const Key &k, const option_t options = KDB_O_NONE) const;
Key lookup (std::string const & name, const option_t options = KDB_O_NONE) const;
Key at (cursor_t pos) const;
#ifndef ELEKTRA_WITHOUT_ITERATOR
typedef KeySetIterator iterator;
typedef KeySetIterator const_iterator;
typedef KeySetReverseIterator reverse_iterator;
typedef KeySetReverseIterator const_reverse_iterator;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
#endif //ELEKTRA_WITHOUT_ITERATOR
private:
ckdb::KeySet *ks; ///< holds an elektra keyset
};
#ifndef ELEKTRA_WITHOUT_ITERATOR
/**
* For C++ forward Iteration over KeySets.
* (External Iterator)
* @code
for (Key k:ks3)
{
std::cout << k.getName() << std::endl;
}
* @endcode
*/
class KeySetIterator
{
public:
typedef Key value_type;
typedef cursor_t difference_type;
typedef Key pointer;
typedef Key reference;
typedef std::random_access_iterator_tag iterator_category;
KeySetIterator(KeySet const & k) : ks(k), current() {};
KeySetIterator(KeySet const & k, const cursor_t c) : ks(k), current(c) {};
// conversion to const iterator?
Key get() const { return Key(ckdb::ksAtCursor(ks.getKeySet(), current)); }
Key get(cursor_t pos) const { return Key(ckdb::ksAtCursor(ks.getKeySet(), pos)); }
KeySet const & getKeySet() const { return ks; }
// Forward iterator requirements
reference operator*() const { return get(); }
pointer operator->() const { return get(); }
KeySetIterator& operator++() { ++current; return *this; }
KeySetIterator operator++(int) { return KeySetIterator(ks, current++); }
// Bidirectional iterator requirements
KeySetIterator& operator--() { --current; return *this; }
KeySetIterator operator--(int) { return KeySetIterator(ks, current--); }
// Random access iterator requirements
reference operator[](const difference_type& pos) const { return get(pos); }
KeySetIterator& operator+=(const difference_type& pos) { current += pos; return *this; }
KeySetIterator operator+(const difference_type& pos) const { return KeySetIterator(ks, current + pos); }
KeySetIterator& operator-=(const difference_type& pos) { current -= pos; return *this; }
KeySetIterator operator-(const difference_type& pos) const { return KeySetIterator(ks, current - pos); }
const cursor_t& base() const { return current; }
private:
KeySet const & ks;
cursor_t current;
};
// Forward iterator requirements
inline bool operator==(const KeySetIterator& lhs, const KeySetIterator& rhs)
{ return &lhs.getKeySet() == &rhs.getKeySet() && lhs.base() == rhs.base(); }
inline bool operator!=(const KeySetIterator& lhs, const KeySetIterator& rhs)
{ return &lhs.getKeySet() != &rhs.getKeySet() || lhs.base() != rhs.base() ; }
// Random access iterator requirements
inline bool operator<(const KeySetIterator& lhs, const KeySetIterator& rhs)
{ return lhs.base() < rhs.base(); }
inline bool operator>(const KeySetIterator& lhs, const KeySetIterator& rhs)
{ return lhs.base() > rhs.base(); }
inline bool operator<=(const KeySetIterator& lhs, const KeySetIterator& rhs)
{ return lhs.base() <= rhs.base(); }
inline bool operator>=(const KeySetIterator& lhs, const KeySetIterator& rhs)
{ return lhs.base() >= rhs.base(); }
// DR 685.
inline auto operator-(const KeySetIterator& lhs, const KeySetIterator& rhs)
-> decltype(lhs.base() - rhs.base())
{ return lhs.base() - rhs.base(); }
inline KeySetIterator
operator+(KeySetIterator::difference_type n, const KeySetIterator& i)
{ return KeySetIterator(i.getKeySet(), i.base() + n); }
// some code duplication because std::reverse_iterator
// does not work on value_types
/**
* For C++ reverse Iteration over KeySets.
* (External Iterator)
*/
class KeySetReverseIterator
{
public:
typedef Key value_type;
typedef cursor_t difference_type;
typedef Key pointer;
typedef Key reference;
typedef std::random_access_iterator_tag iterator_category;
KeySetReverseIterator(KeySet const & k) : ks(k), current() {};
KeySetReverseIterator(KeySet const & k, const cursor_t c) : ks(k), current(c) {};
// conversion to const iterator?
Key get() const { return Key(ckdb::ksAtCursor(ks.getKeySet(), current)); }
Key get(cursor_t pos) const { return Key(ckdb::ksAtCursor(ks.getKeySet(), pos)); }
KeySet const & getKeySet() const { return ks;}
// Forward iterator requirements
reference operator*() const { return get(); }
pointer operator->() const { return get(); }
KeySetReverseIterator& operator++() { --current; return *this; }
KeySetReverseIterator operator++(int) { return KeySetReverseIterator(ks, current--); }
// Bidirectional iterator requirements
KeySetReverseIterator& operator--() { ++current; return *this; }
KeySetReverseIterator operator--(int) { return KeySetReverseIterator(ks, current++); }
// Random access iterator requirements
reference operator[](const difference_type& pos) const { return get(ks.size()-pos-1); }
KeySetReverseIterator& operator+=(const difference_type& pos) { current -= pos; return *this; }
KeySetReverseIterator operator+(const difference_type& pos) const { return KeySetReverseIterator(ks, current - pos); }
KeySetReverseIterator& operator-=(const difference_type& pos) { current += pos; return *this; }
KeySetReverseIterator operator-(const difference_type& pos) const { return KeySetReverseIterator(ks, current + pos); }
const cursor_t& base() const { return current; }
private:
KeySet const & ks;
cursor_t current;
};
// Forward iterator requirements
inline bool operator==(const KeySetReverseIterator& lhs, const KeySetReverseIterator& rhs)
{ return &lhs.getKeySet() == &rhs.getKeySet() && lhs.base() == rhs.base(); }
inline bool operator!=(const KeySetReverseIterator& lhs, const KeySetReverseIterator& rhs)
{ return &lhs.getKeySet() != &rhs.getKeySet() || lhs.base() != rhs.base() ; }
// Random access iterator requirements
inline bool operator<(const KeySetReverseIterator& lhs, const KeySetReverseIterator& rhs)
{ return lhs.base() < rhs.base(); }
inline bool operator>(const KeySetReverseIterator& lhs, const KeySetReverseIterator& rhs)
{ return lhs.base() > rhs.base(); }
inline bool operator<=(const KeySetReverseIterator& lhs, const KeySetReverseIterator& rhs)
{ return lhs.base() <= rhs.base(); }
inline bool operator>=(const KeySetReverseIterator& lhs, const KeySetReverseIterator& rhs)
{ return lhs.base() >= rhs.base(); }
// DR 685.
inline auto operator-(const KeySetReverseIterator& lhs, const KeySetReverseIterator& rhs)
-> decltype(lhs.base() - rhs.base())
{ return lhs.base() - rhs.base(); }
inline KeySetReverseIterator
operator+(KeySetReverseIterator::difference_type n, const KeySetReverseIterator& i)
{ return KeySetReverseIterator(i.getKeySet(), i.base() + n); }
inline KeySet::iterator KeySet::begin()
{
return KeySet::iterator(*this, 0);
}
inline KeySet::const_iterator KeySet::begin() const
{
return KeySet::const_iterator(*this, 0);
}
inline KeySet::iterator KeySet::end()
{
return KeySet::iterator(*this, size());
}
inline KeySet::const_iterator KeySet::end() const
{
return KeySet::const_iterator(*this, size());
}
inline KeySet::reverse_iterator KeySet::rbegin()
{
return KeySet::reverse_iterator(*this, size()-1);
}
inline KeySet::const_reverse_iterator KeySet::rbegin() const
{
return KeySet::const_reverse_iterator(*this, size()-1);
}
inline KeySet::reverse_iterator KeySet::rend()
{
return KeySet::reverse_iterator(*this, -1);
}
inline KeySet::const_reverse_iterator KeySet::rend() const
{
return KeySet::const_reverse_iterator(*this, -1);
}
inline KeySet::const_iterator KeySet::cbegin() const noexcept
{
return KeySet::const_iterator(*this, 0);
}
inline KeySet::const_iterator KeySet::cend() const noexcept
{
return KeySet::const_iterator(*this, size());
}
inline KeySet::const_reverse_iterator KeySet::crbegin() const noexcept
{
return KeySet::const_reverse_iterator(*this, size()-1);
}
inline KeySet::const_reverse_iterator KeySet::crend() const noexcept
{
return KeySet::const_reverse_iterator(*this, -1);
}
#endif //ELEKTRA_WITHOUT_ITERATOR
/**
* Creates a new empty keyset with no keys
*
* @copydoc ksNew
*/
inline KeySet::KeySet () :
ks (ckdb::ksNew(0, KS_END))
{}
/**
* Takes ownership of keyset!
*
* Keyset will be destroyed at destructor
* you cant continue to use keyset afterwards!
*
* Use KeySet::release() to avoid destruction.
*
* @param k the keyset to take the ownership from
* @see release()
* @see setKeySet()
*/
inline KeySet::KeySet (ckdb::KeySet *k) :
ks(k)
{}
/**
* Duplicate a keyset.
*
* This keyset will be a duplicate of the other afterwards.
*
* @note that they still reference to the same Keys, so
* if you change key values also the keys in the original keyset
* will be changed.
*
* So it is shallow copy, to create a deep copy you have to dup() every
* key (it still won't copy meta data, but they are COW):
* @snippet cpp_example_dup.cpp ksDeepCopy
*
* @see dup
*/
inline KeySet::KeySet (const KeySet &other)
{
ks = other.dup();
}
/**
* @brief Create a new keyset.
*
* @param alloc minimum number of keys to allocate
* @param ap variable arguments list
*
* Use va as first argument to use this constructor, e.g.:
* @code
* KeySet ks(va, 23, ...);
* @endcode
*
* @copydoc ksVNew
*/
inline KeySet::KeySet (Va, size_t alloc, va_list av)
{
ks = ckdb::ksVNew (alloc, av);
}
/**
* @brief Create a new keyset
*
* @param alloc minimum number of keys to allocate
* @param ... variable argument list
*
* @copydoc ksVNew
*/
inline KeySet::KeySet (size_t alloc, ...)
{
va_list vl;
va_start (vl, alloc);
ks = ckdb::ksVNew (alloc, vl);
va_end (vl);
}
/**
* @brief Deconstruct a keyset
*
* @copydoc ksDel
*/
inline KeySet::~KeySet ()
{
ckdb::ksDel (ks);
}
/**
* If you don't want destruction of keyset at
* the end you can release the pointer.
* */
inline ckdb::KeySet * KeySet::release()
{
ckdb::KeySet *ret = ks;
ks = ckdb::ksNew(0, KS_END);
return ret;
}
/**
* @brief Passes out the raw keyset pointer
*
* @return pointer to internal ckdb KeySet
*
* @see release()
* @see setKeySet()
*/
inline ckdb::KeySet * KeySet::getKeySet () const
{
return ks;
}
/**
* @brief Take ownership of passed keyset
*
* @param k the keyset to take ownership from
* @see release()
* @see getKeySet()
*/
inline void KeySet::setKeySet (ckdb::KeySet * k)
{
ckdb::ksDel(ks);
ks = k;
}
/**
* Duplicate a keyset.
*
* This keyset will be a duplicate of the other afterwards.
*
* @note that they still reference to the same Keys, so
* if you change key values also the keys in the original keyset
* will be changed.
*/
inline KeySet& KeySet::operator=(KeySet const& other)
{
if (this != &other)
{
ckdb::ksDel(ks);
ks = other.dup();
}
return *this;
}
/**
* @brief The size of the keyset
*
* @return the number of keys in the keyset
*/
inline ssize_t KeySet::size () const
{
return ckdb::ksGetSize(ks);
}
/**
* @brief Duplicate a keyset
*
* @return a copy of the keys
*
* This is only a shallow copy. For a deep copy you need to dup every
* key.
*
* @copydoc ksDup()
*/
inline ckdb::KeySet* KeySet::dup () const
{
return ckdb::ksDup(ks);
}
/**
* @brief Copy a keyset
*
* @param other other keyset to copy
*
* This is only a shallow copy. For a deep copy you need to dup every
* key.
*
* @copydoc ksCopy()
*/
inline void KeySet::copy (const KeySet &other)
{
ckdb::ksCopy(ks,other.ks);
}
/**
* @brief Clear the keyset
*
* Keyset will have no keys afterwards.
*/
inline void KeySet::clear ()
{
ckdb::ksCopy(ks,0);
}
/**
* @brief append a key
*
* @param toAppend key to append
*
* @return number of keys in the keyset
*
* @copydoc ksAppendKey()
*/
inline ssize_t KeySet::append (const Key &toAppend)
{
return ckdb::ksAppendKey(ks, toAppend.getKey());
}
/**
* @brief append a keyset
*
* @param toAppend keyset to append
*
* @return number of keys in the keyset
*
* @copydoc ksAppend()
*/
inline ssize_t KeySet::append (KeySet const & toAppend)
{
return ckdb::ksAppend(ks, toAppend.getKeySet());
}
/**
* @return alphabetical first key
*
* @copydoc ksHead()
*/
inline Key KeySet::head() const
{
return Key(ckdb::ksHead(ks));
}
/**
* @return alphabetical last key
*
* @copydoc ksTail()
*/
inline Key KeySet::tail() const
{
return Key(ckdb::ksTail(ks));
}
/**
* @copydoc ksRewind()
*/
inline void KeySet::rewind () const
{
ckdb::ksRewind(ks);
}
/**
* @copydoc ksNext()
*/
inline Key KeySet::next() const
{
ckdb::Key *k = ckdb::ksNext(ks);
return Key(k);
}
/**
* @copydoc ksCurrent()
*/
inline Key KeySet::current() const
{
return Key(ckdb::ksCurrent(ks));
}
/**
* @copydoc ksSetCursor()
*/
inline void KeySet::setCursor(cursor_t cursor) const
{
ckdb::ksSetCursor(ks, cursor);
}
/**
* @copydoc ksGetCursor()
*/
inline cursor_t KeySet::getCursor() const
{
return ckdb::ksGetCursor(ks);
}
/**
* @copydoc ksPop()
*/
inline Key KeySet::pop()
{
ckdb::Key *k = ckdb::ksPop(ks);
Key key(k);
return key;
}
/**
* @copydoc ksCut()
*/
inline KeySet KeySet::cut (Key k)
{
return KeySet(ckdb::ksCut(ks, k.getKey()));
}
/**
* @copydoc ksLookup()
*
* @note That the internal key cursor will point to the found key
*/
inline Key KeySet::lookup (const Key &key, const option_t options) const
{
ckdb::Key *k = ckdb::ksLookup(ks, key.getKey(), options);
return Key(k);
}
/**
* @brief Lookup a key by name
*
* @param name the name to look for
* @param options some options to pass
*
* @return the found key
* @see lookup (const Key &key, const option_t options)
*
* @note That the internal key cursor will point to the found key
*/
inline Key KeySet::lookup (std::string const & name, option_t const options) const
{
ckdb::Key *k = ckdb::ksLookupByName(ks, name.c_str(), options);
return Key(k);
}
/**
* @brief Lookup a key by index
*
* @param pos cursor position
*
* @return the found key
*/
inline Key KeySet::at (cursor_t pos) const
{
if (pos < 0)
pos += size();
return Key(ckdb::ksAtCursor(ks, pos));
}
} // end of namespace kdb
#endif
|