vrpn 07.35
Virtual Reality Peripheral Network
Loading...
Searching...
No Matches
vrpn_EndpointContainer.C
Go to the documentation of this file.
1
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
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
39namespace 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
67 : needsCompact_(false)
68 {
69 VRPN_EC_TRACE("Constructor.");
70 }
71
73 {
74 VRPN_EC_TRACE("Destructor - about to call clear.");
75 clear();
76 }
77
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 {
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 "
97 }
98
100 {
104 }
105
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 "
134 container_.push_back(endpoint);
135 }
136 }
137
138} // namespace vrpn
size_type get_full_container_size() const
Get size of container including NULL elements that haven't been compacted yet.
bool full() const
Can we no longer accommodate a new endpoint?
bool destroy(base_pointer endpoint)
Destroys the contained endpoint by address.
void clear()
Tells each held endpoint in turn to drop the connection then deletes it.
~EndpointContainer()
Destructor - includes a call to clear()
EndpointContainer()
Constructor of empty container.
container_type::size_type size_type
Encapsulation of the data and methods for a single IP-based connection to take care of one part of ma...
Encapsulation of the data and methods for a single generic connection to take care of one part of man...
const int vrpn_MAX_ENDPOINTS
Number of endpoints that a server connection can have. Arbitrary limit.
#define VRPN_EC_TRACE(X)