by_library/properties/properties/properties.cpp
[C++ only] Illustrates use of the
Properties library field_property_get and
method_property_getset_external components. This example demonstrates the definition of a read-only field property in conjunction with a (validating) read/write external method property.
#include <stlsoft/properties/field_properties.hpp>
#include <stlsoft/properties/method_properties.hpp>
#include <exception>
#include <iostream>
#include <string>
#include <stdexcept>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
#include <stdio.h>
#include <stdlib.h>
class ClassWithProperties
{
public:
typedef std::string string_type;
typedef int index_type;
public:
ClassWithProperties(char const *name, char const *value)
: Name(name)
, m_value(value)
{}
public:
string_type get_Value() const
{
return m_value;
}
void set_Value(char const *newValue)
{
if( NULL == newValue ||
'\0' == *newValue)
{
throw std::runtime_error("Invalid value of Value");
}
else
{
m_value = newValue;
}
}
public:
stlsoft::field_property_get<string_type, string_type const&, ClassWithProperties> Name;
STLSOFT_METHOD_PROPERTY_GETSET_EXTERNAL(string_type, char const*, ClassWithProperties, get_Value, set_Value, Value);
private:
string_type m_value;
};
int main()
{
ClassWithProperties c("date", "today");
cout << "Name: " << c.Name << endl;
cout << "Value: " << c.Value << endl;
cout << "Changing the value" << endl;
c.Value = "tomorrow";
cout << "New Value: " << c.Value << endl;
return EXIT_SUCCESS;
}