Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6c3182ecc059be411f5c12f0ccb74d907a193da6 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2012 Draeger Medical GmbH (http://www.draeger.com).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Peter Karlitschek (initial contribution)
 *
 *******************************************************************************/

#include "common/messaging/RTObject.h"
#include "etDatatypes.h"
#include <iterator>
#include <string>
#include <vector>

namespace etRuntime {

RTObject::RTObject(IRTObject* parent, const std::string& name) :
		m_name(name),
		m_parent(parent),
		m_children() {

	if (m_parent != 0) {
		m_parent->getChildren().push_back(this);
		m_instancePath = m_parent->getInstancePath() + PATH_DELIM + m_name;
		m_instancePathName = m_parent->getInstancePathName() + PATHNAME_DELIM + m_name;
	} else {
		m_instancePath = PATH_DELIM + m_name;
		m_instancePathName = PATHNAME_DELIM + m_name;
	}
}

void RTObject::destroy() {
	for (std::vector<IRTObject*>::iterator it = m_children.begin(); it != m_children.end(); ++it) {
		RTObject* child = dynamic_cast<RTObject*>(*it);
		if (child != 0) {
			child->destroy();
		}
	}

	// no removal in parent to maintain reference for deletion
	m_parent = 0;
}

IRTObject* RTObject::getRoot() const {
	IRTObject* root = const_cast<RTObject*>(this);
	while (root->getParent() != 0)
		root = root->getParent();

	return root;
}

IRTObject* RTObject::getChild(const std::string& name) const {
	for (std::vector<IRTObject*>::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
		if (name == ((*it)->getName())) {
			return *it;
		}
	}

	return 0;
}

IRTObject* RTObject::getObject(const std::string& path) const {
	etBool isAbsolute = (path[0] == PATH_DELIM);
	if (isAbsolute && getParent() != 0)
		return getParent()->getObject(path);

	std::string segment;
	std::size_t last = 0;
	if (isAbsolute) {
		last = 1;
		size_t first = path.find(PATH_DELIM, last);
		segment = path.substr(last, (first == std::string::npos) ? std::string::npos : first - 1);
		if (segment != m_name)
			return 0;

		last = first;
	}

	IRTObject* current = const_cast<RTObject*>(this);
	std::size_t next;
	while ((next = path.find(PATH_DELIM, last)) != std::string::npos) {
		if (next > last + 1) {
			segment = path.substr(last, next - last);
			current = current->getChild(segment);
			if (current == 0)
				return 0;
		}
		last = next + 1;
	}
	if (last < path.length() - 1) {
		segment = path.substr(last);
		current = current->getChild(segment);
	}

	return current;
}

int RTObject::getThreadForPath(const std::string& path) const {
	if (m_parent != 0)
		return m_parent->getThreadForPath(path);

	return -1;
}

std::string RTObject::toStringRecursive(const std::string& indent) const {
	std::string result(indent + toString() + "\n");

	std::string indentInc("  " + indent);
	std::vector<IRTObject*>::const_iterator it = m_children.begin();
	for (; it != m_children.end(); ++it) {
		RTObject* child = dynamic_cast<RTObject*>(*it);
		if (child != 0)
			result += child->toStringRecursive(indentInc);
	}

	return result;
}

std::string RTObject::toStringRecursive() const {
	return toStringRecursive("");
}

std::string RTObject::toString() const {
	return getName();
}

} /* namespace etRuntime */

Back to the top