Skip to content
Snippets Groups Projects
Commit 0ad98de9 authored by Brad King's avatar Brad King
Browse files

vtkOStreamWrapper: Support std::string

This will allow std::string to be used with vtkSet/Get macros.

See discussion thread

 VTK coding conventions clarification
 http://www.vtk.org/pipermail/vtk-developers/2014-March/014867.html



It is not possible to portably forward-declare std::string or the
std::basic_string<> template, but we do not want to include <string>.
Instead, use a template that happens to match std::basic_string<> and
forwards to a private implementation (where <string> and <iostream>
are available) by casting through an internal concrete type.  Use
std::char_traits<> and std::allocator<> declarations that we know
vtkSystemIncludes.h brought in before including the
vtkOStreamWrapper.h header.

Add a new "TestOStreamWrapper" test case to cover use of the
vtkOStreamWrapper with std::string in a translation unit that acts
like wrapper-generated sources (define VTK_STREAMS_FWD_ONLY) where
no <iostream> is included.

Suggested-by: default avatarDavid C. Lonie <david.lonie@kitware.com>
Change-Id: I5bf4803b0e4b1e7c899b061fdbd6f66f08cbc7d6
parent 1aa0a945
No related branches found
No related tags found
No related merge requests found
......@@ -37,6 +37,7 @@ vtk_add_test_cxx(NO_DATA NO_VALID NO_OUTPUT
TestObjectFactory.cxx
TestObservers.cxx
TestObserversPerformance.cxx
TestOStreamWrapper.cxx
TestSMP.cxx
TestSmartPointer.cxx
TestSortDataArray.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestOStreamWrapper.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#define VTK_STREAMS_FWD_ONLY // like wrapper-generated sources
#include "vtkSystemIncludes.h"
#include <stdio.h> // test covers NOT including <iostream>
#include <string>
int TestOStreamWrapper(int, char *[])
{
int failed = 0;
std::string const expect = "hello, world: 1";
std::string actual;
std::string s = "hello, world";
vtkOStrStreamWrapper vtkmsg;
vtkmsg << s << ": " << 1;
actual = vtkmsg.str();
vtkmsg.rdbuf()->freeze(0);
if(actual != expect)
{
failed = 1;
fprintf(stderr, "Expected '%s' but got '%s'\n",
expect.c_str(), actual.c_str());
}
return failed;
}
......@@ -21,6 +21,8 @@
#include "vtkObjectBase.h"
#include "vtkSmartPointerBase.h"
#include <string>
#define VTKOSTREAM_OPERATOR(type) \
vtkOStreamWrapper& vtkOStreamWrapper::operator << (type a) \
{ this->ostr << a; return *this; }
......@@ -86,6 +88,13 @@ VTKOSTREAM_OPERATOR_FUNC(float* (*a)(void*));
VTKOSTREAM_OPERATOR_FUNC(const char* (*a)(void*));
VTKOSTREAM_OPERATOR_FUNC(void (*a)(void*, int*));
//----------------------------------------------------------------------------
vtkOStreamWrapper& vtkOStreamWrapper::operator << (std_string const& s)
{
this->ostr << reinterpret_cast<std::string const&>(s);
return *this;
}
//----------------------------------------------------------------------------
#if defined(__IBMCPP__)
vtkOStreamWrapper& vtkOStreamWrapper::WriteInternal(const char* a)
......
......@@ -43,6 +43,7 @@ class vtkStdString;
class VTKCOMMONCORE_EXPORT vtkOStreamWrapper
{
class std_string;
public:
// Description:
// Construct class to reference a real ostream. All methods and
......@@ -106,6 +107,14 @@ public:
vtkOStreamWrapper& operator << (const char* (*)(void*));
vtkOStreamWrapper& operator << (void (*)(void*, int*));
// Accept std::string without a declaration.
template <template <typename, typename, typename> class S>
vtkOStreamWrapper& operator << (const
S< char, std::char_traits<char>, std::allocator<char> >& s)
{
return *this << reinterpret_cast<std_string const&>(s);
}
// Description:
// Forward the write method to the real stream.
vtkOStreamWrapper& write(const char*, unsigned long);
......@@ -137,6 +146,7 @@ protected:
ostream& ostr;
private:
vtkOStreamWrapper& operator=(const vtkOStreamWrapper& r); // Not Implemented.
vtkOStreamWrapper& operator << (std_string const&);
};
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment