OMNeT++ NEDXML  5.4.1
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  typedef std::map<std::string, ASTNode *> NedFileMap;
82  typedef std::map<std::string, NedTypeInfo *> NedTypeInfoMap;
83 
84  // table of loaded NED files; maps file name to NED element.
85  NedFileMap files;
86 
87  // table of NED type declarations; key is fully qualified name, and
88  // elements point into the files map
89  NedTypeInfoMap nedTypes;
90 
91  // cached keys of the nedTypes map, for getTypeNames(); zero size means out of date
92  mutable std::vector<std::string> nedTypeNames;
93 
94  // maps the loaded source NED folders (as absolute paths, canonical representation)
95  // to package names
96  typedef std::map<std::string,std::string> StringMap;
97  StringMap folderPackages;
98 
99  struct PendingNedType {
100  std::string qname;
103  PendingNedType(const char *q, bool inner, ASTNode *e) {qname=q;isInnerType=inner;node=e;}
104  };
105 
106  // storage for NED components not resolved yet because of missing dependencies
107  std::vector<PendingNedType> pendingList;
108 
109  protected:
110  virtual void registerBuiltinDeclarations();
111  virtual int doLoadNedSourceFolder(const char *foldername, const char *expectedPackage);
112  virtual void doLoadNedFileOrText(const char *nedfname, const char *nedtext, const char *expectedPackage, bool isXML);
113  virtual ASTNode *parseAndValidateNedFileOrText(const char *nedfname, const char *nedtext, bool isXML);
114  virtual std::string determineRootPackageName(const char *nedSourceFolderName);
115  virtual std::string getNedSourceFolderForFolder(const char *folder) const;
116  virtual void collectNedTypesFrom(ASTNode *node, const std::string& packagePrefix, bool areInnerTypes);
117  virtual void collectNedType(const char *qname, bool isInnerType, ASTNode *node);
118  virtual bool areDependenciesResolved(const char *qname, ASTNode *node);
119  virtual void registerPendingNedTypes();
120  virtual void registerNedType(const char *qname, bool isInnerType, ASTNode *node);
121  virtual std::string getFirstError(ErrorStore *errors, const char *prefix=nullptr);
122 
123  public:
126 
128  virtual ~NedResourceCache();
129 
139  virtual int loadNedSourceFolder(const char *foldername);
140 
148  virtual void loadNedFile(const char *nedfname, const char *expectedPackage, bool isXML);
149 
160  virtual void loadNedText(const char *name, const char *nedtext, const char *expectedPackage, bool isXML);
161 
167  virtual void doneLoadingNedFiles();
168 
174  virtual bool addFile(const char *fname, ASTNode *node); //XXX make protected?
175 
177  virtual ASTNode *getFile(const char *fname) const;
178 
185  virtual NedFileElement *getParentPackageNedFile(NedFileElement *nedfile) const;
186 
188  virtual NedTypeInfo *lookup(const char *qname) const;
189 
191  virtual NedTypeInfo *getDecl(const char *qname) const;
192 
194  virtual std::string resolveNedType(const NedLookupContext& context, const char *nedtypename, INedTypeNames *qnames);
195 
197  virtual std::string resolveNedType(const NedLookupContext& context, const char *nedtypename) {
198  CachedTypeNames names(this);
199  return resolveNedType(context, nedtypename, &names);
200  }
201 
203  virtual const std::vector<std::string>& getTypeNames() const;
204 
209  virtual std::string getNedPackageForFolder(const char *folder) const;
210 
214  static NedLookupContext getParentContextOf(const char *qname, ASTNode *node);
215 
216 };
217 
218 } // namespace nedxml
219 } // namespace omnetpp
220 
221 
222 #endif
223 
ASTNode * element
Definition: nedresourcecache.h:39
#define NEDXML_API
Definition: nedxmldefs.h:31
Context of NED type lookup, for NedResourceCache.
Definition: nedresourcecache.h:37
Definition: nedresourcecache.h:99
virtual NedTypeInfo * lookup(const char *qname) const
std::map< std::string, std::string > StringMap
Definition: nedresourcecache.h:96
NedFileMap files
Definition: nedresourcecache.h:85
virtual bool contains(const char *qname) const override
Definition: nedresourcecache.h:75
bool isInnerType
Definition: nedresourcecache.h:101
std::vector< std::string > nedTypeNames
Definition: nedresourcecache.h:92
virtual int size() const override
Definition: nedresourcecache.h:76
std::string qname
Definition: nedresourcecache.h:100
PendingNedType(const char *q, bool inner, ASTNode *e)
Definition: nedresourcecache.h:103
virtual std::string resolveNedType(const NedLookupContext &context, const char *nedtypename)
Definition: nedresourcecache.h:197
NedResourceCache * p
Definition: nedresourcecache.h:72
NedTypeInfoMap nedTypes
Definition: nedresourcecache.h:89
virtual ~INedTypeNames()
Definition: nedresourcecache.h:67
virtual const std::vector< std::string > & getTypeNames() const
Stores loaded NED files, and keeps track of components in them.
Definition: nedresourcecache.h:52
std::map< std::string, ASTNode * > NedFileMap
Definition: nedresourcecache.h:81
Stores information on a NED type.
Definition: nedtypeinfo.h:43
ASTNode * node
Definition: nedresourcecache.h:102
NedLookupContext(ASTNode *e, const char *q)
Definition: nedresourcecache.h:41
Definition: astbuilder.h:25
CachedTypeNames(NedResourceCache *p)
Definition: nedresourcecache.h:74
std::vector< PendingNedType > pendingList
Definition: nedresourcecache.h:107
std::map< std::string, NedTypeInfo * > NedTypeInfoMap
Definition: nedresourcecache.h:82
Definition: astnode.h:75
Definition: errorstore.h:37
StringMap folderPackages
Definition: nedresourcecache.h:97
std::string qname
Definition: nedresourcecache.h:40
Definition: nedresourcecache.h:56