factor out fequently used functions for ordinal numbers

This commit is contained in:
Fischlurch 2012-12-11 04:07:06 +01:00
parent 08ff817afd
commit 602a04c4b5
3 changed files with 95 additions and 8 deletions

43
src/include/limits.h Normal file
View file

@ -0,0 +1,43 @@
/*
LIMITS.h - hard wired safety limits
Copyright (C) Lumiera.org
2012, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/** @file limits.h
** hard wired safety limits.
** This tiny header defines some hard limits for indexing, counting, searching
** and similar ordinal operations. These limits are a protection against ordinal
** and index numbers insanely out-of dimension, like e.g. a symbolic ID with more
** than 1000 characters. Whenever an actual allocation is based on such ordinal values,
** a tighter and more specific limitation will be enforced on a case by case base
**
** @see symbol-impl.cpp
** @see util::uNum
*/
#ifndef LUMIERA_LIMITS_H
#define LUMIERA_LIMITS_H
#define LUMIERA_IDSTRING_MAX_RELEVANT 1000
#define LUMIERA_MAX_ORDINAL_NUMBER 1000
#endif /*LUMIERA_LIMITS_H*/

View file

@ -35,6 +35,7 @@
#include "lib/symbol.hpp"
#include "include/limits.h"
extern "C" {
#include "lib/safeclib.h"
}
@ -50,7 +51,7 @@ using boost::hash_combine;
namespace lib {
const size_t STRING_MAX_RELEVANT = 1000;
const size_t STRING_MAX_RELEVANT = LUMIERA_IDSTRING_MAX_RELEVANT;
/** equality on Symbol values is defined

View file

@ -24,6 +24,8 @@
#ifndef LIB_UTIL_H
#define LIB_UTIL_H
#include "include/limits.h"
#include <set>
#include <string>
#include <algorithm>
@ -56,6 +58,54 @@ namespace util {
return n1 < n2? N1(n2) : n1;
}
/** cut a numeric value to be >=0 */
template <typename NUM>
inline NUM
noneg (NUM val)
{
return (0<val? val : 0);
}
/** force a numeric to be within bounds, inclusively */
template <typename NUM, typename NB>
inline NUM
limited (NB lowerBound, NUM val, NB upperBound)
{
return min ( max (val, lowerBound)
, upperBound);
}
/** positive integral number from textual representation
* @return always a number, 0 in case of unparseable text,
* limited to 0 <= num <= LUMIERA_MAX_ORDINAL_NUMBER */
inline uint
uNum (const char* pCStr)
{
if (!pCStr) return 0;
int parsedNumber(std::atoi (pCStr));
return limited (0, parsedNumber, LUMIERA_MAX_ORDINAL_NUMBER);
}
inline int
sNum (const char* pCStr)
{
if (!pCStr) return 0;
int parsedNumber(std::atoi (pCStr));
return limited (-LUMIERA_MAX_ORDINAL_NUMBER, parsedNumber, LUMIERA_MAX_ORDINAL_NUMBER);
}
inline uint
uNum (string const& spec)
{
return uNum (spec.c_str());
}
inline int
sNum (string const& spec)
{
return sNum (spec.c_str());
}
/* ======== generic empty check ========= */
@ -90,13 +140,6 @@ namespace util {
}
/** cut a numeric value to be >=0 */
template <typename NUM>
inline NUM
noneg (NUM val)
{
return (0<val? val : 0);
}
/** shortcut for containment test on a map */
template <typename MAP>