vrpn
07.35
Virtual Reality Peripheral Network
Toggle main menu visibility
Loading...
Searching...
No Matches
vrpn_EndpointContainer.C
Go to the documentation of this file.
1
10
11
// Copyright 2015 Sensics, Inc.
12
// Distributed under the Boost Software License, Version 1.0.
13
// (See accompanying file LICENSE_1_0.txt or copy at
14
// http://www.boost.org/LICENSE_1_0.txt)
15
16
// Internal Includes
17
#include "
vrpn_EndpointContainer.h
"
18
#include "
vrpn_Connection.h
"
19
20
// Library/third-party includes
21
// - none
22
23
// Standard includes
24
#include <algorithm>
25
26
#undef VRPN_EC_VERBOSE
27
28
#ifdef VRPN_EC_VERBOSE
29
#include <iostream>
30
#define VRPN_EC_TRACE(X) \
31
do { \
32
std::cerr << " [EC " << this << (needsCompact_ ? "*" : " ") << "] " \
33
<< X << std::endl; \
34
} while (0)
35
#else
36
#define VRPN_EC_TRACE(X) ((void)0)
37
#endif
38
39
namespace
vrpn
{
40
namespace
{
41
template
<
typename
T>
struct
EndpointCloser {
42
public
:
43
void
operator()(T *obj)
44
{
45
if
(obj) {
46
obj->drop_connection();
47
try
{
48
delete
obj;
49
}
catch
(...) {
50
fprintf(stderr,
"EndpointCloser: delete failed\n"
);
51
return
;
52
}
53
}
54
}
55
};
56
}
// namespace
57
61
static
inline
EndpointContainer::pointer
getNullEndpoint()
62
{
63
return
static_cast<
EndpointContainer::pointer
>
(NULL);
64
}
65
66
EndpointContainer::EndpointContainer
()
67
: needsCompact_(false)
68
{
69
VRPN_EC_TRACE
(
"Constructor."
);
70
}
71
72
EndpointContainer::~EndpointContainer
()
73
{
74
VRPN_EC_TRACE
(
"Destructor - about to call clear."
);
75
clear
();
76
}
77
78
void
EndpointContainer::clear
()
79
{
80
if
(container_.empty()) {
81
// Early out if there's nothing to clear.
82
return
;
83
}
84
VRPN_EC_TRACE
(
"Clear."
);
85
::std::for_each(begin_(), end_(), EndpointCloser<T>());
86
container_.clear();
87
}
88
89
void
EndpointContainer::compact_()
90
{
91
size_type
before =
get_full_container_size
();
92
raw_iterator it = std::remove(begin_(), end_(), getNullEndpoint());
93
container_.resize(it - begin_());
94
needsCompact_ =
false
;
95
VRPN_EC_TRACE
(
"Compact complete: was "
<< before <<
", now "
96
<<
get_full_container_size
());
97
}
98
99
bool
EndpointContainer::full
()
const
100
{
103
return
!(
get_full_container_size
() <
vrpn_MAX_ENDPOINTS
- 1);
104
}
105
106
bool
EndpointContainer::destroy
(
EndpointContainer::base_pointer
endpoint)
107
{
108
if
(!endpoint) {
109
return
false
;
110
}
111
raw_iterator it = std::find(begin_(), end_(), endpoint);
112
if
(it != end_()) {
113
needsCompact_ =
true
;
114
VRPN_EC_TRACE
(endpoint <<
" destroyed at location "
115
<< (it - begin_()));
116
try
{
117
delete
*it;
118
}
catch
(...) {
119
fprintf(stderr,
"EndpointContainer::destroy: delete failed\n"
);
120
return
false
;
121
}
122
*it = NULL;
123
return
true
;
124
}
125
return
false
;
126
}
127
128
void
EndpointContainer::acquire_(
EndpointContainer::pointer
endpoint)
129
{
130
if
(NULL != endpoint) {
131
132
VRPN_EC_TRACE
(endpoint <<
" acquired at location "
133
<<
get_full_container_size
());
134
container_.push_back(endpoint);
135
}
136
}
137
138
}
// namespace vrpn
vrpn::EndpointContainer::get_full_container_size
size_type get_full_container_size() const
Get size of container including NULL elements that haven't been compacted yet.
Definition
vrpn_EndpointContainer.h:338
vrpn::EndpointContainer::full
bool full() const
Can we no longer accommodate a new endpoint?
Definition
vrpn_EndpointContainer.C:99
vrpn::EndpointContainer::pointer
T * pointer
Definition
vrpn_EndpointContainer.h:59
vrpn::EndpointContainer::base_pointer
vrpn_Endpoint * base_pointer
Definition
vrpn_EndpointContainer.h:60
vrpn::EndpointContainer::destroy
bool destroy(base_pointer endpoint)
Destroys the contained endpoint by address.
Definition
vrpn_EndpointContainer.C:106
vrpn::EndpointContainer::clear
void clear()
Tells each held endpoint in turn to drop the connection then deletes it.
Definition
vrpn_EndpointContainer.C:78
vrpn::EndpointContainer::~EndpointContainer
~EndpointContainer()
Destructor - includes a call to clear().
Definition
vrpn_EndpointContainer.C:72
vrpn::EndpointContainer::EndpointContainer
EndpointContainer()
Constructor of empty container.
Definition
vrpn_EndpointContainer.C:66
vrpn::EndpointContainer::size_type
container_type::size_type size_type
Definition
vrpn_EndpointContainer.h:66
vrpn
Definition
vrpn_Assert.C:25
vrpn_Connection.h
vrpn_MAX_ENDPOINTS
const int vrpn_MAX_ENDPOINTS
Number of endpoints that a server connection can have. Arbitrary limit.
Definition
vrpn_Connection.h:100
VRPN_EC_TRACE
#define VRPN_EC_TRACE(X)
Definition
vrpn_EndpointContainer.C:36
vrpn_EndpointContainer.h
Header.
vrpn_EndpointContainer.C
Generated by
1.17.0