fix infinite check bug
[rofl0r-oopoker.git] / util.h
blobf31a512f68b84b0f3228939f1b81d446b75db048
1 /*
2 OOPoker
4 Copyright (c) 2010 Lode Vandevenne
5 All rights reserved.
7 This file is part of OOPoker.
9 OOPoker is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 OOPoker is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with OOPoker. If not, see <http://www.gnu.org/licenses/>.
23 #pragma once
25 #include <sstream>
28 //this can be used to get the correct index of previous and next players compared to you or the dealer
29 template<typename T, typename U>
30 T wrap(T a, U high) //wraps in range [0,high[, high NOT included!
32 if(high == 0) return 0;
34 if(a < 0) a += ((long)((-a) / (T)high) + 1) * ((T)high);
35 if(a >= (T)high) a -= ((long)((a - high) / (T)high) + 1) * ((T)high);
37 return a;
40 //convert any variable to a string
41 //usage: std::string str = valtostr(25454.91654654f);
42 template<typename T>
43 std::string valtostr(const T& val)
45 std::ostringstream sstream;
46 sstream << val;
47 return sstream.str();
50 //convert string to a variable of type T
51 template<typename T>
52 T strtoval(const std::string& s)
54 std::istringstream sstream(s);
55 T val;
56 sstream >> val;
57 return val;
61 getNearestRoundNumber: returns a number near the input, but it'll be a nice round value.
62 The result will always be smaller than or equal to the input. If the input is non-zero, the
63 number will never be zero.
64 It can return values such as 0, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, ...
66 int getNearestRoundNumber(int i);