Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 1 | #ifndef SIGNAL_HANDLER_H_ |
| 2 | #define SIGNAL_HANDLER_H_ |
| 3 | |
| 4 | #include <boost/function.hpp> |
| 5 | #include <map> |
| 6 | |
| 7 | // TODO(aschuh): Template std::map as well. |
| 8 | // Maps the key to the value, inserting it if it isn't there, or replacing it if |
| 9 | // it is. Returns true if the key was added and false if it was replaced. |
| 10 | template <typename K, typename V> |
| 11 | bool InsertIntoMap(std::map<K, V> *my_map, const K &key, const V &new_value) { |
| 12 | std::pair<typename std::map<K, V>::iterator, bool> element; |
| 13 | element = my_map->insert(std::pair<K,V>(key, new_value)); |
| 14 | if (element.second == false) { |
| 15 | element.first->second = new_value; |
| 16 | } |
| 17 | return element.second; |
| 18 | } |
| 19 | |
| 20 | // Gets the value for the key from the map. |
| 21 | // Returns true if the key was found and then populates value with the value. |
| 22 | // Otherwise, leaves value alone and returns false. |
| 23 | template <typename K, typename V> |
| 24 | bool GetFromMap(const std::map<K, V> &my_map, const K &key, V *value) { |
| 25 | typename std::map<K, V>::const_iterator element = my_map.find(key); |
| 26 | if (element != my_map.end()) { |
| 27 | *value = element->second; |
| 28 | return true; |
| 29 | } |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | // Registers a signal handler to be run when we get the specified signal. |
| 35 | // The handler will run in a thread, rather than in the signal's context. |
| 36 | void RegisterSignalHandler(int signal_number, |
| 37 | boost::function<void(int)> function); |
| 38 | |
| 39 | #endif // SIGNAL_HANDLER_H_ |