/usr/include/openh323/ptlib_extras.h is in libh323plus-dev 1.24.0~dfsg2-1.3.
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 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | // ptlib_extras.h:
/*
* Ptlib Extras Implementation for the h323plus Library.
*
* Copyright (c) 2011 ISVO (Asia) Pte Ltd. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Alternatively, the contents of this file may be used under the terms
* of the General Public License (the "GNU License"), in which case the
* provisions of GNU License are applicable instead of those
* above. If you wish to allow use of your version of this file only
* under the terms of the GNU License and not to allow others to use
* your version of this file under the MPL, indicate your decision by
* deleting the provisions above and replace them with the notice and
* other provisions required by the GNU License. If you do not delete
* the provisions above, a recipient may use your version of this file
* under either the MPL or the GNU License."
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is derived from and used in conjunction with the
* H323plus Project (www.h323plus.org/)
*
* The Initial Developer of the Original Code is ISVO (Asia) Pte Ltd.
*
*
* Contributor(s): ______________________________________.
*
* $Id: ptlib_extras.h,v 1.12 2011/11/15 09:54:42 willamowius Exp $
*
*/
#ifndef _PTLIB_EXTRAS_H
#define _PTLIB_EXTRAS_H
#include "openh323buildopts.h"
#ifndef H323_STLDICTIONARY
#define H323Dictionary PDictionary
#define H323DICTIONARY PDICTIONARY
#define H323List PList
#define H323LIST PLIST
#else
#include <map>
#include <algorithm>
template <class PAIR>
class deleteDictionaryEntry {
public:
void operator()(const PAIR & p) { delete p.second.second; }
};
template <class E>
inline void deleteDictionaryEntries(const E & e)
{
typedef typename E::value_type PT;
std::for_each(e.begin(), e.end(), deleteDictionaryEntry<PT>());
}
struct PSTLSortOrder {
int operator() ( unsigned p1, unsigned p2 ) const {
return (p1 > p2);
}
};
template <class K, class D> class PSTLDictionary : public PObject,
public std::map< unsigned, std::pair<K, D*>, PSTLSortOrder >
{
public:
PCLASSINFO(PSTLDictionary, PObject);
/**@name Construction */
//@{
/**Create a new, empty, dictionary.
Note that by default, objects placed into the dictionary will be
deleted when removed or when all references to the dictionary are
destroyed.
*/
PSTLDictionary() :disallowDeleteObjects(false) {}
~PSTLDictionary() { RemoveAll(); }
//@}
/**@name Overrides from class PObject */
//@{
/**Make a complete duplicate of the dictionary. Note that all objects in
the array are also cloned, so this will make a complete copy of the
dictionary.
*/
virtual PObject * Clone() const
{ return PNEW PSTLDictionary(*this); }
//@}
/**@name New functions for class */
//@{
/**Get the object contained in the dictionary at the \p key
position. The hash table is used to locate the data quickly via the
hash function provided by the \p key.
The last key/data pair is remembered by the class so that subseqent
access is very fast.
@return
reference to the object indexed by the key.
*/
D & operator[](
const K & key ///< Key to look for in the dictionary.
) const
{ unsigned pos = 0; return *InternalFindKey(key,pos); }
/**Determine if the value of the object is contained in the hash table. The
object values are compared, not the pointers. So the objects in the
collection must correctly implement the <code>PObject::Compare()</code>
function. The hash table is used to locate the entry.
@return
true if the object value is in the dictionary.
*/
PBoolean Contains(
const K & key ///< Key to look for in the dictionary.
) const { unsigned pos = 0; return InternalContains(key,pos); }
/**Remove an object at the specified \p key. The returned pointer is then
removed using the <code>SetAt()</code> function to set that key value to
NULL. If the <code>AllowDeleteObjects</code> option is set then the
object is also deleted.
@return
pointer to the object being removed, or NULL if the key was not
present in the dictionary. If the dictionary is set to delete objects
upon removal, the value -1 is returned if the key existed prior to removal
rather than returning an illegal pointer
*/
virtual D * RemoveAt(
const K & key ///< Key for position in dictionary to get object.
) {
return InternalRemoveKey(key);
}
/**Add a new object to the collection. If the objects value is already in
the dictionary then the object is overrides the previous value. If the
<code>AllowDeleteObjects</code> option is set then the old object is also deleted.
The object is placed in the an ordinal position dependent on the keys
hash function. Subsequent searches use the hash function to speed access
to the data item.
@return
true if the object was successfully added.
*/
virtual PBoolean SetAt(
const K & key, // Key for position in dictionary to add object.
D * obj // New object to put into the dictionary.
) { return InternalAddKey(key, obj); }
/**Get the object at the specified key position. If the key was not in the
collection then NULL is returned.
@return
pointer to object at the specified key.
*/
virtual D * GetAt(
const K & key // Key for position in dictionary to get object.
) { unsigned pos = 0; return (D *)InternalFindKey(key,pos); }
/**Get the key in the dictionary at the ordinal index position.
The ordinal position in the dictionary is determined by the hash values
of the keys and the order of insertion.
The last key/data pair is remembered by the class so that subseqent
access is very fast.
@return
reference to key at the index position.
*/
const K & GetKeyAt(
PINDEX pos ///< Ordinal position in dictionary for key.
) const
{ return InternalGetKeyAt(pos); }
/**Get the data in the dictionary at the ordinal index position.
The ordinal position in the dictionary is determined by the hash values
of the keys and the order of insertion.
The last key/data pair is remembered by the class so that subseqent
access is very fast.
@return
reference to data at the index position.
*/
D & GetDataAt(
PINDEX pos ///< Ordinal position in dictionary for data.
) const
{ return InternalGetAt(pos); }
PINDEX GetSize() const { return this->size(); }
/**Remove all of the elements in the collection. This operates by
continually calling <code>RemoveAt()</code> until there are no objects left.
The objects are removed from the last, at index
(GetSize()-1) toward the first at index zero.
*/
virtual void RemoveAll()
{
PWaitAndSignal m(dictMutex);
if (!disallowDeleteObjects)
deleteDictionaryEntries(*this);
this->clear();
}
PINLINE void AllowDeleteObjects(
PBoolean yes = true ///< New value for flag for deleting objects
) { disallowDeleteObjects = !yes; }
/**Disallow the deletion of the objects contained in the collection. See
the <code>AllowDeleteObjects()</code> function for more details.
*/
void DisallowDeleteObjects() { disallowDeleteObjects = true; }
//@}
typedef struct std::pair<K,D*> DictionaryEntry;
protected:
PBoolean disallowDeleteObjects;
PMutex dictMutex;
D * InternalFindKey(
const K & key, ///< Key to look for in the dictionary.
unsigned & ref ///< Returned index
) const
{
typename std::map< unsigned, std::pair<K, D*>,PSTLSortOrder>::const_iterator i;
for (i = this->begin(); i != this->end(); ++i) {
if (i->second.first == key) {
ref = i->first;
return i->second.second;
}
}
return NULL;
};
D & InternalGetAt(
unsigned ref ///< Returned index
) const
{
PWaitAndSignal m(dictMutex);
PAssert(ref < this->size(), psprintf("Index out of Bounds ref: %u sz: %u",ref,this->size()));
typename std::map< unsigned, std::pair<K, D*>,PSTLSortOrder>::const_iterator i = this->find(ref);
return *(i->second.second);
};
const K & InternalGetKeyAt(
unsigned ref ///< Ordinal position in dictionary for key.
) const
{
PWaitAndSignal m(dictMutex);
PAssert(ref < this->size(), psprintf("Index out of Bounds ref: %u sz: %u",ref,this->size()));
typename std::map< unsigned, std::pair<K, D*>,PSTLSortOrder>::const_iterator i = this->find(ref);
return i->second.first;
}
D * InternalRemoveResort(unsigned pos) {
unsigned newpos = pos;
unsigned sz = this->size();
D * dataPtr = NULL;
typename std::map< unsigned, std::pair<K, D*>, PSTLSortOrder >::iterator it = this->find(pos);
if (it == this->end()) return NULL;
if (disallowDeleteObjects)
dataPtr = it->second.second;
else
delete it->second.second;
this->erase(it);
for (unsigned i = pos+1; i < sz; ++i) {
typename std::map< unsigned, std::pair<K, D*>, PSTLSortOrder >::iterator j = this->find(i);
DictionaryEntry entry = make_pair(j->second.first,j->second.second) ;
this->insert(pair<unsigned, std::pair<K, D*> >(newpos,entry));
newpos++;
this->erase(j);
}
return dataPtr;
};
D * InternalRemoveKey(
const K & key ///< Key to look for in the dictionary.
)
{
PWaitAndSignal m(dictMutex);
unsigned pos = 0;
InternalFindKey(key,pos);
return InternalRemoveResort(pos);
}
PBoolean InternalAddKey(
const K & key, // Key for position in dictionary to add object.
D * obj // New object to put into the dictionary.
)
{
PWaitAndSignal m(dictMutex);
unsigned pos = this->size();
DictionaryEntry entry = make_pair(key,obj);
this->insert(pair<unsigned, std::pair<K, D*> >(pos,entry));
return true;
}
PBoolean InternalContains(
const K & key, ///< Key to look for in the dictionary.
unsigned & ref ///< Returned index
) const
{
return (InternalFindKey(key,ref) != NULL);
};
};
#define PSTLDICTIONARY(cls, K, D) typedef PSTLDictionary<K, D> cls
#define H323Dictionary PSTLDictionary
#define H323DICTIONARY PSTLDICTIONARY
//////////////////////////////////////////////////////////////////////////////////////
template <class PAIR>
class deleteListEntry {
public:
void operator()(const PAIR & p) { delete p.second; }
};
template <class E>
inline void deleteListEntries(const E & e)
{
typedef typename E::value_type PT;
std::for_each(e.begin(), e.end(), deleteListEntry<PT>() );
}
template <class D> class PSTLList : public PObject,
public std::map< unsigned, D* , PSTLSortOrder >
{
public:
PCLASSINFO(PSTLList, PObject);
/**@name Construction */
//@{
/**Create a new, empty, dictionary.
Note that by default, objects placed into the dictionary will be
deleted when removed or when all references to the dictionary are
destroyed.
*/
PSTLList() :disallowDeleteObjects(false) {}
~PSTLList() { RemoveAll(); }
//@}
/**@name Overrides from class PObject */
//@{
/**Make a complete duplicate of the dictionary. Note that all objects in
the array are also cloned, so this will make a complete copy of the
dictionary.
*/
virtual PObject * Clone() const
{ return PNEW PSTLList(*this); }
//@}
virtual PINDEX Append(
D * obj ///< New object to place into the collection.
) { return InternalAddKey(obj); }
/**Insert a new object at the specified ordinal index. If the index is
greater than the number of objects in the collection then the
equivalent of the <code>Append()</code> function is performed.
If not greater it will insert at the ordinal index and shuffle down ordinal values.
@return
index of the newly inserted object.
*/
virtual PINDEX InsertAt(
PINDEX index, ///< Index position in collection to place the object.
D * obj ///< New object to place into the collection.
) { return InternalSetAt(index,obj,false,true); }
/**Remove the object at the specified ordinal index from the collection.
If the AllowDeleteObjects option is set then the object is also deleted.
Note if the index is beyond the size of the collection then the
function will assert.
@return
pointer to the object being removed, or NULL if it was deleted.
*/
virtual D * RemoveAt(
PINDEX index ///< Index position in collection to place the object.
) { return InternalRemoveKey(index); }
PBoolean Remove(
D * obj ///< Index position in collection to place the object.
) { unsigned index=0;
if (!InternalFindIndex(index,obj))
return false;
return (InternalRemoveResort(index) != NULL);
}
/**Set the object at the specified ordinal position to the new value. This
will overwrite the existing entry.
This method will NOT delete the old object independently of the
AllowDeleteObjects option. Use <code>ReplaceAt()</code> instead.
Note if the index is beyond the size of the collection then the
function will assert.
@return
true if the object was successfully added.
*/
virtual PBoolean SetAt(
PINDEX index, ///< Index position in collection to set.
D * obj ///< New value to place into the collection.
) { return InternalSetAt(index,obj); }
/**Set the object at the specified ordinal position to the new value. This
will overwrite the existing entry. If the AllowDeleteObjects option is
set then the old object is also deleted.
Note if the index is beyond the size of the collection then the
function will assert.
@return
true if the object was successfully replaced.
*/
virtual PBoolean ReplaceAt(
PINDEX index, ///< Index position in collection to set.
D * obj ///< New value to place into the collection.
) { return InternalSetAt(index,obj, true); }
/**Get the object at the specified ordinal position. If the index was
greater than the size of the collection then NULL is returned.
The object accessed in this way is remembered by the class and further
access will be fast. Access to elements one either side of that saved
element, and the head and tail of the list, will always be fast.
@return
pointer to object at the specified index.
*/
virtual D * GetAt(
PINDEX index ///< Index position in the collection of the object.
) const { return InternalAt(index); }
D & operator[](PINDEX i) const { return InternalGetAt(i); }
/**Search the collection for the specific instance of the object. The
object pointers are compared, not the values. A simple linear search
from "head" of the list is performed.
@return
ordinal index position of the object, or P_MAX_INDEX.
*/
virtual PINDEX GetObjectsIndex(
const D * obj ///< Object to find.
) const {
unsigned index=0;
if (InternalFindIndex(index,obj))
return index;
else
return P_MAX_INDEX;
}
/**Search the collection for the specified value of the object. The object
values are compared, not the pointers. So the objects in the
collection must correctly implement the <code>PObject::Compare()</code>
function. A simple linear search from "head" of the list is performed.
@return
ordinal index position of the object, or P_MAX_INDEX.
*/
virtual PINDEX GetValuesIndex(
const D & obj ///< Object to find value of.
) const {
unsigned index=0;
if (InternalIndex(index,obj))
return index;
else
return P_MAX_INDEX;
}
//@}
PINDEX GetSize() const { return this->size(); }
PBoolean IsEmpty() const { return (this->size() == 0); }
/**Remove all of the elements in the collection. This operates by
continually calling <code>RemoveAt()</code> until there are no objects left.
The objects are removed from the last, at index
(GetSize()-1) toward the first at index zero.
*/
virtual void RemoveAll()
{
PWaitAndSignal m(dictMutex);
if (IsEmpty()) return;
if (!disallowDeleteObjects)
deleteListEntries(*this);
this->clear();
}
void SetSize(PINDEX i)
{ if (i == 0) RemoveAll(); }
PINLINE void AllowDeleteObjects(
PBoolean yes = true ///< New value for flag for deleting objects
) { disallowDeleteObjects = !yes; }
/**Disallow the deletion of the objects contained in the collection. See
the <code>AllowDeleteObjects()</code> function for more details.
*/
void DisallowDeleteObjects() { disallowDeleteObjects = true; }
//@}
protected:
PBoolean disallowDeleteObjects;
PMutex dictMutex;
PBoolean InternalFindIndex(
unsigned & ref, ///< Returned index
const D * data ///< Data to match
) const
{
PWaitAndSignal m(dictMutex);
typename std::map< unsigned, D* , PSTLSortOrder>::const_iterator i;
for (i = this->begin(); i != this->end(); ++i) {
if (i->second == data) {
ref = i->first;
return true;
}
}
return false;
};
PBoolean InternalIndex(
unsigned & ref, ///< Returned index
const D & data ///< Data to match
) const
{
PWaitAndSignal m(dictMutex);
typename std::map< unsigned, D* , PSTLSortOrder>::const_iterator i;
for (i = this->begin(); i != this->end(); ++i) {
if (*(i->second) == data) {
ref = i->first;
return true;
}
}
return false;
};
D & InternalGetAt(
unsigned ref ///< Returned index
) const
{
PWaitAndSignal m(dictMutex);
PAssert(ref < this->size(), psprintf("Index out of Bounds ref: %u sz: %u",ref,this->size()));
typename std::map< unsigned, D*, PSTLSortOrder>::const_iterator i = this->find(ref);
PAssert(i != this->end() , psprintf("Index not found: %u sz: %u",ref,this->size()));
return *(i->second);
};
D * InternalAt(
unsigned ref ///< Returned index
) const
{
PWaitAndSignal m(dictMutex);
PAssert(ref < this->size(), psprintf("Index out of Bounds ref: %u sz: %u",ref,this->size()));
typename std::map< unsigned, D*, PSTLSortOrder>::const_iterator i = this->find(ref);
return i->second;
};
D * InternalRemoveResort(unsigned pos) {
unsigned newpos = pos;
unsigned sz = this->size();
D * dataPtr = NULL;
typename std::map< unsigned, D*, PSTLSortOrder >::iterator it = this->find(pos);
if (it == this->end()) return NULL;
if (disallowDeleteObjects)
dataPtr = it->second;
else
delete it->second;
this->erase(it);
for (unsigned i = pos+1; i < sz; ++i) {
typename std::map< unsigned, D*, PSTLSortOrder >::iterator j = this->find(i);
PAssert(j != this->end() , psprintf("Index not found: %u sz: %u",i,this->size()));
D* entry = j->second;
this->insert(std::pair<unsigned, D*>(newpos,entry));
newpos++;
this->erase(j);
}
return dataPtr;
};
D * InternalRemoveKey(
PINDEX pos ///< Key to look for in the dictionary.
)
{
PWaitAndSignal m(dictMutex);
return InternalRemoveResort(pos);
}
PINDEX InternalAddKey(
D * obj // New object to put into list.
)
{
PWaitAndSignal m(dictMutex);
unsigned pos = this->size();
this->insert(std::pair<unsigned, D*>(pos,obj));
return pos;
}
PINDEX InternalSetAt(
PINDEX ref, ///< Index position in collection to set.
D * obj, ///< New value to place into the collection.
PBoolean replace = false,
PBoolean reorder = false
)
{
if (ref >= GetSize())
return InternalAddKey(obj);
PWaitAndSignal m(dictMutex);
if (!reorder) {
typename std::map< unsigned, D*, PSTLSortOrder>::iterator it = this->find(ref);
if (replace)
delete it->second;
this->erase(it);
} else {
//for (typename std::map< unsigned, D*, PSTLSortOrder >::iterator r = this->begin(); r != this->end(); ++r) {
// if (*obj == *(r->second)) {
// InternalRemoveResort(r->first);
// break;
// }
//}
int sz = this->size();
if (sz > 0) {
int newpos = sz;
for (int i = sz-1; i >= ref; --i) {
typename std::map< unsigned, D*, PSTLSortOrder >::iterator it = this->find(i);
D* entry = it->second;
this->insert(std::pair<unsigned, D*>(newpos,entry));
this->erase(it);
newpos--;
}
}
}
this->insert(std::pair<unsigned, D*>(ref,obj));
return ref;
}
};
#define PSTLLIST(cls, D) typedef PSTLList<D> cls;
#define H323List PSTLList
#define H323LIST PSTLLIST
#endif // PTLIB_VER < 2110
#endif // _PTLIB_EXTRAS_H
|