first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / nodes / packable_tree.h
1 #ifndef PACKABLE_TREE_CLASS
2 #define PACKABLE_TREE_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : packable_tree                                                     *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include "tree.h"
19
20 #include <basis/byte_array.h>
21
22 namespace nodes {
23
24 // forward.
25 class packable_tree_factory;
26
27 //! A tree object that can be packed into an array of bytes and unpacked again.
28
29 class packable_tree : public tree, public virtual basis::packable
30 {
31 public:
32   packable_tree();
33     //!< constructs a new tree with a root and zero branches.
34
35   // the recursive packing methods here will operate on all nodes starting at this one
36   // and moving downwards to all branches.
37
38   int recursive_packed_size() const;
39     //!< spiders the tree starting at this node to calculate the packed size.
40
41   void recursive_pack(basis::byte_array &packed_form) const;
42     //!< packs the whole tree starting at this node into the packed form.
43   
44   static packable_tree *recursive_unpack(basis::byte_array &packed_form,
45            packable_tree_factory &creator);
46     //!< unpacks a tree stored in "packed_form" and returns it.
47     /*!< if NIL is returned, then the unpack failed.  the "creator" is needed
48     for making new derived packable_tree objects of the type stored. */
49
50   // standard pack, unpack and packed_size methods must be implemented by the derived tree.
51
52 private:
53   static void packit(basis::byte_array &packed_form, const packable_tree *current_node);
54     //!< used by our recursive packing methods.
55   static void calcit(int &size_accumulator, const packable_tree *current_node);
56     //!< used by recursive packed size calculator.
57 };
58
59 //////////////
60
61 class packable_tree_factory
62 {
63 public:
64   virtual ~packable_tree_factory();
65   virtual packable_tree *create() = 0;
66     //!< a tree factory is needed when we are recreating the packed tree.
67     /*!< this is because the real type held is always a derived object.
68     this method should just create a blank object of the appropriate type. */
69 };
70
71 } // namespace.
72
73 #endif
74