root/src/Describer.cpp

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
////////////////////////////////////////////////////////////////////////
// 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
////////////////////////////////////////////////////////////////////////




#include "Describer.h"
#include "PointerCollector.h"

namespace nw {

Describer::Describer() {

}
DescribedObject* (*Describer::createObjectForClassId)(ClassId) = NULL;
String Describer::encodingUsed = N0Slib_BinaryToTextEncoding_Default;

    void Describer::setPointers(const PointerCollector& pc) {
        this->unhandledPointers = pc.unhandledPointers;
        this->describePointers();
    }


void Describer::describePointers()
{
        bool hasPointers = false;

        if(this->isInWriteMode())
        {
                if(unhandledPointers.empty())
                        return;
                else
                    hasPointers = true;
        }

        push("N0Slib:Header");

        this->describe("hasPointers", hasPointers);
        if(hasPointers)
        {
                if(this->isInReadMode() && createObjectForClassId == NULL)
                {
                        std::cout << "\n\n";
                        std::cout << "IMPORTANT WARNING: createObjectForClassId not set! Don't know what objects shall be created for the saved classIds\n";
                        std::cout << "Please set a creatObjectForClassId to a function that returns a new SerializedObject* for a given classId\n";
                        std::cout << "The program will probably crash now...\n";
                }

                describeArray("Pointers", "Pointer", unhandledPointers.size());
                for (unsigned int i = 0; enterNextElement(i); ++i)
                {
                  unsigned int id;
                  ClassId classId;
                  if(isInWriteMode())
                  {
                          if(unhandledPointers.at(i) != NULL)
                          {
                                  id = i;
                                  classId = unhandledPointers.at(i)->getClassId();
                          }
                          else
                          {
                                  id = N0Slib_UndefinedPointerId;
                          }
                  }
                  describe("classId", classId);
                  describe("pId",id);
                  //TODO make the <id> tag work for xml, in xml the order is 2 important, or remove it
                  // will be transfered to handled
                  if(!isInWriteMode()) { unhandledPointers.push_back(Describer::createObjectForClassId(classId)); }
                }
        //      if(!isInWriteMode()) { SerializedObject::addSerializedObjectsToBeginning(unhandledPointers); }
                serializePointerData();
        }
        pop();
}

void Describer::serializePointerData()
{
        handledPointers = unhandledPointers;
        unhandledPointers.clear();
        //TODO think about using iterators, compare them with indices
        describeArray("PointerData", "Pointer", handledPointers.size());
          for (unsigned int i = 0; this->enterNextElement(i); ++i)
          {
                  if(handledPointers.at(i) != NULL)
                  {
                          int pid = i;
                          this->describe("pId", pid);
                          handledPointers.at(i)->describeObject(this);
                  }
          }
}

Describer::~Describer() {
        for (unsigned int i = 0; i < states.size(); ++i) {
                delete states.at(i);
        }
}


bool Describer::getBooleanState(String stateName)
{
        for (unsigned int i = 0; i < states.size(); ++i)
        {
                if(states.at(i)->getName() == stateName)
                {
                        return states.at(i)->getBooleanValue();
                }
        }
        return false;
}


void Describer::setState(String stateName, bool value)
{
        Tag* newTag = new Tag(stateName);
        String stringValue;
        std::stringstream ss;
        ss << value;
        ss >> stringValue;
        newTag->setValue(stringValue);
        states.push_back(newTag);
}

void Describer::setState(String stateName, void* value)
{
        Tag* newTag = new Tag(stateName);
        String stringValue;
        std::stringstream ss;
        ss << value;
        ss >> stringValue;
        newTag->setValue(stringValue);
        states.push_back(newTag);
}

void Describer::getState(String stateName, void* pointerToPointer)
{
        void** realPointer = (void**)pointerToPointer;
        for (unsigned int i = 0; i < states.size(); ++i)
        {
                if(states.at(i)->getName() == stateName)
                {
                        std::stringstream ss(states.at(i)->getValue());
                        ss >> *realPointer;
                        return;
                }
        }
}

bool Describer::hasState(String stateName)
{
        for (unsigned int i = 0; i < states.size(); ++i)
        {
                if(states.at(i)->getName() == stateName)
                {
                        return true;
                }
        }
        return false;
}

DescribedObject* Describer::getSerializedObjectForId(unsigned int id)
{
        if(id != N0Slib_UndefinedPointerId && id < handledPointers.size())
                return handledPointers.at(id);
        if(id != N0Slib_UndefinedPointerId)
                std::cout << "Pointer not Found for id " << id << std::endl;
        return NULL;
}

unsigned int Describer::getIDForSerializedObject(Pointer ref)
{
        if(ref == NULL)
                return N0Slib_UndefinedPointerId;

        for (unsigned int i = 0; i < handledPointers.size(); ++i)
        {
                if(handledPointers.at(i) == ref)
                {
                        return i;
                }
        }
        std::cout << "Requested an id for an unkown pointer " << ref << std::endl;
        return N0Slib_UndefinedPointerId; //not knowing, just null it; maybe I should register it
}

bool Describer::pointerIsKnown(DescribedObject* pointer)
{
        unsigned int numberOfCollectedPointers = unhandledPointers.size();
        for (unsigned int i = 0; i < numberOfCollectedPointers; ++i)
        {
                if(unhandledPointers.at(i) == pointer)
                {
                        return true;
                }
        }
        return false;
}

bool Describer::isInReadMode()
{
        return !this->isInWriteMode();
}

bool Describer::isSilentCrawler()
{
        return false;
}

bool Describer::conditionalPush(String tagname, bool condition)
{
        if(this->isInWriteMode())
        {
                if(condition)
                {
                        return push(tagname);
                }
        }
        else
        {
                return push(tagname);
        }
        return false;
}

std::string Describer::getErrorMessage() {
        return errorMessage.str();
}

}  // namespace nw