rainmeter-studio/Common/RawString.h

93 lines
1.7 KiB
C
Raw Normal View History

2012-01-08 17:35:29 +00:00
/*
Copyright (C) 2011 Birunthan Mohanathas
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2012-01-08 17:35:29 +00:00
*/
2013-05-31 14:34:36 +00:00
#ifndef RM_COMMON_RAWSTRING_H_
#define RM_COMMON_RAWSTRING_H_
2012-01-08 17:35:29 +00:00
#include <windows.h>
2013-05-31 14:18:52 +00:00
class RawString
2012-01-08 17:35:29 +00:00
{
public:
2013-05-31 14:18:52 +00:00
RawString() :
2012-01-08 17:35:29 +00:00
m_String()
{
}
2013-05-31 14:18:52 +00:00
RawString(const WCHAR* str) :
2012-01-08 17:35:29 +00:00
m_String(str_alloc(str))
{
}
2013-05-31 14:18:52 +00:00
RawString(const RawString& rhs) :
2012-03-17 12:23:49 +00:00
m_String(str_alloc(rhs.c_str()))
{
}
2013-05-31 14:18:52 +00:00
~RawString()
2012-01-08 17:35:29 +00:00
{
clear();
}
2013-05-31 14:18:52 +00:00
RawString& operator=(const WCHAR* rhs)
2012-01-08 17:35:29 +00:00
{
clear();
m_String = str_alloc(rhs);
return *this;
}
2013-05-31 14:18:52 +00:00
RawString& operator=(const RawString& rhs)
2012-01-08 17:35:29 +00:00
{
if (&rhs != this)
{
clear();
m_String = str_alloc(rhs.m_String);
}
return *this;
}
const WCHAR* c_str() const
{
return m_String ? m_String : L"";
}
bool empty() const
{
return !m_String || !(*m_String);
}
void clear()
{
if (m_String)
{
free(m_String);
2013-05-31 14:28:39 +00:00
m_String = nullptr;
2012-01-08 17:35:29 +00:00
}
}
private:
WCHAR* str_alloc(const WCHAR* str)
{
2013-05-31 14:28:39 +00:00
return str ? _wcsdup(str) : nullptr;
2012-01-08 17:35:29 +00:00
}
WCHAR* m_String;
};
#endif