目前正在尝试使用 Ogre 和 STL 创建 XML 系统。作为概念证明,我们试图让它将 XML 文件的内容输出到我们的日志文件中。遗憾的是,我们当前的代码无法编译,我们也不知道为什么。相关代码如下:
这是我们继承的流类,以简化自定义流缓冲区的管理。它存在的主要原因是删除其析构函数中的自定义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);
};
}
这是尝试使用此流类的一段代码。TheWorld->LogStream 是一个 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'
我已经研究过这个错误,我唯一能找到的关于这个......这是由于一些不应该被声明为 const 的东西。据我们所知,我们的代码并非如此。所以我不知道为什么会发生这种情况,或者如何解决它。任何见解将不胜感激。