File: TargetDescription.cpp

package info (click to toggle)
aseba 1.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,300 kB
  • sloc: cpp: 44,647; ansic: 5,686; python: 1,455; java: 1,136; sh: 393; xml: 202; makefile: 10
file content (78 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download
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
/*
	Aseba - an event-based framework for distributed robot control
	Copyright (C) 2007--2016:
		Stephane Magnenat <stephane at magnenat dot net>
		(http://stephane.magnenat.net)
		and other contributors, see authors.txt for details
	
	This program 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, version 3 of the License.
	
	This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "TargetDescription.h"
#include "../utils/utils.h"

namespace Aseba
{
	//! Compute the XModem CRC of the description, as defined in AS001 at https://aseba.wikidot.com/asebaspecifications
	uint16_t TargetDescription::crc() const
	{
		uint16_t crc(0);
		crc = crcXModem(crc, bytecodeSize);
		crc = crcXModem(crc, variablesSize);
		crc = crcXModem(crc, stackSize);
		for (const auto & namedVariable : namedVariables)
		{
			crc = crcXModem(crc, namedVariable.size);
			crc = crcXModem(crc, namedVariable.name);
		}
		for (const auto & localEvent : localEvents)
		{
			crc = crcXModem(crc, localEvent.name);
		}
		for (const auto & nativeFunction : nativeFunctions)
		{
			crc = crcXModem(crc, nativeFunction.name);
			for (size_t j = 0; j < nativeFunction.parameters.size(); ++j)
			{
				crc = crcXModem(crc, nativeFunction.parameters[j].size);
				crc = crcXModem(crc, nativeFunction.parameters[j].name);
			}
		}
		return crc;
	}
	
	//! Get a VariablesMap out of namedVariables, overwrite freeVariableIndex
	VariablesMap TargetDescription::getVariablesMap(unsigned& freeVariableIndex) const
	{
		freeVariableIndex = 0;
		VariablesMap variablesMap;
		for (const auto & namedVariable : namedVariables)
		{
			variablesMap[namedVariable.name] =
			std::make_pair(freeVariableIndex, namedVariable.size);
			freeVariableIndex += namedVariable.size;
		}
		return variablesMap;
	}
	
	//! Get a FunctionsMap out of nativeFunctions
	FunctionsMap TargetDescription::getFunctionsMap() const
	{
		FunctionsMap functionsMap;
		for (unsigned i = 0; i < nativeFunctions.size(); i++)
		{
			functionsMap[nativeFunctions[i].name] = i;
		}
		return functionsMap;
	}
} // namespace Aseba