OMNeT++ NEDXML  6.0.3
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  // true after doneLoadingNedFiles() has been called
88  bool doneLoadingNedFilesCalled = false;
89 
90  // table of NED type declarations; key is fully qualified name, and elements point into nedFiles
91  std::map<std::string, NedTypeInfo*> nedTypes;
92 
93  // cached keys of the nedTypes map, for getTypeNames(); zero size means out of date
94  mutable std::vector<std::string> nedTypeNames;
95 
96  // maps the loaded source NED folders (as absolute paths, canonical representation)
97  // to package names
98  typedef std::map<std::string,std::string> StringMap;
100 
101  struct PendingNedType {
102  std::string qname;
105  PendingNedType(const char *q, bool inner, ASTNode *e) {qname=q;isInnerType=inner;node=e;}
106  };
107 
108  // storage for NED components not resolved yet because of missing dependencies
109  std::vector<PendingNedType> pendingList;
110 
111  protected:
112  virtual void addFile(const char *fname, NedFileElement *node);
113  virtual void registerBuiltinDeclarations();
114  virtual int doLoadNedSourceFolder(const char *foldername, const char *expectedPackage, const std::vector<std::string>& excludedFolders);
115  virtual void doLoadNedFileOrText(const char *nedfname, const char *nedtext, const char *expectedPackage, bool isXML);
116  virtual NedFileElement *parseAndValidateNedFileOrText(const char *nedfname, const char *nedtext, bool isXML);
117  virtual std::string determineRootPackageName(const char *nedSourceFolderName);
118  virtual std::string getNedSourceFolderForFolder(const char *folder) const;
119  virtual void collectNedTypesFrom(ASTNode *node, const std::string& packagePrefix, bool areInnerTypes);
120  virtual void collectNedType(const char *qname, bool isInnerType, ASTNode *node);
121  virtual bool areDependenciesResolved(const char *qname, ASTNode *node);
122  virtual void registerPendingNedTypes();
123  virtual void registerNedType(const char *qname, bool isInnerType, ASTNode *node);
124  virtual std::string getFirstError(ErrorStore *errors, const char *prefix=nullptr);
125 
126  public:
129 
131  virtual ~NedResourceCache();
132 
145  virtual int loadNedSourceFolder(const char *foldername, const char *excludedPackages);
146 
154  virtual void loadNedFile(const char *nedfname, const char *expectedPackage, bool isXML);
155 
166  virtual void loadNedText(const char *name, const char *nedtext, const char *expectedPackage, bool isXML);
167 
173  virtual void doneLoadingNedFiles();
174 
179  std::vector<NedFileElement*> getPackageNedListForLookup(const char *packageName) const;
180 
182  virtual NedTypeInfo *lookup(const char *qname) const;
183 
185  virtual NedTypeInfo *getDecl(const char *qname) const;
186 
188  virtual std::string resolveNedType(const NedLookupContext& context, const char *nedtypename, INedTypeNames *qnames);
189 
191  virtual std::string resolveNedType(const NedLookupContext& context, const char *nedtypename) {
192  CachedTypeNames names(this);
193  return resolveNedType(context, nedtypename, &names);
194  }
195 
197  virtual const std::vector<std::string>& getTypeNames() const;
198 
203  virtual std::string getNedPackageForFolder(const char *folder) const;
204 
208  static NedLookupContext getParentContextOf(const char *qname, ASTNode *node);
209 
210 };
211 
212 } // namespace nedxml
213 } // namespace omnetpp
214 
215 
216 #endif
217 
omnetpp::nedxml::NedResourceCache
Stores loaded NED files, and keeps track of components in them.
Definition: nedresourcecache.h:52
omnetpp::nedxml::NedResourceCache::PendingNedType::isInnerType
bool isInnerType
Definition: nedresourcecache.h:103
omnetpp::nedxml::NedResourceCache::PendingNedType::PendingNedType
PendingNedType(const char *q, bool inner, ASTNode *e)
Definition: nedresourcecache.h:105
omnetpp::nedxml::NedResourceCache::resolveNedType
virtual std::string resolveNedType(const NedLookupContext &context, const char *nedtypename)
Definition: nedresourcecache.h:191
omnetpp::nedxml::NedResourceCache::PendingNedType::qname
std::string qname
Definition: nedresourcecache.h:102
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:98
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:128
omnetpp::nedxml::NedResourceCache::lookup
virtual NedTypeInfo * lookup(const char *qname) const
NEDXML_API
#define NEDXML_API
Definition: nedxmldefs.h:31
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:94
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::PendingNedType::node
ASTNode * node
Definition: nedresourcecache.h:104
omnetpp::nedxml::NedResourceCache::folderPackages
StringMap folderPackages
Definition: nedresourcecache.h:99
omnetpp::nedxml::NedResourceCache::CachedTypeNames::p
NedResourceCache * p
Definition: nedresourcecache.h:72
omnetpp::nedxml::NedResourceCache::PendingNedType
Definition: nedresourcecache.h:101
omnetpp::nedxml::NedResourceCache::pendingList
std::vector< PendingNedType > pendingList
Definition: nedresourcecache.h:109
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
omnetpp::nedxml::NedResourceCache::nedTypes
std::map< std::string, NedTypeInfo * > nedTypes
Definition: nedresourcecache.h:91
omnetpp::nedxml::NedResourceCache::CachedTypeNames::CachedTypeNames
CachedTypeNames(NedResourceCache *p)
Definition: nedresourcecache.h:74