Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/VLXVisitorLinker.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef VLXVisitorLinker_INCLUDE_ONCE
00033 #define VLXVisitorLinker_INCLUDE_ONCE
00034 
00035 #include <vlCore/VLXVisitor.hpp>
00036 #include <vlCore/VLXValue.hpp>
00037 
00038 namespace vl
00039 {
00041   class VLXVisitorLinker: public VLXVisitor
00042   {
00043     VL_INSTRUMENT_CLASS(vl::VLXVisitorLinker, VLXVisitor)
00044 
00045   public:
00046     typedef enum 
00047     {
00048       NoError,
00049       UnresolvedID
00050     } EError;
00051 
00052   public:
00053     VLXVisitorLinker(const std::map< std::string, ref<VLXStructure> >* map=NULL)
00054     {
00055       mLinkMap = map;
00056       mError = NoError;
00057     }
00058 
00059     void setLinkMap(const std::map< std::string, ref<VLXStructure> >* map)
00060     {
00061       mLinkMap = map;
00062     }
00063 
00064     VLXStructure* link(const std::string& uid)
00065     {
00066       VL_CHECK(mLinkMap)
00067       VL_CHECK(!uid.empty())
00068       std::map< std::string, ref<VLXStructure> >::const_iterator it = mLinkMap->find(uid);
00069       if( it != mLinkMap->end() )
00070       {
00071         // this should never happen
00072         VL_CHECK(uid != "#NULL")
00073 
00074         /* Log::debug( Say("- ID '%s' linked to '%s'.\n") << uid << it->second->tag() ); */
00075         return it->second.get_writable();
00076       }
00077       else
00078       {
00079         if (uid != "#NULL")
00080         {
00081           mError = UnresolvedID;
00082           Log::error( Say("Could not link ID '%s' to anything!\n") << uid );
00083         }
00084         return NULL;
00085       }
00086     }
00087 
00088     virtual void visitStructure(VLXStructure* obj)
00089     {
00090       if (isVisited(obj))
00091         return;
00092 
00093       for(size_t i=0; i<obj->value().size(); ++i)
00094       {
00095         if (obj->value()[i].value().type() == VLXValue::Structure)
00096           obj->value()[i].value().getStructure()->acceptVisitor(this);
00097         else
00098         if (obj->value()[i].value().type() == VLXValue::List)
00099           obj->value()[i].value().getList()->acceptVisitor(this);
00100         else
00101         /*
00102         if (obj->value()[i].value().type() == VLXValue::ArrayID)
00103           obj->value()[i].value().getArrayID()->acceptVisitor(this);
00104         else
00105         */
00106         if (obj->value()[i].value().type() == VLXValue::ID)
00107         {
00108           // transform ID -> Structure
00109           VLXStructure* lnk_obj = link( obj->value()[i].value().getID() );
00110           obj->value()[i].value().setStructure( lnk_obj );
00111         }
00112       }
00113     }
00114 
00115     virtual void visitList(VLXList* list)
00116     {
00117       // this should happen only if the user manually creates loops
00118       if (isVisited(list))
00119       {
00120         Log::warning("VLXVisitorLinker: cycle detected on VLXList.\n");
00121         return;
00122       }
00123 
00124       for(size_t i=0; i<list->value().size(); ++i)
00125       {
00126         if (list->value()[i].type() == VLXValue::Structure)
00127           list->value()[i].getStructure()->acceptVisitor(this);
00128         if (list->value()[i].type() == VLXValue::List)
00129           list->value()[i].getList()->acceptVisitor(this);
00130         else
00131         /*
00132         if (list->value()[i].type() == VLXValue::ArrayID)
00133           list->value()[i].getArrayID()->acceptVisitor(this);
00134         else
00135         */
00136         if (list->value()[i].type() == VLXValue::ID)
00137         {
00138           // transform ID -> Structure
00139           VLXStructure* lnk_obj = link( list->value()[i].getID() );
00140           list->value()[i].setStructure( lnk_obj );
00141         }
00142       }
00143     }
00144 
00145     /*
00146     virtual void visitArray(VLXArrayString*)  {}
00147 
00148     virtual void visitArray(VLXArrayID* arr)
00149     {
00150       // retrieves the assigned Structure
00151       for(size_t i=0 ;i<arr->value().size(); ++i)
00152         arr->value()[i].setStructure ( link(arr->value()[i].uid()) );
00153     }
00154 
00155     virtual void visitArray(VLXArrayIdentifier*) {}
00156     */
00157 
00158     virtual void visitArray(VLXArrayInteger*)  {}
00159 
00160     virtual void visitArray(VLXArrayReal*)  {}
00161 
00162     EError error() const { return mError; }
00163 
00164     void setError(EError err) { mError = err; }
00165 
00166   private:
00167     const std::map< std::string, ref<VLXStructure> >* mLinkMap;
00168     EError mError;
00169   };
00170 }
00171 
00172 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:33.
Permission is granted to use this page to write and publish articles regarding Visualization Library.