TL;DR The logging services code uses smart pointers now, but where it makes sense and not where it would make usage verbose and obfuscated.
This failure to implement the intended guarded approach to resources propagates back to the seralizor_policy class that has now done away with the public close() method in favour of having the destructor tidy up - as it should:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
virtual ~serializor_policy() { | |
if (is_open()) { | |
do_close(); | |
assert(is_closed()); | |
} | |
} |
So to set up the smart pointer usage I knocked up a couple of aliases to reduced code clutter and obfuscation:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using serializor_ptr = std::unique_ptr<io::serializor_policy<std::string>>; | |
using stamp_ptr = std::shared_ptr<log::stamp_policy>; | |
. | |
. | |
. | |
logger(const std::string name, std::initializer_list<stamp_ptr> stamp_list): | |
write_policy(new policy), | |
stamps(stamp_list) { | |
help_init(name); | |
} | |
. | |
. | |
. | |
virtual ~logger() { | |
//write_policy->close(); | |
//delete write_policy; | |
//(auto stamp: stamps) | |
//delete(stamp); | |
} | |
. | |
. | |
. | |
serializor_ptr write_policy; | |
std::initializer_list<stamp_ptr> stamps; |
Or was it...?
Because now in order to use logger the instantiation of the shared_ptr guarded stamp_policy(s) the code is now verbose and obfuscates the intention:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log::logger<io::console_serializor> clog("", | |
{std::shared_ptr<log::stamp_policy>(new log::line_number_stamp()), | |
std::shared_ptr<log::stamp_policy>(new log::line_number_stamp(10)), | |
std::shared_ptr<log::stamp_policy>(new log::unix_time_stamp())}); |
For the sake of saving a few lines in the destructor this is just not worth to the end user (me).
So with a quick change to the alias the job is fixed!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using stamp_ptr = log::stamp_policy*; | |
. | |
. | |
. | |
virtual ~logger() { | |
for (auto stamp: stamps) | |
delete(stamp); | |
} |
[1] Using C++11 Smart Pointers - Kieras (2013)
[2] What is the difference between 'typedef' and 'using' in C++11? (2012)
No comments:
Post a Comment