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 std::string GetFullPathOrDie(const std::string& basename) = 0;
38  virtual std::string GetFileContentOrDie(const std::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 std::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 std::string& code, std::unique_ptr<FileResolver> file_resolver);
54 
56 
57  // Returns all available keys.
58  std::vector<std::string> GetKeys() const;
59 
60  // Returns true if the key is in this dictionary.
61  bool HasKey(const std::string& key) const;
62 
63  // These methods CHECK() that the 'key' exists.
64  std::string GetString(const std::string& key);
65  double GetDouble(const std::string& key);
66  int GetInt(const std::string& key);
67  bool GetBool(const std::string& key);
68  std::unique_ptr<LuaParameterDictionary> GetDictionary(const std::string& key);
69 
70  // Gets an int from the dictionary and CHECK()s that it is non-negative.
71  int GetNonNegativeInt(const std::string& key);
72 
73  // Returns a string representation for this LuaParameterDictionary.
74  std::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<std::string> GetArrayValuesAsStrings();
79  std::vector<std::unique_ptr<LuaParameterDictionary>>
80  GetArrayValuesAsDictionaries();
81 
82  private:
83  enum class ReferenceCount { YES, NO };
84  LuaParameterDictionary(const std::string& code,
85  ReferenceCount reference_count,
86  std::unique_ptr<FileResolver> file_resolver);
87 
88  // For GetDictionary().
89  LuaParameterDictionary(lua_State* L, ReferenceCount reference_count,
90  std::shared_ptr<FileResolver> file_resolver);
91 
92  // Function that recurses to keep track of indent for ToString().
93  std::string DoToString(const std::string& indent) const;
94 
95  // Pop the top of the stack and CHECKs that the type is correct.
96  double PopDouble() const;
97  int PopInt() const;
98  bool PopBool() const;
99 
100  // Pop the top of the stack and CHECKs that it is a string. The returned value
101  // is either quoted to be suitable to be read back by a Lua interpretor or
102  // not.
103  enum class Quoted { YES, NO };
104  std::string PopString(Quoted quoted) const;
105 
106  // Creates a LuaParameterDictionary from the Lua table at the top of the
107  // stack, either with or without reference counting.
108  std::unique_ptr<LuaParameterDictionary> PopDictionary(
109  ReferenceCount reference_count) const;
110 
111  // CHECK() that 'key' is in the dictionary.
112  void CheckHasKey(const std::string& key) const;
113 
114  // CHECK() that 'key' is in this dictionary and reference it as being used.
115  void CheckHasKeyAndReference(const std::string& key);
116 
117  // If desired, this can be called in the destructor of a derived class. It
118  // will CHECK() that all keys defined in the configuration have been used
119  // exactly once and resets the reference counter.
120  void CheckAllKeysWereUsedExactlyOnceAndReset();
121 
122  // Reads a file into a Lua string.
123  static int LuaRead(lua_State* L);
124 
125  // Handles inclusion of other Lua files and prevents double inclusion.
126  static int LuaInclude(lua_State* L);
127 
128  lua_State* L_; // The name is by convention in the Lua World.
130 
131  // This is shared with all the sub dictionaries.
132  const std::shared_ptr<FileResolver> file_resolver_;
133 
134  // If true will check that all keys were used on destruction.
136 
137  // This is modified with every call to Get* in order to verify that all
138  // parameters are read exactly once.
139  std::map<std::string, int> reference_counts_;
140 
141  // List of all included files in order of inclusion. Used to prevent double
142  // inclusion.
143  std::vector<std::string> included_files_;
144 };
145 
146 } // namespace common
147 } // namespace cartographer
148 
149 #endif // CARTOGRAPHER_COMMON_LUA_PARAMETER_DICTIONARY_H_
virtual std::string GetFileContentOrDie(const std::string &basename)=0
const std::shared_ptr< FileResolver > file_resolver_
virtual std::string GetFullPathOrDie(const std::string &basename)=0


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58