hari4063 wrote:
This is mostly for Borland Builder and new keyword "__property". I
try (this is not my original idea) to make class (template) that
imitates __property. Basic idea was proposed by my friend, but I try
to rewrite Borlands VCL without __property to ensure that only
standard C++ is used.
Is any work similary to this one.
Greetz.
Is something like this what you want (I don't know what borland's extensions
do)?
- Pete
#include <iostream>
#include <string>
using namespace std;
template <class T, class Modify>
class property
{
protected:
T var;
Modify mod;
public:
property<T, Modify>(const T& init, const Modify& m)
: mod(m), var(init)
{}
operator T()
{
return var;
}
property& operator =(const T& val)
{
if(!mod(val))
throw invalid_argument("bad property argument.");
var = val;
return *this;
}
};
template<class T>
struct RangeValidationPropModified
{
string msg;
T minval, maxval;
public:
RangeValidationPropModified<T>(string m, const T& minv, const T& maxv)
: msg(m),
minval(minv),
maxval(maxv)
{}
bool operator() (const T& val)
{
if(val >= minval && val <= maxval)
{
cout << msg;
return true;
}
else
return false;
}
};
int main(int argc, char* argv[])
{
property<int, RangeValidationPropModified<int> >
p(0, RangeValidationPropModified<int>("the property is being modifed!\n", 0,
100));
cout << p << endl;
p = 5;
cout << p << endl;
p = 10;
cout << p << endl;
try
{
p = -1;
cout << p << endl;
p = 1000;
cout << p << endl;
}
catch(invalid_argument ex)
{
cout << ex.what() << endl;
}
return 0;
}