gusimplewhiteboard
Clocks.hpp
Go to the documentation of this file.
1/*
2 * Clocks.h
3 * gusimplewhiteboard
4 *
5 * Created by Josh Stover on 02/09/2015.
6 * Copyright © 2015 Josh Stover. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Rene Hexel.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58
59#ifndef Clocks_DEFINED
60#define Clocks_DEFINED
61
62
63#include "wb_clocks.h"
64#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
65#include <sstream>
66#include <ctype.h>
67#endif
68
69namespace guWhiteboard
70{
86 class Clocks: public wb_clocks
87 {
88 public:
90 int64_t get(uint8_t clock_id) const {
91 return ( clock_id<static_cast<uint8_t>(CLOCKS_SIZE) ? clocks(clock_id) : -1 );
92 }
93
95 void set(int clock_id, int64_t value) { set_clocks(value, clock_id); }
96
98 void reset(int clock_id) { set_clocks(get_utime(), clock_id); }
99
101 int64_t time(int clock_id) { return get_utime() - clocks(clock_id); }
102
104 int64_t time_ms(int clock_id) { return (get_utime() - clocks(clock_id))/1000; }
105
106#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
108 Clocks(const std::string &state): wb_clocks() { from_string(state); }
109
111 std::string description() const
112 {
113 std::ostringstream ss;
114 bool first = true;
115 for (unsigned int i=0; i<CLOCKS_SIZE; i++) {
116 if (clocks(i)) {
117 ss<< (first ? "" : ",") << i << "=" << clocks(i) ;
118 first = false;
119 }
120 }
121 return ss.str();
122 }
123
125 void from_string(const std::string &str)
126 {
127 std::vector<std::string> msg;
128 std::vector<std::string> kv;
129 std::vector<std::string>::const_iterator it;
130
131 msg = components_of_string_separated(str, ',', true);
132 for (it = msg.begin(); it!=msg.end(); ++it) {
133 kv = components_of_string_separated(*it, '=', true);
134 if (VALID_KEYVALUE(kv)) {
135 set_clocks(atoll(kv[1].c_str()), atoi(kv[0].c_str()));
136 }
137 }
138 }
139#endif
140 };
141}
142
143#endif /* Clocks_DEFINED */
Class for working with timing and clock messages.
Definition: Clocks.hpp:87
void reset(int clock_id)
reset a clock counter to the current time
Definition: Clocks.hpp:98
int64_t get(uint8_t clock_id) const
clock counter getter
Definition: Clocks.hpp:90
int64_t time_ms(int clock_id)
time since last reset in ms
Definition: Clocks.hpp:104
void set(int clock_id, int64_t value)
clock counter setter
Definition: Clocks.hpp:95
Clocks(const std::string &state)
string constructor
Definition: Clocks.hpp:108
void from_string(const std::string &str)
convert from a string
Definition: Clocks.hpp:125
int64_t time(int clock_id)
get the current time since last reset of a given clock
Definition: Clocks.hpp:101
std::string description() const
convert to a string
Definition: Clocks.hpp:111
/file APM_Interface.h
clocks and timers class
Definition: wb_clocks.h:75
#define CLOCKS_SIZE
clocks available
Definition: wb_clocks.h:65
#define VALID_KEYVALUE(v)
Definition: wb_clocks.h:67