Click here to Skip to main content
12,406,795 members (59,853 online)
Click here to Skip to main content

Stats

67.9K views
620 downloads
26 bookmarked
Posted

WTL CString Class Implemented with the Standard C++ Library

, 28 Sep 2006 CPL
Plug-in CodeProject's CStdString as WTL::CString and get the WTL CString support with a Standard C++ Library implementation.
// atlssmisc.h 
//
// Use class CStdString as WTL::CString: 
//		#include this file instead of <atlmisc.h> if you want WTL::CString to be a  
//		CStdString deriving from std::basic_string<TCHAR>.
//
// Copyright (c) Alain Rist 2006
//
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php).
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
//
//
// Notes: 
//		atlssmisc.h should be included first of WTL headers after atlwin.h.
//		StdString.h and this file must be in the same directory or allow: #include "StdString.h".
//		VS2005 and VCExpress projects should '#define _CRT_SECURE_NO_DEPRECATE' before '#include <atlbase.h>'
//		eVC4 projects should compile with /GX to enable unwind semantics for C++ exception handler.

///////////////////////////////////////////////////////////////////////////////////
// Recommended stdafx.h layout
/*
// stdafx.h 
//...
// Change these values to use different versions
//...

#define _CRT_SECURE_NO_DEPRECATE
#include <atlbase.h>

//#include <atlstr.h>  // uncomment for WTL ATL::CString support
//#define _WTL_NO_CSTRING // uncomment for WTL ATL::CString support or no WTL CString suppport
//#define _WTL_USE_CSTRING // uncomment for full WTL WTL::CString support

#include <atlapp.h>

extern CAppModule _Module;

#include <atlwin.h>

//#include <atlmisc.h> // uncomment AND comment next line for original WTL:CString defintion
#include <atlssmisc.h> // CStdString based WTL:CString defintion
//...
*/
///////////////////////////////////////////////////////////////////////////////////

#ifndef __ATLSSMISC_H__
	#define __ATLSSMISC_H__

#pragma once

#if _WTL_VER > 0x750
	#error atlssmisc.h requires WTL version under or equal to 7.5
#endif

#ifdef __ATLMISC_H__
	#error atlssmisc.h cannot be included after atlmisc.h
#endif

#if defined(_WTL_USE_CSTRING) && (defined(_ATL_TMP_NO_CSTRING) || defined(__ATLSTR_H__) || defined(_WTL_NO_CSTRING))
	#error Incompatible '#define xxSTRxx's found.
#endif 

// push CStdString in global namespace
#include "StdString.h"	

#if defined(_ATL_TMP_NO_CSTRING) || defined(__ATLSTR_H__) // project uses ATL::CString
	#define _WTL_NO_CSTRING
#endif // defined(_ATL_TMP_NO_CSTRING) || defined(__ATLSTR_H__)

#ifdef _WTL_USE_CSTRING
	#undef _WTL_USE_CSTRING 
	#define _WTL_USE_CSTRING 0
#endif

#if !defined(_WTL_USE_CSTRING) && !defined(_WTL_NO_CSTRING)
	#define _WTL_USE_CSTRING 1
#endif // !defined(_WTL_USE_CSTRING) && !defined(_WTL_NO_CSTRING)

#ifdef _WTL_USE_CSTRING
	#define _WTL_USE_STDSTRING

	namespace WTL
	{
	#if _WTL_USE_CSTRING == 1	// class CString not forward declared
		// push ::CStdString as WTL::CString
		typedef ::CStdString CString;
	#else						// atlapp.h may have declared class CString;
		// provide a class CString derived from ::CStdString
		class CString : public ::CStdString
		{
		// These typedefs are private to CStdStr so are their CString equivalents
			typedef ::CStdString				MYBASE;	 // my base class 
			typedef CString						MYTYPE;	 // myself
			typedef  MYBASE::const_iterator		MYCITER; // you get the idea...
			typedef  MYBASE::size_type			MYSIZE;   
			typedef  MYBASE::value_type			MYVAL; 
			typedef  MYBASE::allocator_type		MYALLOC;
		public:
		// CStdStr constructors propagated to CString
			CString() : ::CStdString(){}
			CString(const MYTYPE& str) : ::CStdString(str){}
			CString(const std::string& str) : ::CStdString(str){}
			CString(const std::wstring& str) : ::CStdString(str){}
			template <typename TC> 
			CString(const TC* pTC, MYSIZE n) : ::CStdString(pTC, n){}
			CString(PCUSTR pU) : ::CStdString(pU){}
			CString(PCSTR pA) : ::CStdString(pA){}
			CString(PCWSTR pW) : ::CStdString(pW){}
			CString(MYCITER first, MYCITER last) : ::CStdString(first, last){}
			CString(MYSIZE nSize, MYVAL ch, const MYALLOC& al=MYALLOC()) : ::CStdString(nSize, ch, al){}
	#ifdef SS_INC_COMDEF
			CString(const _bstr_t& bstr) : ::CStdString(bstr){}
	#endif // SS_INC_COMDEF
		};
	#endif // _WTL_USE_CSTRING == 1
	}; // Namespace WTL

	// prepare inclusion of atlmisc.h without WTL::CString
		#undef _WTL_USE_CSTRING
		#define _WTL_NO_CSTRING
		#define __ATLSTR_H__ // for CFindFile and CRecentDocumentList CString support
#endif // _WTL_USE_CSTRING

#include<atlmisc.h>

#ifdef _WTL_USE_STDSTRING
// set back proper macro definitions
	#undef __ATLSTR_H__
	#undef _WTL_NO_CSTRING
	#define _WTL_USE_CSTRING
#endif // _WTL_USE_STDSTRING

#endif // __ATLSSMISC_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)

Share

About the Author

Alain Rist
France France
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 29 Sep 2006
Article Copyright 2006 by Alain Rist
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid