nedresourcecache.h Source File

NEDXML API: nedresourcecache.h Source File
NEDXML API
nedresourcecache.h
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
27namespace omnetpp {
28namespace nedxml {
29
30class ErrorStore;
31
37struct NedLookupContext
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
52class NEDXML_API NedResourceCache
53{
54 public:
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:
74 CachedTypeNames(NedResourceCache *p) {this->p=p;}
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;
96 StringMap folderPackages;
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
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
Definition astnode.h:88
Definition errorstore.h:38
Definition nedresourcecache.h:56
virtual bool contains(const char *qname) const =0
virtual const char * get(int k) const =0
Stores loaded NED files, and keeps track of components in them.
Definition nedresourcecache.h:53
static NedLookupContext getParentContextOf(const char *qname, ASTNode *node)
virtual NedTypeInfo * getDecl(const char *qname)
virtual std::string lookupNedType(const NedLookupContext &context, const char *nedTypeName)
Definition nedresourcecache.h:192
std::vector< NedFileElement * > getPackageNedListForLookup(const char *packageName) const
virtual void loadNedText(const char *name, const char *nedtext, const char *expectedPackage, bool isXML)
virtual std::string lookupNedType(const NedLookupContext &context, const char *nedTypeName, const INedTypeNames &amongQNames)
virtual int loadNedSourceFolder(const char *foldername, const char *excludedPackages)
NedResourceCache()
Definition nedresourcecache.h:114
virtual NedTypeInfo * lookup(const char *qname) const
virtual const std::vector< std::string > & getTypeNames() const
Definition nedresourcecache.h:199
virtual std::string getNedPackageForFolder(const char *folder) const
virtual void loadNedFile(const char *nedfname, const char *expectedPackage, bool isXML)
Stores information on a NED type.
Definition nedtypeinfo.h:45
Context of NED type lookup, for NedResourceCache.
Definition nedresourcecache.h:38