src/Sim/Foundation/Resource/Builder/ResourceBuilder.h

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 //
00033 // The factory class for physical resources
00034 //
00035 
00036 #ifndef __RESOURCE_BUILDER_H
00037 #define __RESOURCE_BUILDER_H
00038 
00039 #include "Sim/Foundation/Resource/Builder/ResourceFactory.h"
00040 
00041 namespace Onikiri
00042 {
00043     class ResourceBuilder
00044     {
00045     public:
00046         // Node of structure
00047         struct ResNode
00048         {
00049             typedef 
00050                 boost::shared_ptr<ResNode> 
00051                 ResNodePtr;
00052 
00053             ResNodePtr parent;
00054             std::vector< PhysicalResourceNode* > instances;
00055 
00056             struct Child
00057             {
00058                 String to;
00059                 String name;
00060                 ResNodePtr node;
00061             };
00062             std::vector< Child > children;
00063 
00064             String type;
00065             String name;
00066             ParamXMLPath paramPath;
00067             ParamXMLPath resultPath;
00068             int count;
00069             int cluster;
00070             int copyCount;
00071             bool external;
00072 
00073             ResNode();
00074         };
00075         typedef ResNode::ResNodePtr ResNodePtr;
00076 
00077         typedef ParamXMLTree::Node    XMLNode;
00078         typedef ParamXMLTree::NodePtr XMLNodePtr;
00079         typedef ParamXMLTree::NodeArray XMLNodeArray;
00080         typedef ParamXMLTree::ChildMap  XMLChildMap;
00081         typedef ParamXMLTree::AttributeMap XMLAttributeMap;
00082 
00083     protected:
00084         ResNodePtr m_rootNode;
00085 
00086         typedef std::map<String, ResNodePtr> ResNodeMap;
00087         ResNodeMap m_resNodeMap;
00088         std::vector<PhysicalResourceNode*> m_instanceList;
00089 
00090         ResourceFactory m_factory;
00091 
00092         // Constant value map
00093         typedef std::map<String, int> ConstantMap;
00094         ConstantMap m_constMap;
00095 
00096         // Get the path of 'Configuration' node specified by 'Simulator/@Configuration'
00097         ParamXMLPath GetConfigurationPath();
00098         
00099         // Load the constant section in XML
00100         void LoadConstantSection(const ParamXMLPath& path);
00101 
00102         // Load the parameter section
00103         void LoadParameterSection(const ParamXMLPath& path);
00104 
00105         // Load the structure section
00106         void LoadStructureSection(const ParamXMLPath& path);
00107 
00108         // Get attribute of 'Structure' node 
00109         int GetStructureNodeValue(const XMLNodePtr node, const String& attrName);
00110         String GetStructureNodeString(const XMLNodePtr node, const String& attrName);
00111 
00112         // Process structure nodes recursively
00113         void TraverseStructureNode(ResNodePtr resParent, XMLNodePtr xmlParent, int copyCount);
00114 
00115         // Construct each resource from the node map
00116         void ConstructResources();
00117 
00118         // Connect each resource
00119         void ConnectResources();
00120 
00121         // Initialize all nodes
00122         void InitializeNodes( PhysicalResourceNode::InitPhase phase );
00123 
00124         // Validate connection.
00125         void ValidateConnection();
00126 
00127         // --- Dump the loaded data for debug
00128         void Dump();
00129 
00130     public:
00131         ResourceBuilder();
00132         virtual ~ResourceBuilder();
00133 
00134         // Set external resource
00135         void SetExternalResource( ResNode* node );
00136 
00137         // Construct all physical resources
00138         void Construct();
00139 
00140         // Release all configuration data
00141         void Release();
00142 
00143         // Get all resources
00144         std::vector<PhysicalResourceNode*> GetAllResources()
00145         {
00146             return m_instanceList; 
00147         }
00148 
00149         // Get resources specified by the passed name.
00150         // Todo: add a version specified by the type name
00151         template <class T>
00152         bool GetResource(const String& name, PhysicalResourceArray<T>& resArray)
00153         {
00154             ResNodeMap::iterator ni = m_resNodeMap.find( name );
00155             if( ni == m_resNodeMap.end() )
00156                 return false;
00157 
00158             ResNodePtr node = ni->second;
00159             size_t size = node->instances.size();
00160             resArray.Resize( size );
00161             for( size_t i = 0; i < size; i++ ){
00162                 resArray[i] = dynamic_cast<T*>( node->instances[i] );
00163             }
00164 
00165             return true;
00166         }
00167 
00168         template <class T>
00169         bool GetResource(const String& name, T*& resPtr)
00170         {
00171             PhysicalResourceArray<T> resArray;
00172             if( !GetResource( name, resArray ) ){
00173                 return false;
00174             }
00175 
00176             if( resArray.GetSize() != 1 ){
00177                 return false;
00178             }
00179 
00180             resPtr = resArray[0];
00181             return true;
00182         }
00183 
00184 
00185         // Get resources specified by the passed type name.
00186         template <class T>
00187         bool GetResourceByType(const String& typeName, PhysicalResourceArray<T>& resArray)
00188         {
00189             resArray.Clear();
00190             for( size_t i = 0; i < m_instanceList.size(); ++i ){
00191                 if( m_instanceList[i]->GetTypeName() == typeName ){
00192                     resArray.Add( dynamic_cast<T*>( m_instanceList[i] ) );
00193                 }
00194             }
00195 
00196             return resArray.GetSize() != 0;
00197         }
00198 
00199         template <class T>
00200         bool GetResourceByType(const String& typeName, T*& resPtr)
00201         {
00202             PhysicalResourceArray<T> resArray;
00203             if( !GetResourceByType( typeName, resArray ) ){
00204                 return false;
00205             }
00206 
00207             if( resArray.GetSize() != 1 ){
00208                 return false;
00209             }
00210 
00211             resPtr = resArray[0];
00212             return true;
00213         }
00214     };
00215 
00216 } // namespace Onikiri
00217 
00218 
00219 #endif

Onikiri2に対してTue Jun 18 14:34:23 2013に生成されました。  doxygen 1.4.7