GrpcPrint/PrintC/external/vl/include/vlX/VisitorLinkMapper.hpp
2024-04-01 18:26:14 +08:00

140 lines
5.3 KiB
C++

/**************************************************************************************/
/* */
/* Visualization Library */
/* http://visualizationlibrary.org */
/* */
/* Copyright (c) 2005-2020, Michele Bosi */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without modification, */
/* are permitted provided that the following conditions are met: */
/* */
/* - Redistributions of source code must retain the above copyright notice, this */
/* list of conditions and the following disclaimer. */
/* */
/* - Redistributions in binary form must reproduce the above copyright notice, this */
/* list of conditions and the following disclaimer in the documentation and/or */
/* other materials provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
/* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
/* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
/* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/**************************************************************************************/
#ifndef VLXVisitorLinkMapper_INCLUDE_ONCE
#define VLXVisitorLinkMapper_INCLUDE_ONCE
#include <vlX/Visitor.hpp>
namespace vlX
{
/** Compiles the link-map which associates a VLXStructure to it's ID, to be used later by other visitors. Can be called multiple times. */
class VisitorLinkMapper: public Visitor
{
VL_INSTRUMENT_CLASS(vlX::VisitorLinkMapper, Visitor)
public:
typedef enum
{
NoError,
DuplicateID
} EError;
public:
VisitorLinkMapper(std::map< std::string, vl::ref<VLXStructure> >* map=NULL)
{
mLinkMap = map;
mError = NoError;
}
void setLinkMap(std::map< std::string, vl::ref<VLXStructure> >* map)
{
mLinkMap = map;
}
void declareID(VLXStructure* obj)
{
if (obj->uid() != "#NULL")
{
const std::map< std::string, vl::ref<VLXStructure> >::const_iterator it = mLinkMap->find(obj->uid());
if (it == mLinkMap->end())
(*mLinkMap)[obj->uid()] = obj;
else
{
if ( it->second != obj )
{
mError = DuplicateID;
vl::Log::error( vl::Say("ID '%s' used by '%s' is already assigned to another node '%s'!\n") << obj->uid() << obj->tag() << it->second->tag() );
}
}
}
}
virtual void visitStructure(VLXStructure* obj)
{
if (isVisited(obj))
return;
declareID(obj);
for(size_t i=0; i<obj->value().size(); ++i)
{
if (obj->value()[i].value().type() == VLXValue::Structure)
obj->value()[i].value().getStructure()->acceptVisitor(this);
else
if (obj->value()[i].value().type() == VLXValue::List)
obj->value()[i].value().getList()->acceptVisitor(this);
}
}
virtual void visitList(VLXList* list)
{
// this should happen only if the user manually creates loops
if (isVisited(list))
{
vl::Log::warning("VisitorLinkMapper: cycle detected on VLXList.\n");
return;
}
for(size_t i=0; i<list->value().size(); ++i)
{
if (list->value()[i].type() == VLXValue::Structure)
list->value()[i].getStructure()->acceptVisitor(this);
else
if (list->value()[i].type() == VLXValue::List)
list->value()[i].getList()->acceptVisitor(this);
}
}
/*
virtual void visitArray(VLXArrayString*) {}
virtual void visitArray(VLXArrayIdentifier*) {}
virtual void visitArray(VLXArrayID*) {}
*/
virtual void visitArray(VLXArrayInteger*) {}
virtual void visitArray(VLXArrayReal*) {}
EError error() const { return mError; }
void setError(EError err) { mError = err; }
private:
std::map< std::string, vl::ref<VLXStructure> >* mLinkMap;
EError mError;
};
}
#endif