Imagine a class representing a mail :
class mail {
string subject;
string content;
date receivedDate;
};
Now what I want to achieve is to know if my mail data is set, and once they're set, which ones where changed. I could go with a combination of std::optional and a std::map like this :
class Mail {
std::optional<string> subject;
std::optional<string> content;
std::optional<date> receivedDate;
enum EField { Subject, Content, ReceivedDate };
typedef std::map<EField, bool> ChangedMap;
ChangedMap changedFields;
public:
Mail(optional<string> subject, ... ) {
// initialize map with fields... hard coded
}
bool HasSubject() const { return subject; }
string GetSubject() const { return subject.get(); }
void SetSubject(const std::string& newSubject) {
subject = newSubject;
changedFields[Subject] = true;
}
void RemoveSubject() {
changedFields[Subject] = HasSubject();
subject.reset();
}
bool IsSubjectChanged() const {
return changedFields[Subject];
}
};
But I really think I am missing something crucial here. Would you see any better way to do it, preferably with less memory usage and no hardcoded values ?
I thought about about inheriting from std::optional but I don't see it as a good thing too.
Thanks