root/src/PointerCollector.h

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
////////////////////////////////////////////////////////////////////////
// Copyright (c) Nehmulos 2011-2014
// This file is part of N0 Strain Serialization Library.
//
// N0Strain-Serialization-Library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// N0Strain-Serialization-Library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with N0Strain-Serialization-Library.  If not, see https://gnu.org/licenses/lgpl-3.0
////////////////////////////////////////////////////////////////////////




#ifndef POINTERCOLLECTOR_H_
#define POINTERCOLLECTOR_H_

#include "Describer.h"
#include "DescribedObject.h"
#include "N0SlibConstants.h"
#include <stack>

namespace nw {

/// Used for the PointerCollector constructor
namespace HandlePointers {
        enum HandlePointers {
            all,
            none,
            selected
        };
}  // namespace HandlePointers

struct N0PcArray
{
        unsigned int size;
        unsigned int actualSize;
        unsigned int numberOfFoundPointersBeforeSearch;
};

/// Collects pointers that are serialized in the serialize Function of an SerializedObject
extern void collectPointers(DescribedObject* pointer);


/////////////////////////////////////////////////////////////////////////////////////////////////////
/// This class runs through the serialize() function of a SerializedObject,
/// but it doesn't write or read anything, it just grabs all pointers from
/// serialize(SerializeObject*) and adds them to the static unhandeldPointers vector
/////////////////////////////////////////////////////////////////////////////////////////////////////

class PointerCollector : public Describer
{
public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        /// The Constructor searches for pointers in the given pointer to add them to Serializer::unhandledPointers.
        /// The given pointer will be added. If the given pointer is null the constructor does nothing.
        /// PointerCollector calls the serialize function of the given pointer with this as Serializer.
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        PointerCollector(DescribedObject* pointer);

        /// PointerCollector does not save anything in it's instance, so there's nothing to remove for it.
        virtual ~PointerCollector();

        void close();                                           ///< Does nothing
        void readFromFile(const String filename);       ///< Does nothing

        bool isInWriteMode();                           ///< Returns true, as this Class is required before writing
        bool isSilentCrawler();                         ///< Returns true, this was written for pointercollector

        /// Return ture to make sure it's entered.
        bool conditionalPush(const String tagname, bool& condition);

        void describeArray(const String arrayName, const String elementsName, unsigned int size);       ///< saves the arraySize
        void describeValueArray(const String arrayName, unsigned int size);
        bool enterNextElement(unsigned int iterator);                                                                   ///< see skipOpenArrays

        /* Serialize functions */
        void describeName(const String name);
        void describeValue(bool&);                              ///< Does nothing
        void describeValue(char&);                              ///< Does nothing
        void describeValue(unsigned char&);     ///< Does nothing
        void describeValue(signed char&);               ///< Does nothing
        void describeValue(short&);                     ///< Does nothing
        void describeValue(unsigned short&);    ///< Does nothing
        void describeValue(int&);                               ///< Does nothing
        void describeValue(unsigned int&);              ///< Does nothing
        void describeValue(long&);                              ///< Does nothing
        void describeValue(unsigned long&);     ///< Does nothing
        void describeValue(float&);                     ///< Does nothing
        void describeValue(Pointer);                    ///< adds the pointer to unhandledPointers
        void describeValue(double&);                    ///< Does nothing
        void describeValue(long long&);         ///< Does nothing
        void describeValue(long double&);               ///< Does nothing
        void describeValue(String&);                    ///< Does nothing
        void describeBlob(const String childName,nw::Pointer binaryBlob, unsigned int blobSize); ///< Does nothing
        void comment(const String text);                                                        ///< Does nothing
        String getParentName();                                                         ///< Does nothing
        void pop();                                                                             ///< Does nothing
        bool push(const String tagName);                                                        ///< Does nothing. Returns always true.

        /////////////////////////////////////////////////////////////////////////////////////////////////////
        /// If it's true enterNextElement will return false if no pointers were found during the first loop.
        /// as we just use this class to crawl through the serialize function to search for pointers,
        ///  we can skip arrays that don't contain pointers
        /// If this is a problem, you can set skipEmptyArrays to false;
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        static bool skipsEmptyArrays;
private:
        std::stack<N0PcArray> openArrays;
};

}  // namespace nw
#endif /* POINTERCOLLECTOR_H_ */