I have a string utility that converts the
boost::filesystem::path into a
string. The current implementation was done in the following way.
/** header file **/
using convert_typeX = std::codecvt_utf8<wchar_t>;
class Path {
...
...
private:
wstring_convert<convert_typeX, wchar_t> converterX;
};
/** implementation file **/
Path::Path(path thePath)
: rootPath(thePath), searchDone(false)
{
string filename = converterX.to_bytes(thePath.wstring());
string rootPath = converterX.to_bytes(this->rootPath.wstring());
...
}
This utility is spread across in every class that require path-to-string conversion. After some time when I look back on this code I felt that the design approach is very unpractical and quite troublesome too. Since this utility is used in everywhere, I just wonder, would it be better if I make it static. I'm just trying to simplify things, complex thing is tiring me.
/** header file **/
using convert_typeX = std::codecvt_utf8<wchar_t>;
class FileHelper {
...
...
public:
static string convertPathToString(boost::filesystem::path thePath) {
wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.to_bytes(thePath.wstring());
}
private:
wstring_convert<convert_typeX, wchar_t> converterX;
};
/** implementation file **/
Path::Path(path thePath)
: rootPath(thePath), searchDone(false)
{
string filename = FileHelper::convertPathToString(thePath);
string rootPath = FileHelper::convertPathToString(this->rootPath);
...
}
With the new approach, I centralize that piece into
FileHelper (a utility class specializes in file). When
Path objects need it, just direct call on
convertPathToString() from the class.
No comments:
Post a Comment