basic_tree.inl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*******************************************************************************
  2. Tree Container Library: Generic container library to store data in tree-like structures.
  3. Copyright (c) 2006 Mitchel Haas
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the author be held liable for any damages arising from
  6. the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it freely,
  9. subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented;
  11. you must not claim that you wrote the original software.
  12. If you use this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such,
  15. and must not be misrepresented as being the original software.
  16. 3. The above copyright notice and this permission notice may not be removed
  17. or altered from any source distribution.
  18. For complete documentation on this library, see http://www.datasoftsolutions.net
  19. Email questions, comments or suggestions to mhaas@datasoftsolutions.net
  20. *******************************************************************************/
  21. // static member variable definition
  22. template< typename stored_type, typename tree_type, typename container_type >
  23. typename tcl::basic_tree<stored_type, tree_type, container_type>::tClone_fcn
  24. tcl::basic_tree<stored_type, tree_type, container_type>::pClone_fcn = 0;
  25. // constructor
  26. template< typename stored_type, typename tree_type, typename container_type >
  27. tcl::basic_tree<stored_type, tree_type, container_type>::basic_tree(const stored_type& value)
  28. : children(container_type()), pElement(0), pParent_node(0),
  29. stored_type_allocator(std::allocator<stored_type>()), tree_type_allocator(std::allocator<tree_type>())
  30. {
  31. // use clone function if available
  32. if ( pClone_fcn )
  33. pElement = pClone_fcn(value);
  34. else
  35. allocate_stored_type(pElement, value);
  36. }
  37. // copy constructor
  38. template< typename stored_type, typename tree_type, typename container_type >
  39. tcl::basic_tree<stored_type, tree_type, container_type>::basic_tree(const basic_tree_type& rhs)
  40. : children(container_type()), pElement(0), pParent_node(0),
  41. stored_type_allocator(std::allocator<stored_type>()), tree_type_allocator(std::allocator<tree_type>())
  42. {
  43. pParent_node = 0; // new tree obj is always root node
  44. set(*rhs.get()); // set data obj
  45. }
  46. // assignment operator
  47. template< typename stored_type, typename tree_type, typename container_type >
  48. tcl::basic_tree<stored_type, tree_type, container_type>& tcl::basic_tree<stored_type, tree_type, container_type>::operator = (const basic_tree_type& rhs)
  49. {
  50. if ( &rhs == this )
  51. return *this;
  52. set(*rhs.get()); // set data obj
  53. return *this;
  54. }
  55. // destructor
  56. template< typename stored_type, typename tree_type, typename container_type >
  57. tcl::basic_tree<stored_type, tree_type, container_type>::~basic_tree()
  58. {
  59. deallocate_stored_type(pElement);
  60. }
  61. // set(stored_type&)
  62. template< typename stored_type, typename tree_type, typename container_type >
  63. void tcl::basic_tree<stored_type, tree_type, container_type>::set(const stored_type& value)
  64. {
  65. if ( pElement ) // if data node already exists, free memory
  66. deallocate_stored_type(pElement);
  67. if ( pClone_fcn ) // use clone fcn if available
  68. pElement = pClone_fcn(value);
  69. else
  70. allocate_stored_type(pElement, value);
  71. }