CFugue
MString.h
1 /*
2  This is part of CFugue, the C++ Runtime for MIDI Score Programming
3  Copyright (C) 2009 Gopalakrishna Palem
4 
5  For links to further information, or to contact the author,
6  see <http://cfugue.sourceforge.net/>.
7 
8  $LastChangedDate: 2014-05-10 11:12:21 +0530 (Sat, 10 May 2014) $
9  $Rev: 587 $
10  $LastChangedBy: krishnapg $
11 */
12 
13 #ifndef __MSTRING_H__2B50AFA1_EFB9_428a_A397_3FFEA175FA33__
14 #define __MSTRING_H__2B50AFA1_EFB9_428a_A397_3FFEA175FA33__
15 
16 #include "_TChar.h" // On Non win32 platforms we use a local TChar.h
17 #include <string>
18 #include <string.h>
19 #include <wchar.h>
20 #include <stdlib.h>
21 #include <sstream>
22 
23 namespace CFugue
24 {
25 #if 1 // defined _UNICODE || defined UNICODE
26  typedef std::wstringstream _stringstreamBase;
27  typedef std::wstring _stringBase;
28  typedef wchar_t _baseChar;
29  typedef char _otherChar;
30 #else
31  typedef std::stringstream _stringstreamBase;
32  typedef std::string _stringBase;
33  typedef char _baseChar;
34  typedef wchar_t _otherChar;
35 #endif
36  /// <Summary> Helper class for string stream manipulations, useful for tracing </Summary>
37  /// It is advised to use always std::wstringstream as the base, since it is capable of
38  /// dealing with both unicode and non-unicode (i.e., it correctly promotes the char* to wchar_t*).
39  /// On the otherhand, when std::stringstream is used as the base, any unicode input supplied to it
40  /// will be just read as unsigned int* and not as string.
41  class MStringStream : public _stringstreamBase
42  {
43  public:
44  typedef _stringstreamBase _Mybase;
45  inline MStringStream(){}
46  inline MStringStream(const wchar_t* arg) { *this << arg; }
47  inline MStringStream(const char* arg) { *this << arg; }
48  };
49 
50  /// <Summary> Helper class for simple string manipulations </Summary>
51  class MString : public _stringBase
52  {
53  typedef const _baseChar* CHAR_PTR;
54  typedef const _otherChar* OCHAR_PTR;
55  public:
56  typedef _stringBase _Mybase;
57  inline MString() {}
58  //inline MString(MString&& other) : _stringBase(std::move(other)) { }
59  inline MString(CHAR_PTR arg) : _stringBase(arg) { }
60  inline MString(OCHAR_PTR arg) : _stringBase(MStringStream(arg).str()) { }
61  inline MString(const MStringStream& arg) : _stringBase(arg.str()) { }
62  template<typename T> inline MString& operator<<(T arg)
63  {
64  MStringStream _s; _s << arg; this->append(_s.str()); return *this;
65  }
66  //inline MString& operator=(MString&& other)
67  //{
68  // _Mybase::operator=(std::move(other)); return *this;
69  //}
70  inline operator CHAR_PTR() const { return c_str(); }
71  };
72 
73 // /// <Summary> Helper class for simple string manipulations </Summary>
74 // class MString : public
75 //#ifdef UNICODE
76 // std::wstring
77 //#else
78 // std::string
79 //#endif // UNICODE
80 // {
81 //#ifdef UNICODE
82 // typedef std::wstring base;
83 //#else
84 // typedef std::string base;
85 //#endif // UNICODE
86 // public:
87 // inline MString() : base()
88 // { }
89 // inline MString(const TCHAR* sz) : base(sz)
90 // { }
91 // inline MString(const base& sz) : base(sz)
92 // { }
93 // inline MString& operator += (const TCHAR* sz)
94 // {
95 // base::operator += (sz);
96 // return *this;
97 // }
98 // inline MString operator + (const TCHAR* sz) const
99 // {
100 // return (base&)(*this) + sz;
101 // }
102 // inline MString operator + (const TCHAR ch) const
103 // {
104 // return (base&)(*this) + ch;
105 // }
106 // inline MString operator + (const MString& other) const
107 // {
108 // return (*this) + (const TCHAR*)other;
109 // }
110 // inline friend MString operator + ( const TCHAR* sz, const MString& obj)
111 // {
112 // return MString(sz) + obj;
113 // }
114 // inline operator const TCHAR* () const { return c_str(); }
115 //
116 //#if defined UNICODE || defined _UNICODE
117 // inline MString(const char* arg)
118 // {
119 // size_t nLen = strlen(arg);
120 // wchar_t *pSz = new wchar_t[nLen +2];
121 // mbstowcs(pSz, arg, nLen+1);
122 // *this = pSz;
123 // delete[] pSz;
124 // }
125 // inline MString operator + (const char* sz) const
126 // {
127 // return std::move((base&)(*this) + MString(sz));
128 // }
129 //#else
130 // inline MString(const wchar_t* argw)
131 // {
132 // size_t nLen = wcslen(argw);
133 // char* pSz = new char[nLen + 2];
134 // wcstombs(pSz, argw, nLen+1);
135 // *this = pSz;
136 // delete[] pSz;
137 // }
138 // inline MString operator + (const wchar_t* sz) const
139 // {
140 // return std::move((base&)(*this) + MString(sz));
141 // }
142 //#endif // Cross Unicode and normal initialization
143 //
144 //#if defined QSTRING_H
145 // #if defined UNICODE || defined _UNICODE
146 // inline QString toQString() const
147 // {
148 // return QString::fromStdWString((base&)*this); //QString((QChar*)stdWString.c_str(), (int) stdWString.length());
149 // }
150 // inline static QString toQString(const MString& stdWString)
151 // {
152 // return stdWString.toQString();
153 // }
154 // inline static MString::base fromQString(const QString& qString)
155 // {
156 // return qString.toStdWString(); //std::wstring((wchar_t*)qString.unicode());
157 // }
158 // #else
159 // #error "no QString to MString conversions in non-unicode mode"
160 // #endif // unicode, normal QString
161 //#endif // if QString is available
162 //
163 // }; // class MString
164 
165 } // namespace CFugue
166 
167 #endif // __MSTRING_H__2B50AFA1_EFB9_428a_A397_3FFEA175FA33__
Helper class for simple string manipulations
Definition: MString.h:51

CFugue, the C++ Music Programming Library © Copyright 2009 Cenacle Research India Private Limited Gopalakrishna Palem