当前正在尝试使用ogre和stl创建XML系统。作为概念的证明,我们试图让它将XML文件的内容输出到日志文件中。遗憾的是,我们当前的代码无法编译,我们也不知道为什么。相关代码如下:
这里是一个继承自的流类,用于简化自定义streambuf的管理。它存在的主要原因是删除了它的析构函数中的自定义streambuf。
class ResourceInputStream : public std::istream
{
private:
internal::OgreDataStreamBuf* OgreBuffer;
ResourceManager* Manager;
/// @internal
/// @brief Called by the constructors to actually construct this class
/// @param InputBuffer A pointer to an internal::OgreDataStreamBuf. A buffer to read from the disk access subsystem (which happens to be part of Ogre). This Stream will assume ownership of this buffer and will handle deleting it.
/// @param ResourceManager_ Currently unused, future functionality may tuse this.
void Construct(std::streambuf *InputBuffer, ResourceManager* ResourceManager_);
protected:
public:
/// @brief Descriptive Constructor
/// @param InputBuffer A pointer to an internal::OgreDataStreamBuf. A buffer to read from the disk access subsystem (which happens to be part of Ogre). This Stream will assume ownership of this buffer and will handle deleting it.
/// @param ResourceManager_ Currently unused, future functionality may tuse this.
/// @warning Do not delete the InputBuffer you pass in, this class will assume owner ship and delete it on it's own
ResourceInputStream(std::streambuf *InputBuffer, ResourceManager* ResourceManager_) :
std::istream(InputBuffer)
{ this->Construct(InputBuffer, ResourceManager_); }
/// @brief Tears down the Stream, and Delete the Buffer Passed in.
virtual ~ResourceInputStream();
};
下面是自定义streambuf类的定义。它使用ogredatastreambuf读取由ogre资源库管理的压缩文件:
class OgreDataStreamBuf : public std::streambuf
{
protected:
/// @brief a shard_ptr to the internal Ogre Datastream
Ogre::DataStreamPtr OgreStream;
public:
/// @brief constructor
/// @param Datum A pointer to the Ogre Datastream that this stream will use
OgreDataStreamBuf(const Ogre::DataStreamPtr& Datum) : OgreStream(Datum)
{
#ifdef PHYSDEBUG
World::GetWorldPointer()->Log("Entering/Exiting OgreDataStreamBuf Constructor");
#endif
}
/// @brief Should get the amount of characters left in the sequence
/// @returns -1 if no estimate could be made, other wise this returns an estimate of the amount of bytes in the buffer
std::streamsize showmanyc();
/// @brief Gets a sequence of characters
/// @param s a Pointer to where the characters should go
/// @param n How many characters
/// @return This returns the amount of characters retrieved
std::streamsize xsgetn(char* s, std::streamsize n);
/// @brief puts a sequence of characters in
/// @param s a Pointer to the characters
/// @param n How many characters
/// @return This returns the amount of characters inserted
/// @detail currently unimplimented
std::streamsize xsputn(const char_type*, std::streamsize n);
};
}
下面是一段试图使用这个流类的代码。世界日志流是一个std::stringstream。
ResourceInputStream* XMLptr = TheWorld->GetResourceManager()->GetResourceStream("test.xml");
std::stringstream XMLStringStream;
(*XMLptr) >> XMLStringStream;
String ShouldHaveXML(XMLStringStream.str());
TheWorld->LogStream << "ShouldHaveXML: " << ShouldHaveXML << endl << "End XML Logging" <<endl;
TheWorld->Log("Delete XML Stream");
delete XMLptr;
尝试编译会产生此错误:
error: ambiguous overload for 'operator>>' in '* XMLptr >> XMLStringStream'
我研究过这个错误,唯一能发现的就是…这是由于某些被声明为常量的东西不应该这样做。据我们所知,我们的代码不是这样的。所以我对这件事的发生原因,或者如何着手解决这个问题感到茫然。任何见解都将受到极大的赞赏。