OMNeT++ NEDXML 6.1
Discrete Event Simulation Library
nedresourcecache.h
Go to the documentation of this file.
1 //==========================================================================
2 // NEDRESOURCECACHE.H -
3 //
4 // OMNeT++/OMNEST
5 // Discrete System Simulation in C++
6 //
7 //==========================================================================
8 
9 /*--------------------------------------------------------------*
10  Copyright (C) 2002-2017 Andras Varga
11  Copyright (C) 2006-2017 OpenSim Ltd.
12 
13  This file is distributed WITHOUT ANY WARRANTY. See the file
14  `license' for details on this and other legal matters.
15 *--------------------------------------------------------------*/
16 
17 
18 #ifndef __OMNETPP_NEDXML_NEDRESOURCECACHE_H
19 #define __OMNETPP_NEDXML_NEDRESOURCECACHE_H
20 
21 #include <map>
22 #include <vector>
23 #include <string>
24 #include "nedelements.h"
25 #include "nedtypeinfo.h"
26 
27 namespace omnetpp {
28 namespace nedxml {
29 
30 class ErrorStore;
31 
38 {
39  ASTNode *element; // compound module or NED file
40  std::string qname; // fully qualified name, or (for NED files) package name
41  NedLookupContext(ASTNode *e, const char *q) {element=e;qname=q;}
42 };
43 
53 {
54  public:
56  class INedTypeNames {
57  public:
59  virtual bool contains(const char *qname) const = 0;
60 
62  virtual int size() const = 0;
63 
65  virtual const char *get(int k) const = 0;
66 
67  virtual ~INedTypeNames() {} // make the compiler happy
68  };
69 
70  class CachedTypeNames : public INedTypeNames {
71  protected:
73  public:
75  virtual bool contains(const char *qname) const override {return p->lookup(qname)!=nullptr;}
76  virtual int size() const override {return p->getTypeNames().size();}
77  virtual const char *get(int k) const override {return p->getTypeNames()[k].c_str();}
78  };
79 
80  protected:
81  // table of loaded NED files (by NED file name as absolute path, canonical representation)
82  std::map<std::string,NedFileElement*> nedFiles;
83 
84  // "package.ned" files by package name (only one per package accepted)
85  std::map<std::string, NedFileElement*> packageDotNedFiles;
86 
87  // table of NED type declarations; key is fully qualified name, and elements point into nedFiles
88  std::map<std::string, NedTypeInfo*> nedTypes;
89 
90  // cached keys of the nedTypes map, for getTypeNames()
91  std::vector<std::string> nedTypeNames;
92 
93  // maps the loaded source NED folders (as absolute paths, canonical representation)
94  // to package names
95  typedef std::map<std::string,std::string> StringMap;
97 
98  protected:
99  virtual void addFile(NedFileElement *node, const char *expectedPackage);
100  virtual void registerBuiltinDeclarations();
101  virtual int doLoadNedSourceFolder(const char *foldername, const char *expectedPackage, const std::vector<std::string>& excludedFolders);
102  virtual void doLoadNedFileOrText(const char *nedfname, const char *nedtext, const char *expectedPackage, bool isXML);
103  virtual NedFileElement *parseAndValidateNedFileOrText(const char *nedfname, const char *nedtext, bool isXML);
104  virtual std::string determineRootPackageName(const char *nedSourceFolderName);
105  virtual std::string getNedSourceFolderForFolder(const char *folder) const;
106  virtual void collectNedTypesFrom(ASTNode *node, const std::string& packagePrefix, bool areInnerTypes);
107  virtual NedTypeInfo *createTypeInfo(const char *qname, bool isInnerType, ASTNode *node);
108  virtual void registerNedType(const char *qname, bool isInnerType, ASTNode *node);
109  virtual bool hasResolvedTypeUnder(const std::string& packageName) const;
110  virtual std::string getFirstError(ErrorStore *errors, const char *prefix=nullptr);
111 
112  public:
115 
117  virtual ~NedResourceCache();
118 
131  virtual int loadNedSourceFolder(const char *foldername, const char *excludedPackages);
132 
140  virtual void loadNedFile(const char *nedfname, const char *expectedPackage, bool isXML);
141 
152  virtual void loadNedText(const char *name, const char *nedtext, const char *expectedPackage, bool isXML);
153 
159  virtual void resolveAllNedTypes();
160 
165  virtual void doneLoadingNedFiles();
166 
171  std::vector<NedFileElement*> getPackageNedListForLookup(const char *packageName) const;
172 
176  virtual NedTypeInfo *lookup(const char *qname) const;
177 
181  virtual NedTypeInfo *getDecl(const char *qname);
182 
187  virtual std::string lookupNedType(const NedLookupContext& context, const char *nedTypeName, const INedTypeNames& amongQNames);
188 
192  virtual std::string lookupNedType(const NedLookupContext& context, const char *nedTypeName) {
193  return lookupNedType(context, nedTypeName, CachedTypeNames{this});
194  }
195 
199  virtual const std::vector<std::string>& getTypeNames() const {return nedTypeNames;}
200 
205  virtual std::string getNedPackageForFolder(const char *folder) const;
206 
210  static NedLookupContext getParentContextOf(const char *qname, ASTNode *node);
211 
212 };
213 
214 } // namespace nedxml
215 } // namespace omnetpp
216 
217 
218 #endif
219 
omnetpp::nedxml::NedResourceCache
Stores loaded NED files, and keeps track of components in them.
Definition: nedresourcecache.h:52
omnetpp::nedxml::NedLookupContext::element
ASTNode * element
Definition: nedresourcecache.h:39
nedtypeinfo.h
omnetpp::nedxml::NedResourceCache::CachedTypeNames::size
virtual int size() const override
Definition: nedresourcecache.h:76
omnetpp::nedxml::NedResourceCache::StringMap
std::map< std::string, std::string > StringMap
Definition: nedresourcecache.h:95
omnetpp::nedxml::NedLookupContext::qname
std::string qname
Definition: nedresourcecache.h:40
omnetpp::nedxml::NedResourceCache::packageDotNedFiles
std::map< std::string, NedFileElement * > packageDotNedFiles
Definition: nedresourcecache.h:85
omnetpp::nedxml::NedLookupContext
Context of NED type lookup, for NedResourceCache.
Definition: nedresourcecache.h:37
omnetpp::nedxml::NedResourceCache::NedResourceCache
NedResourceCache()
Definition: nedresourcecache.h:114
omnetpp::nedxml::NedResourceCache::lookup
virtual NedTypeInfo * lookup(const char *qname) const
NEDXML_API
#define NEDXML_API
Definition: nedxmldefs.h:31
omnetpp::nedxml::NedResourceCache::lookupNedType
virtual std::string lookupNedType(const NedLookupContext &context, const char *nedTypeName)
Definition: nedresourcecache.h:192
omnetpp
Definition: astbuilder.h:25
omnetpp::nedxml::NedResourceCache::CachedTypeNames
Definition: nedresourcecache.h:70
omnetpp::nedxml::NedResourceCache::nedFiles
std::map< std::string, NedFileElement * > nedFiles
Definition: nedresourcecache.h:82
omnetpp::nedxml::NedResourceCache::nedTypeNames
std::vector< std::string > nedTypeNames
Definition: nedresourcecache.h:91
omnetpp::nedxml::NedResourceCache::CachedTypeNames::contains
virtual bool contains(const char *qname) const override
Definition: nedresourcecache.h:75
omnetpp::nedxml::NedLookupContext::NedLookupContext
NedLookupContext(ASTNode *e, const char *q)
Definition: nedresourcecache.h:41
omnetpp::nedxml::NedTypeInfo
Stores information on a NED type.
Definition: nedtypeinfo.h:43
omnetpp::nedxml::ASTNode
Definition: astnode.h:87
omnetpp::nedxml::NedResourceCache::INedTypeNames
Definition: nedresourcecache.h:56
omnetpp::nedxml::NedResourceCache::folderPackages
StringMap folderPackages
Definition: nedresourcecache.h:96
omnetpp::nedxml::NedResourceCache::CachedTypeNames::p
NedResourceCache * p
Definition: nedresourcecache.h:72
omnetpp::nedxml::ErrorStore
Definition: errorstore.h:37
omnetpp::nedxml::NedResourceCache::CachedTypeNames::get
virtual const char * get(int k) const override
Definition: nedresourcecache.h:77
omnetpp::nedxml::NedResourceCache::INedTypeNames::~INedTypeNames
virtual ~INedTypeNames()
Definition: nedresourcecache.h:67
omnetpp::nedxml::NedResourceCache::getTypeNames
virtual const std::vector< std::string > & getTypeNames() const
Definition: nedresourcecache.h:199
omnetpp::nedxml::NedResourceCache::nedTypes
std::map< std::string, NedTypeInfo * > nedTypes
Definition: nedresourcecache.h:88
omnetpp::nedxml::NedResourceCache::CachedTypeNames::CachedTypeNames
CachedTypeNames(NedResourceCache *p)
Definition: nedresourcecache.h:74