#pragma once ///////////////////////////////////////////////////////////////////// // CppProperties.h - Defines read/write and read only properties // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2017 // ///////////////////////////////////////////////////////////////////// // // Got the idea from discussion here (scroll down about halfway): // http://stackoverflow.com/questions/8368512/does-c11-have-c-style-properties // This implementation is quite different however. #include #include ///////////////////////////////////////////////////////////////////// // Property class provides encapsulated properties for C++ // - getter and setter functions can be customized without changing // the client interface for any class that uses Property // template class Property { public: // initialize the property field Property() : value(T()) {} Property(const T& t) : value(t) {} virtual ~Property() {} // set the property field with assignment operator virtual Property& operator=(const T &t) { value = setter_(t); return *this; } // get the property field with cast operator virtual operator const T() const { return getter_(); } // define methods to customize getter and setter behaviors void setter(std::function sttr) { setter_ = sttr; } void getter(std::function gttr) { getter_ = gttr; } T getValue() { return value; } // used by custom getters protected: T value; // property field // define default getter and setter behavior using lambdas std::function setter_ = [](const T& t) { return t; }; // setter std::function getter_ = [=]() { return value; }; // gettter }; /*----< overload insertion operator for std::string properties >---*/ std::ostream& operator<<(std::ostream& out, Property& p) { std::string temp = p; try { out << (temp).c_str(); } catch (std::exception& ex) { std::cout << "\n exception:\n " << ex.what() << "\n"; } return out; } ///////////////////////////////////////////////////////////////////// // Property class provides encapsulated properties for C++ // - getter function can be customized without changing // the client interface for any class that uses Property // - no setter is provided since the class is used for const properties // - comments for this class would be same as above, and so are omitted // template class Property { public: Property() : value(T()) {} Property(const T& t) : value(t) {} virtual ~Property() {} void getter(std::function gttr) { getter_ = gttr; } virtual operator const T() const { return getter_(); } // getter T getValue() { return value; } // used by custom getters protected: T value; std::function getter_ = [=]() { return value; }; // default gettter }; /*----< overload insertion operator for std::string properties >---*/ std::ostream& operator<<(std::ostream& out, Property& p) { std::string temp = p; try { out << (temp).c_str(); } catch (std::exception& ex) { std::cout << "\n exception:\n " << ex.what() << "\n"; } return out; }