GEOS
DataContext.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_DATAREPOSITORY_DATACONTEXT_HPP_
21 #define GEOS_DATAREPOSITORY_DATACONTEXT_HPP_
22 
23 #include "common/DataTypes.hpp"
24 #include "common/logger/Logger.hpp"
25 #include "xmlWrapper.hpp"
26 #include "common/format/Format.hpp"
28 
29 namespace geos
30 {
31 namespace dataRepository
32 {
33 
34 
44 {
45 public:
46 
51  DataContext( string_view targetName );
52 
56  virtual ~DataContext() {}
57 
62  virtual string toString() const = 0;
63 
69 
74  explicit operator ErrorLogger::ErrorContext() const {
75  return getContextInfo();
76  }
77 
81  string getTargetName() const
82  { return m_targetName; }
86  friend std::ostream & operator<<( std::ostream & os, const DataContext & ctx );
87 
88 protected:
89  // GroupContext & WrapperContext are friend class to be able to access to the protected method on other instances.
90  friend class GroupContext;
91  friend class WrapperContext;
92 
94  string const m_targetName;
95 
98  struct ToStringInfo
99  {
101  string m_targetName;
103  string m_filePath;
106 
113  ToStringInfo( string_view targetName, string_view filePath, size_t line );
118  ToStringInfo( string_view targetName );
122  bool hasInputFileInfo() const
123  { return !m_filePath.empty() && m_line != xmlWrapper::xmlDocument::npos; }
124  };
125 
131  virtual ToStringInfo getToStringInfo() const = 0;
132 
133 };
134 
141 class DataFileContext final : public DataContext
142 {
143 public:
144 
150  DataFileContext( xmlWrapper::xmlNode const & targetNode, xmlWrapper::xmlNodePos const & nodePos );
158  xmlWrapper::xmlAttributePos const & attPos );
159 
166  DataFileContext( string_view targetName, string_view file, size_t line );
167 
171  string toString() const override;
172 
178 
182  string getTypeName() const
183  { return m_typeName; }
184 
188  string getFilePath() const
189  { return m_filePath; }
190 
194  size_t getLine() const
195  { return m_line; }
196 
201  size_t getOffsetInLine() const
202  { return m_offsetInLine; }
203 
208  size_t getOffset() const
209  { return m_offset; }
210 
211 private:
212 
214  string const m_typeName;
216  string const m_filePath;
218  size_t const m_line;
220  size_t const m_offsetInLine;
222  size_t const m_offset;
223 
227  ToStringInfo getToStringInfo() const override;
228 
229 };
230 
231 
232 } /* namespace dataRepository */
233 } /* namespace geos */
234 
235 
236 
241 template<>
242 struct GEOS_FMT_NS::formatter< geos::dataRepository::DataContext > : GEOS_FMT_NS::formatter< std::string >
243 {
250  auto format( geos::dataRepository::DataContext const & dataContext, format_context & ctx ) const
251  {
252  return GEOS_FMT_NS::formatter< std::string >::format( dataContext.toString(), ctx );
253  }
254 };
255 
256 #endif /* GEOS_DATAREPOSITORY_DATACONTEXT_HPP_ */
DataContext(string_view targetName)
Construct a new DataContext object.
virtual ToStringInfo getToStringInfo() const =0
This method exposes the raw data of a DataContext, in order to access and format it (notably in toStr...
virtual string toString() const =0
virtual ErrorLogger::ErrorContext getContextInfo() const =0
Returns contextual information, including the file name and the line number.
friend std::ostream & operator<<(std::ostream &os, const DataContext &ctx)
Insert contextual information in the provided stream.
virtual ~DataContext()
Destroy the DataContext object.
Definition: DataContext.hpp:56
DataFileContext(string_view targetName, string_view file, size_t line)
Constructs the file context of a Group from a C++ source file.
DataFileContext(xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlAttribute const &att, xmlWrapper::xmlAttributePos const &attPos)
Construct the file context of a Group from an xml node.
DataFileContext(xmlWrapper::xmlNode const &targetNode, xmlWrapper::xmlNodePos const &nodePos)
Construct the file context of a Group from an xml node.
string toString() const override
ErrorLogger::ErrorContext getContextInfo() const override
Return contextual information (file and line of the input file where the error occured)
static constexpr size_t npos
Error value for when an offset / line position is undefined.
Definition: xmlWrapper.hpp:150
pugi::xml_node xmlNode
Definition: xmlWrapper.hpp:59
pugi::xml_attribute xmlAttribute
Definition: xmlWrapper.hpp:64
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
auto format(geos::dataRepository::DataContext const &dataContext, format_context &ctx) const
Format the specified DataContext to a string.
size_t m_line
the file line of the DataFileContext, if it exists (an empty string otherwise)
ToStringInfo(string_view targetName, string_view filePath, size_t line)
Construct a new ToStringInfo object from a DataContext that has input file info.
ToStringInfo(string_view targetName)
Construct a new ToStringInfo object from a DataContext that has no input file info.
string m_filePath
the file path of the DataFileContext, if it exists (an empty string otherwise)
string m_targetName
the targetName of the DataContext