summaryrefslogtreecommitdiff
path: root/libs/surfaces/websockets/state.cc
blob: 18bf28995144fb7615c75611c027c0a6dda2741a (plain)
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) 2020 Luciano Iam <lucianito@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <boost/unordered_set.hpp>
#include <sstream>

#include "state.h"

NodeState::NodeState () {}

NodeState::NodeState (std::string node)
    : _node (node)
{
}

NodeState::NodeState (std::string node, AddressVector addr, ValueVector val)
    : _node (node)
    , _addr (addr)
    , _val (val)
{
}

std::string
NodeState::debug_str () const
{
	std::stringstream s;
	s << "node = " << _node;

	if (!_addr.empty ()) {
		s << std::endl
		  << " addr = ";

		for (AddressVector::const_iterator it = _addr.begin (); it != _addr.end (); ++it) {
			s << *it << ";";
		}
	}

	for (ValueVector::const_iterator it = _val.begin (); it != _val.end (); ++it) {
		s << std::endl
		  << " val " << it->debug_str ();
	}

	return s.str ();
}

int
NodeState::n_addr () const
{
	return static_cast<int> (_addr.size ());
}

uint32_t
NodeState::nth_addr (int n) const
{
	return _addr[n];
}

void
NodeState::add_addr (uint32_t addr)
{
	_addr.push_back (addr);
}

int
NodeState::n_val () const
{
	return static_cast<int> (_val.size ());
}

TypedValue
NodeState::nth_val (int n) const
{
	if (n_val () < n) {
		return TypedValue ();
	}

	return _val[n];
}

void
NodeState::add_val (TypedValue val)
{
	_val.push_back (val);
}

std::size_t
NodeState::node_addr_hash () const
{
	std::size_t seed = 0;
	boost::hash_combine (seed, _node);
	boost::hash_combine (seed, _addr);
	return seed;
}

bool
NodeState::operator== (const NodeState& other) const
{
	return node_addr_hash () == other.node_addr_hash ();
}

std::size_t
hash_value (const NodeState& state)
{
	return state.node_addr_hash ();
}