lua_parameter_dictionary.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_COMMON_LUA_PARAMETER_DICTIONARY_H_
18 #define CARTOGRAPHER_COMMON_LUA_PARAMETER_DICTIONARY_H_
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
27 #include "glog/logging.h"
28 
29 namespace cartographer {
30 namespace common {
31 
32 // Resolves file paths and file content for the Lua 'read' and 'include'
33 // functions. Use this to configure where those functions load other files from.
34 class FileResolver {
35  public:
36  virtual ~FileResolver() {}
37  virtual string GetFullPathOrDie(const string& basename) = 0;
38  virtual string GetFileContentOrDie(const string& basename) = 0;
39 };
40 
41 // A parameter dictionary that gets loaded from Lua code.
43  public:
44  // Constructs the dictionary from a Lua Table specification.
45  LuaParameterDictionary(const string& code,
46  std::unique_ptr<FileResolver> file_resolver);
47 
49  LuaParameterDictionary& operator=(const LuaParameterDictionary&) = delete;
50 
51  // Constructs a LuaParameterDictionary without reference counting.
52  static std::unique_ptr<LuaParameterDictionary> NonReferenceCounted(
53  const string& code, std::unique_ptr<FileResolver> file_resolver);
54 
56 
57  // Returns all available keys.
58  std::vector<string> GetKeys() const;
59 
60  // Returns true if the key is in this dictionary.
61  bool HasKey(const string& key) const;
62 
63  // These methods CHECK() that the 'key' exists.
64  string GetString(const string& key);
65  double GetDouble(const string& key);
66  int GetInt(const string& key);
67  bool GetBool(const string& key);
68  std::unique_ptr<LuaParameterDictionary> GetDictionary(const string& key);
69 
70  // Gets an int from the dictionary and CHECK()s that it is non-negative.
71  int GetNonNegativeInt(const string& key);
72 
73  // Returns a string representation for this LuaParameterDictionary.
74  string ToString() const;
75 
76  // Returns the values of the keys '1', '2', '3' as the given types.
77  std::vector<double> GetArrayValuesAsDoubles();
78  std::vector<string> GetArrayValuesAsStrings();
79  std::vector<std::unique_ptr<LuaParameterDictionary>>
80  GetArrayValuesAsDictionaries();
81 
82  private:
83  enum class ReferenceCount { YES, NO };
84  LuaParameterDictionary(const string& code, ReferenceCount reference_count,
85  std::unique_ptr<FileResolver> file_resolver);
86 
87  // For GetDictionary().
88  LuaParameterDictionary(lua_State* L, ReferenceCount reference_count,
89  std::shared_ptr<FileResolver> file_resolver);
90 
91  // Function that recurses to keep track of indent for ToString().
92  string DoToString(const string& indent) const;
93 
94  // Pop the top of the stack and CHECKs that the type is correct.
95  double PopDouble() const;
96  int PopInt() const;
97  bool PopBool() const;
98 
99  // Pop the top of the stack and CHECKs that it is a string. The returned value
100  // is either quoted to be suitable to be read back by a Lua interpretor or
101  // not.
102  enum class Quoted { YES, NO };
103  string PopString(Quoted quoted) const;
104 
105  // Creates a LuaParameterDictionary from the Lua table at the top of the
106  // stack, either with or without reference counting.
107  std::unique_ptr<LuaParameterDictionary> PopDictionary(
108  ReferenceCount reference_count) const;
109 
110  // CHECK() that 'key' is in the dictionary.
111  void CheckHasKey(const string& key) const;
112 
113  // CHECK() that 'key' is in this dictionary and reference it as being used.
114  void CheckHasKeyAndReference(const string& key);
115 
116  // If desired, this can be called in the destructor of a derived class. It
117  // will CHECK() that all keys defined in the configuration have been used
118  // exactly once and resets the reference counter.
119  void CheckAllKeysWereUsedExactlyOnceAndReset();
120 
121  // Reads a file into a Lua string.
122  static int LuaRead(lua_State* L);
123 
124  // Handles inclusion of other Lua files and prevents double inclusion.
125  static int LuaInclude(lua_State* L);
126 
127  lua_State* L_; // The name is by convention in the Lua World.
129 
130  // This is shared with all the sub dictionaries.
131  const std::shared_ptr<FileResolver> file_resolver_;
132 
133  // If true will check that all keys were used on destruction.
135 
136  // This is modified with every call to Get* in order to verify that all
137  // parameters are read exactly once.
138  std::map<string, int> reference_counts_;
139 
140  // List of all included files in order of inclusion. Used to prevent double
141  // inclusion.
142  std::vector<string> included_files_;
143 };
144 
145 } // namespace common
146 } // namespace cartographer
147 
148 #endif // CARTOGRAPHER_COMMON_LUA_PARAMETER_DICTIONARY_H_
const std::shared_ptr< FileResolver > file_resolver_
virtual string GetFileContentOrDie(const string &basename)=0
virtual string GetFullPathOrDie(const string &basename)=0


cartographer
Author(s):
autogenerated on Wed Jun 5 2019 21:57:58