diff --git a/Core/include/ACTS/Layers/CylinderLayer.hpp b/Core/include/ACTS/Layers/CylinderLayer.hpp index 37539fa541927825fba6baae7a41f5d93a91af9a..b9775025f60e65aa44e1cbffd211e10398cf85c1 100644 --- a/Core/include/ACTS/Layers/CylinderLayer.hpp +++ b/Core/include/ACTS/Layers/CylinderLayer.hpp @@ -67,8 +67,10 @@ public: laytyp)); } + /// Factory for shared Layer pointer, that accepts @c variant_data + /// @param data The data to build from static MutableLayerPtr - create(const variant_data& data_); + create(const variant_data& data); /// Copy constructor - deleted CylinderLayer(const CylinderLayer& cla) = delete; @@ -93,6 +95,8 @@ public: CylinderSurface& surfaceRepresentation() override; + /// Produce a @c variant_data representation of this object + /// @return The representation variant_data toVariantData() const override; diff --git a/Core/include/ACTS/Layers/DiscLayer.hpp b/Core/include/ACTS/Layers/DiscLayer.hpp index 938cb6578182498ff58accfd4b224b0ac9590e68..46311996067a753dd917f811bf8a099e208b374a 100644 --- a/Core/include/ACTS/Layers/DiscLayer.hpp +++ b/Core/include/ACTS/Layers/DiscLayer.hpp @@ -64,6 +64,8 @@ public: laytyp)); } + /// Factory for shared Layer pointer, that accepts @c variant_data + /// @param data The data to build from static MutableLayerPtr create(const variant_data& data); @@ -90,6 +92,8 @@ public: DiscSurface& surfaceRepresentation() override; + /// Produce a @c variant_data representation of this object + /// @return The representation variant_data toVariantData() const override; diff --git a/Core/include/ACTS/Layers/NavigationLayer.hpp b/Core/include/ACTS/Layers/NavigationLayer.hpp index 38bc30f5741797a25e46cc0d1587076a6e119f50..ca19989ffa8abad663efd2ff5bff9a75aa4ca17d 100644 --- a/Core/include/ACTS/Layers/NavigationLayer.hpp +++ b/Core/include/ACTS/Layers/NavigationLayer.hpp @@ -43,6 +43,8 @@ public: return LayerPtr(new NavigationLayer(std::move(sRepresentation), thickness)); } + /// Factory for shared Layer pointer, that accepts @c variant_data + /// @param data The data to build from static LayerPtr create(const variant_data& data); @@ -98,6 +100,8 @@ public: bool resolve(bool, bool, bool) const final override; + /// Produce a @c variant_data representation of this object + /// @return The representation variant_data toVariantData() const; diff --git a/Core/include/ACTS/Surfaces/ConeBounds.hpp b/Core/include/ACTS/Surfaces/ConeBounds.hpp index cffe04069dc562b24ef7a51c3ae92078daef5822..9125bc43a862954c1fe89154b5f732cda9a39ca9 100644 --- a/Core/include/ACTS/Surfaces/ConeBounds.hpp +++ b/Core/include/ACTS/Surfaces/ConeBounds.hpp @@ -72,6 +72,8 @@ public: double halfphi = M_PI, double avphi = 0.); + /// Constructor which accepts @c variant_data + /// @param data The data to build from ConeBounds(const variant_data& data); virtual ~ConeBounds(); @@ -151,6 +153,8 @@ public: double halfPhiSector() const; + /// Produce a @c variant_data representation of this object + /// @return The representation variant_data toVariantData() const override; diff --git a/Core/include/ACTS/Utilities/VariantData.hpp b/Core/include/ACTS/Utilities/VariantData.hpp index dd326d22bd39b2d0ba28eec57a6bc14f147f9206..e4ef527b109eaf72422f067f4994ae24be4cef23 100644 --- a/Core/include/ACTS/Utilities/VariantData.hpp +++ b/Core/include/ACTS/Utilities/VariantData.hpp @@ -22,20 +22,16 @@ namespace Acts { -// typedef boost::make_recursive_variant< -// int, -// double, -// std::string, -// bool, -// std::map<std::string, boost::recursive_variant_>, -// std::vector<boost::recursive_variant_>>::type variant_data; - -// typedef std::map<std::string, gen_structure> variant_map; -// typedef std::vector<gen_structure> variant_vector; - +/// Forward declarations for the recursive definition to work. class variant_map; class variant_vector; +/// This data type allows construction of a tree-like +/// data structure that contains variant data, in particular +/// int, double, strings, bools, maps and vectors. The latter +/// two can also contain variant_data (this allows trees) +/// The definition here uses boost's recursive_wrapper, because +/// @c variant_map and @c variant_vector have to be defined later on. using variant_data = boost::variant<int, double, std::string, @@ -43,28 +39,50 @@ using variant_data = boost::variant<int, boost::recursive_wrapper<variant_map>, boost::recursive_wrapper<variant_vector>>; +/// @brief Class which wraps an @c std::map<std::string, variant_data> +/// +/// This class allows tree-like recursive data, and provides some +/// convenience methods to access keys. The key type is always @c std::string class variant_map { public: - using map_t = std::map<std::string, variant_data>; + /// Map type used by the class + using map_t = std::map<std::string, variant_data>; + using iterator = map_t::iterator; using const_iterator = map_t::const_iterator; + /// Constructore which takes a @c map_t and wraps it. + /// @param src The source map, empty by default variant_map(map_t src = {}) : m_map(src) {} + /// Method to get the size of the map. Is forwarded to the + /// contained map + /// @return The size size_t size() const { return m_map.size(); } + + /// Subscript operator to elements in the map + /// @param key The key to access + /// @return Value stored here. Note that this is @c variant_data. variant_data& operator[](std::string key) { return m_map[key]; } + /// Method to count a key. Is forwarded + /// @param key The key to count elements for + /// @return Number of elements found size_t count(const std::string& key) const { return m_map.count(key); } + /// Convenience method to access a key and transform it to a specific + /// type. This will throw an error if the key is not found. + /// @tparam T The type you want + /// @param key The key to look up in the map template <class T> T& get(const std::string& key) @@ -74,6 +92,10 @@ public: return boost::get<T>(m_map.at(key)); } + /// Convenience method to access a key and transform it to a specific + /// type. This will throw an error if the key is not found. + /// @tparam T The type you want + /// @param key The key to look up in the map template <class T> const T& get(const std::string& key) const @@ -83,27 +105,50 @@ public: return boost::get<T>(m_map.at(key)); } + /// Direct access to a key in the map, is forwarded + /// @param key The key to look up + /// @return value as @c variant_data const variant_data& at(const std::string& key) const { return m_map.at(key); } + /// Direct access to a key in the map, is forwarded + /// @param key The key to look up + /// @return value as @c variant_data + variant_data& + at(const std::string& key) + { + return m_map.at(key); + } + + /// Returns an iterator for the contained map, pointing to begin + /// @return Iterator pointing at begin iterator begin() { return m_map.begin(); } + + /// Returns an iterator for the contained map, pointing to end + /// @return Iterator pointing at end iterator end() { return m_map.end(); } + + /// Returns an const iterator for the contained map, pointing to begin + /// @return Const iterator pointing at begin const_iterator begin() const { return m_map.begin(); } + + /// Returns an const iterator for the contained map, pointing to end + /// @return Const iterator pointing at end const_iterator end() const { @@ -114,40 +159,64 @@ private: map_t m_map; }; +/// Class that wraps a vector of @c variant_data items +/// Provides some convenience methods for access. class variant_vector { public: + /// Vector type used by the class using vector_t = std::vector<variant_data>; using iterator = vector_t::iterator; using const_iterator = vector_t::const_iterator; + /// Constructor for the class accepting an input vector + /// @param src The source vector to wrap variant_vector(vector_t src = {}) : m_vector(src) {} + /// Method to get the current size of the wrapped vector + /// @return The size size_t size() const { return m_vector.size(); } + + /// Operator to get subscript access to an element. + /// @param idx The index to access + /// @return The value at idx, as @c variant_data variant_data& operator[](size_t idx) { return m_vector[idx]; } + /// Method to access value at an index + /// @param idx The index to access + /// @return The value at idx, as @c variant_data variant_data& at(size_t idx) { return m_vector.at(idx); } + /// Method to access value at an index + /// @param idx The index to access + /// @return The value at @c idx, as @c variant_data const variant_data& at(size_t idx) const { return m_vector.at(idx); } + /// Method to append an element to the vector + /// @param data @c variant_data value to add void push_back(const variant_data& data) { m_vector.push_back(data); } + /// Convenience method which accesses a value and converts it + /// to a provided type + /// @tparam T the type you want + /// @param idx The index to access + /// @return The value at @c idx as @c T template <class T> T& get(const size_t& idx) @@ -155,6 +224,11 @@ public: return boost::get<T>(m_vector.at(idx)); } + /// Convenience method which accesses a value and converts it + /// to a provided type + /// @tparam T the type you want + /// @param idx The index to access + /// @return The value at @c idx as @c T template <class T> const T& get(const size_t& idx) const @@ -162,21 +236,32 @@ public: return boost::get<T>(m_vector.at(idx)); } + /// Returns an iterator for the contained vector, pointing to begin + /// @return Iterator pointing at begin iterator begin() { return m_vector.begin(); } + + /// Returns an iterator for the contained vector, pointing to end + /// @return Iterator pointing at end iterator end() { return m_vector.end(); } + + /// Returns an const iterator for the contained vector, pointing to begin + /// @return Const iterator pointing at begin const_iterator begin() const { return m_vector.begin(); } + + /// Returns an const iterator for the contained vector, pointing to end + /// @return Const iterator pointing at end const_iterator end() const { @@ -187,15 +272,20 @@ private: vector_t m_vector; }; +/// This class implements a @c boost::static_vistor +/// which can turn a @c variant_data tree into a json string class variant_json_visitor : public boost::static_visitor<> { public: + /// Constructor for the visitor. + /// @param pretty_ Whether the output is indented. variant_json_visitor(bool pretty_ = false) : json_str(std::stringstream()), pretty(pretty_) { json_str << std::fixed << std::setprecision(30); } + /// Visitor operator overload void operator()(const bool& b) { @@ -206,24 +296,28 @@ public: } } + /// Visitor operator overload void operator()(const int& i) { json_str << i; } + /// Visitor operator overload void operator()(const double& d) { json_str << d; } + /// Visitor operator overload void operator()(const std::string& str) { json_str << "\"" << str << "\""; } + /// Visitor operator overload void operator()(const variant_map& map) { @@ -250,6 +344,7 @@ public: json_str << "}"; } + /// Visitor operator overload void operator()(const variant_vector& vec) { @@ -273,6 +368,8 @@ public: json_str << "]"; } + /// Method to get the resulting json string + /// @return The json string std::string str() const { @@ -295,6 +392,10 @@ private: } }; +/// Function to apply the @c variant_json_visitor to a @c variant_data +/// instance. This turns the input into a json string and returns it. +/// @param data The input data to serialize +/// @param pretty Whether to indent the output inline std::string to_json(const variant_data& data, bool pretty = false) { @@ -303,6 +404,8 @@ to_json(const variant_data& data, bool pretty = false) return jv.str(); } +/// Operator overload for the stream operator. This allows +/// printing of @c variant_data instances. inline std::ostream& operator<<(std::ostream& os, const variant_data& data) { @@ -310,7 +413,10 @@ operator<<(std::ostream& os, const variant_data& data) return os; } -inline variant_map +/// Function to turn a @c Vector2D into @c variant_data +/// @param vec The vector to transform +/// @return The @c variant_data instance +inline variant_data to_variant(const Vector2D& vec) { using namespace std::string_literals; @@ -320,15 +426,17 @@ to_variant(const Vector2D& vec) return data; } -inline variant_map +/// Function to turn a @c Transform3D into @c variant_data +/// @param vec The vector to transform +/// @return The @c variant_data instance +inline variant_data to_variant(const Transform3D& trf) { using namespace std::string_literals; variant_map data; data["type"] = "Transform3D"s; - variant_map payload; - // payload["matrix"] = to_variant(trf.matrix()); + variant_map payload; variant_vector matrix_data; for (size_t i = 0; i < 4; i++) { for (size_t j = 0; j < 4; j++) { @@ -341,7 +449,10 @@ to_variant(const Transform3D& trf) return data; } -inline variant_map +/// Function to turn a @c ActsMatrix into @c variant_data +/// @param vec The vector to transform +/// @return The @c variant_data instance +inline variant_data to_variant(const ActsMatrixD<4, 4>& matrix) { using namespace std::string_literals; @@ -365,10 +476,16 @@ to_variant(const ActsMatrixD<4, 4>& matrix) return data; } +/// Function to produce an instance of type @c T from @c variant_data. +/// Thie is the unimplemented base templated that is specialized +/// for various types. +/// @tparam T The type you want template <typename T> inline T from_variant(const variant_data& data_); +/// Function to produce a @c Transform3D from @c variant_data. +/// @param data_ The input @c variant_data template <> inline Transform3D from_variant<Transform3D>(const variant_data& data_) @@ -394,6 +511,8 @@ from_variant<Transform3D>(const variant_data& data_) return trf; } +/// Function to produce a @c Transform3D from @c variant_data. +/// @param data_ The input @c variant_data template <> inline Vector2D from_variant<Vector2D>(const variant_data& data_) @@ -413,26 +532,6 @@ from_variant<Vector2D>(const variant_data& data_) return vec; } -/*int -main() -{ - using namespace std::string_literals; - - auto data = gen_map(); - data["kint"] = 5; - data["kdouble"] = 5.3; - data["kstring"] = "whoop"s; - data["kbool"] = true; - data["kvector"] = gen_vector({"a"s, 8, "c"s}); - data["kmap"] = gen_map( - {{"h", 17}, {"l", "hurz"s}, {"b", false}, {"w", gen_map({{"m", -1}})}}); - - gen_structure root = data; - - std::cout << std::endl << std::endl; - std::cout << root << std::endl; -}*/ - } // namespace Acts #endif