Panda3D
inputDeviceSet.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file inputDeviceSet.cxx
10  * @author rdb
11  * @date 2015-12-16
12  */
13 
14 #include "inputDeviceSet.h"
15 #include "indent.h"
16 
17 /**
18  *
19  */
20 InputDeviceSet::
21 InputDeviceSet() {
22 }
23 
24 /**
25  *
26  */
27 InputDeviceSet::
28 InputDeviceSet(const InputDeviceSet &copy) :
29  _devices(copy._devices)
30 {
31 }
32 
33 /**
34  *
35  */
36 void InputDeviceSet::
37 operator = (const InputDeviceSet &copy) {
38  _devices = copy._devices;
39 }
40 
41 /**
42  * Adds a new InputDevice to the collection.
43  */
46  _devices.insert(device);
47 }
48 
49 /**
50  * Removes the indicated InputDevice from the collection.
51  * @return true if the device was removed, false if it was not a member.
52  */
55  return _devices.erase(device) > 0;
56 }
57 
58 /**
59  * Adds all the InputDevices indicated in the other collection to this set.
60  */
63  size_t other_num_devices = other.size();
64  for (size_t i = 0; i < other_num_devices; i++) {
65  _devices.push_back(other[i]);
66  }
67  _devices.sort();
68 }
69 
70 /**
71  * Removes from this collection all of the devices listed in the other set.
72  */
75  InputDevices new_devices;
76  InputDevices::const_iterator it;
77  for (it = _devices.begin(); it != _devices.end(); ++it) {
78  if (!other.has_device(*it)) {
79  new_devices.push_back(*it);
80  }
81  }
82  new_devices.sort();
83  _devices.swap(new_devices);
84 }
85 
86 /**
87  * Returns true if the indicated InputDevice appears in this collection, false
88  * otherwise.
89  */
91 has_device(InputDevice *device) const {
92  InputDevices::const_iterator it = _devices.find(device);
93  return (it != _devices.end());
94 }
95 
96 /**
97  * Removes all InputDevices from the collection.
98  */
100 clear() {
101  _devices.clear();
102 }
103 
104 /**
105  * This is a hint to Panda to allocate enough memory to hold the given number
106  * of InputDevices, if you know ahead of time how many you will be adding.
107  */
108 void InputDeviceSet::
109 reserve(size_t num) {
110  _devices.reserve(num);
111 }
112 
113 /**
114  * Writes a brief one-line description of the InputDeviceSet to the indicated
115  * output stream.
116  */
117 void InputDeviceSet::
118 output(std::ostream &out) const {
119  if (_devices.size() == 1) {
120  out << "1 input device";
121  } else {
122  out << _devices.size() << " input devices";
123  }
124 }
125 
126 /**
127  * Writes a complete multi-line description of the InputDeviceSet to the
128  * indicated output stream.
129  */
130 void InputDeviceSet::
131 write(std::ostream &out, int indent_level) const {
132  output(indent(out, indent_level));
133  out << ":\n";
134  indent_level += 2;
135 
136  InputDevices::const_iterator it;
137  for (it = _devices.begin(); it != _devices.end(); ++it) {
138  (*it)->output(indent(out, indent_level));
139  out << '\n';
140  }
141 }
size_type_0 size() const
Returns the number of elements in the ordered vector.
void clear()
Removes all elements from the ordered vector.
iterator_0 begin()
Returns the iterator that marks the first element in the ordered vector.
void write(std::ostream &out, int indent_level=0) const
Writes a complete multi-line description of the InputDeviceSet to the indicated output stream.
bool has_device(InputDevice *device) const
Returns true if the indicated InputDevice appears in this collection, false otherwise.
Manages a list of InputDevice objects, as returned by various InputDeviceManager methods.
void remove_devices_from(const InputDeviceSet &other)
Removes from this collection all of the devices listed in the other set.
iterator_0 end()
Returns the iterator that marks the end of the ordered vector.
void reserve(size_type_0 n)
Informs the vector of a planned change in size; ensures that the capacity of the vector is greater th...
void swap(ordered_vector< Key, Compare, Vector > &other)
Exchanges the contents of this vector and the other vector, in constant time (e.g....
void clear()
Removes all InputDevices from the collection.
bool remove_device(InputDevice *device)
Removes the indicated InputDevice from the collection.
size_t size() const
Returns the number of devices in the collection.
This is a structure representing a single input device.
Definition: inputDevice.h:53
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
void sort()
Maps to sort_unique().
void reserve(size_t num)
This is a hint to Panda to allocate enough memory to hold the given number of InputDevices,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void push_back(const value_type_0 &key)
Adds the new element to the end of the vector without regard for proper sorting.
void add_device(InputDevice *device)
Adds a new InputDevice to the collection.
void add_devices_from(const InputDeviceSet &other)
Adds all the InputDevices indicated in the other collection to this set.
void output(std::ostream &out) const
Writes a brief one-line description of the InputDeviceSet to the indicated output stream.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.