![]() |
| | #1 |
| Registered User Join Date: May 2006
Posts: 894
| Ranged numbers PS: I know it may be a little bit of overhead but I think it cleans up a little of the mess in your code since you don't have to check boundaries and such. Code: #ifndef RANGED_NUM_H_INCLUDED
#define RANGED_NUM_H_INCLUDED
#define READJUST_VALUE(a, min, max) if(a < min) a = min; if(a > max) a = max
template <typename T>
class RangedNum {
public:
explicit RangedNum(T dat = 0, T min = 0, T max = 0) : Data(dat), Min(min), Max(max)
{
}
explicit RangedNum(const RangedNum<T>& cpy)
{
Data = cpy.Data;
Min = cpy.Min;
Max = cpy.Max;
}
void SetMinimum(T min = 0)
{
Min = min;
if(Min > Max) Min = Max;
READJUST_VALUE(Data, Min, Max);
}
void SetMaximum(T max = 0)
{
Max = max;
if(Max < Min) Max = Min;
READJUST_VALUE(Data, Min, Max);
}
void SetRanges(T min, T max)
{
Min = min;
Max = max;
if(Min > Max) Min = Max;
READJUST_VALUE(Data, Min, Max);
}
operator T ()
{
return Data;
}
template <typename OpT>
T& operator + (const OpT& arg)
{
Tmp = Data + Arg;
READJUST_VALUE(Tmp, Min, Max);
return Tmp;
}
template <typename OpT>
T& operator += (const OpT& arg)
{
Data += arg;
READJUST_VALUE(Data, Min, Max);
return Data;
}
template <typename OpT>
T& operator - (const OpT& arg)
{
Tmp = Data - Arg;
READJUST_VALUE(Tmp, Min, Max);
return Tmp;
}
template <typename OpT>
T& operator -= (const OpT& arg)
{
Data -= arg;
READJUST_VALUE(Data, Min, Max);
return Data;
}
template <typename OpT>
T& operator * (const OpT& arg)
{
Tmp = Data * Arg;
READJUST_VALUE(Tmp, Min, Max);
return Tmp;
}
template <typename OpT>
T& operator *= (const OpT& arg)
{
Data *= arg;
READJUST_VALUE(Data, Min, Max);
return Data;
}
template <typename OpT>
T& operator / (const OpT& arg)
{
if(arg = 0) arg = 1;
Tmp = Data / Arg;
READJUST_VALUE(Tmp, Min, Max);
return Tmp;
}
template <typename OpT>
T& operator /= (const OpT& arg)
{
if(arg = 0) arg = 1;
Data /= arg;
READJUST_VALUE(Data, Min, Max);
return Data;
}
T& operator ++(RangedNum<T>& arg)
{
arg.Data++;
return arg.Data;
}
T& operator ++ ()
{
Data++;
return Data;
}
template <typename OpT>
bool operator == (const OpT& arg) const
{
return (Data == arg);
}
template <typename OpT>
bool operator != (const OpT& arg) const
{
return (Data != arg);
}
protected:
T Data, Min, Max, Tmp;
};
#endif // RANGED_NUM_H_INCLUDED
Last edited by Desolation; 07-24-2006 at 05:13 PM. |
| Desolation is offline | |
| | #2 |
| and the hat of marbles Join Date: May 2002 Location: Göteborg, Sweden
Posts: 2,038
| You are using nice features like templates and operator overloading, but at the same time you're using macros. To make the ++ operator work, use an unnamed int argument to distinguish pre- and post-increment. I'm sure you can google somthing.
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling |
| Sang-drax is offline | |
| | #3 |
| Devil's Advocate Join Date: May 2004 Location: Out of scope
Posts: 3,735
| As Sang-drax was saying, the prefix takes no operators, the postfix takes a dummy int operator. It's used for nothing, it's just there to tell the compiler that this is the postfix version.
__________________ Terms of Service By quoting or replying directly to this post, you consent to the fact that all of the information in the post above is completely accurate and highly intelligent and no comments will be made towards its validity, thoughtlessness, and/or grammatical structure. Violators will be prosecuted to the fullest extent of the law. |
| SlyMaelstrom is offline | |
| | #4 |
| Registered User Join Date: May 2006
Posts: 894
| Ok thank you for the info =] As for using macros, they are perfectly fine if you make a good use of them. Besides, my macro should read Code: #define READJUST_VALUE(a, min, max) if((a) < (min)) (a) = (min); if((a) > (max)) (a) = (max) Code: #define READJUST_VALUE(a, min, max) if(a < min) a = min; if(a > max) a = max |
| Desolation is offline | |
| | #5 |
| (?<!re)tired Join Date: May 2006 Location: Portugal
Posts: 5,220
| And how is that possibly a good use of a macro?? Nice class though.
__________________ Originally Posted by brewbuck: Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster. |
| Mario F. is offline | |
| | #6 |
| Devil's Advocate Join Date: May 2004 Location: Out of scope
Posts: 3,735
| That's real nice, now what happens if that macro is used in a conditional statement, loop statement, or function? Don't design bad code because you know the limited ways it could be used properly. Write good code and then you don't have to worry about whether or not you can use it where you're using it.
__________________ Terms of Service By quoting or replying directly to this post, you consent to the fact that all of the information in the post above is completely accurate and highly intelligent and no comments will be made towards its validity, thoughtlessness, and/or grammatical structure. Violators will be prosecuted to the fullest extent of the law. |
| SlyMaelstrom is offline | |
| | #7 |
| Registered User Join Date: May 2006
Posts: 894
| Gneh, for you ladies ;-) Code: template <typename T>
inline T& ReadjustValues(T* a, T min, T max)
{
if((*a) < min) *a = min;
if((*a) > max) *a = max;
return *a;
}
Last edited by Desolation; 07-25-2006 at 09:27 PM. |
| Desolation is offline | |
| | #8 |
| Devil's Advocate Join Date: May 2004 Location: Out of scope
Posts: 3,735
| Would you mind capitalizing the 'a' on adjust in your function name? I see that as "read just values".
__________________ Terms of Service By quoting or replying directly to this post, you consent to the fact that all of the information in the post above is completely accurate and highly intelligent and no comments will be made towards its validity, thoughtlessness, and/or grammatical structure. Violators will be prosecuted to the fullest extent of the law. |
| SlyMaelstrom is offline | |
| | #9 |
| Registered User Join Date: May 2006
Posts: 894
| I tried to add a little system to catch the difference between a and min or a and max in case of an overflow/underflow but I get a "OverflowException<T> cannot be raised" error (and the same for UnderflowException), what does that mean ? Code: template <typename T>
inline T& ReAdjustValues(T* a, T min, T max) throw()
{
if((*a) < min)
{
*a = min;
throw (UnderflowException<T>(*a - min));
}
if((*a) > max)
{
*a = max;
throw (OverflowException<T>(*a - max));
}
return *a;
}
template <typename T>
class LimitBreakException
{
protected:
enum ExceptionType
{
LBE_OVERFLOW,
LBE_UNDERFLOW
};
public:
LimitBreakException(const T& diff) : Difference(diff)
{
}
virtual ~LimitBreakException() = 0;
const T& GetDifference() const
{
return Difference;
}
const ExceptionType& GetExceptionType() const
{
return EType;
}
const char* GetErrStr()
{
ErrStr.clear();
ErrStr << ((EType == LBE_OVERFLOW) ? "Overflow" : "Underflow");
ErrStr << " error by " << Difference;
return ErrStr.str().c_str();
}
protected:
ExceptionType EType;
T Difference;
private:
std::stringstream ErrStr;
};
template <typename T>
class UnderflowException : public LimitBreakException<T>
{
public:
UnderflowException(const T& diff) : LimitBreakException<T>(diff), EType(LBE_UNDERFLOW)
{
}
~UnderflowException()
{
}
};
template <typename T>
class OverflowException : public LimitBreakException<T>
{
public:
OverflowException(const T& diff) : LimitBreakException<T>(diff), EType(LBE_OVERFLOW)
{
}
~OverflowException()
{
}
};
PS: I did pre-declare the classes before using them, this is not the problem. Last edited by Desolation; 07-25-2006 at 10:08 PM. |
| Desolation is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question about random numbers | Kempelen | C Programming | 2 | 07-02-2008 06:28 AM |
| Logical errors with seach function | Taka | C Programming | 4 | 09-18-2006 05:20 AM |
| Generating ranged randomized numbers | Cloud_909 | C++ Programming | 8 | 12-05-2005 05:42 PM |
| the definition of a mathematical "average" or "mean" | DavidP | A Brief History of Cprogramming.com | 7 | 12-03-2002 11:15 AM |
| A (complex) question on numbers | Unregistered | C++ Programming | 8 | 02-03-2002 06:38 PM |