cppx-core
Abstract_source_location.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 //
3 // This astract class is used by cppx::fail. It can't use things that use cppx::fail.
4 // Line and character position indexing is 1-based.
5 
6 #include <cppx-core/collections/is_empty.hpp> // cppx::is_empty
7 #include <cppx-core/language/syntax/macro-with.hpp> // CPPX_WITH
8 #include <cppx-core/text/data/Symbol_strings.hpp> // cppx::best_effort::*
9 
10 #include <string> // std::string
11 #include <string_view> // std::string_view
12 
13 namespace cppx
14 {
15  CPPX_USE_STD( string, string_view, to_string );
16 
18  {
19  static // Uses simple straight ASCII quotes if CPPX_ASCII_PLEASE is defined.
20  inline auto quoted( const string& s )
21  -> string
22  {
23  string result;
24 
26  result += s;
28  return result;
29  }
30 
31  public:
32  virtual auto filename() const -> string = 0; // Empty if none
33  virtual auto linenumber() const -> int = 0; // 0 if none.
34  virtual auto character_position() const -> int = 0; // 0 if none.
35  virtual auto function_name() const -> string = 0; // Empty if none.
36 
38  -> string
39  {
40  string name = function_name();
41  return is_empty( name )? string( "<unspecified function>" ) : move( name );
42  }
43 
44  auto file_and_line() const
45  -> string
46  {
47  return string()
48  + "File " + quoted( filename() ) + " at line " + to_string( linenumber() );
49  }
50 
51  auto file_line_and_pos() const
52  -> string
53  {
54  const int pos = character_position();
55  string s = file_and_line();
56  s += " position ";
57  if( pos <= 0 )
58  {
59  s += "<unspecified position>";
60  }
61  else
62  {
63  s += to_string( pos );
64  }
65  return s;
66  }
67 
68  auto failpoint_spec() const
69  -> string
70  {
71  return file_and_line() + ", in function " + function_name_or_unspecified();
72  }
73 
74  operator string() const { return failpoint_spec(); }
75 
77  };
78 
79  inline auto to_string( const Abstract_source_location& loc )
80  -> string
81  { return loc.operator string(); }
82 
83 } // namespace cppx
$with binds the specified declarator to auto&& _ in the following braces block.
virtual auto function_name() const -> string=0
auto to_string(const Abstract_source_location &loc) -> string
CPPX_USE_STD(basic_string, basic_string_view, bitset, char_traits, size)
auto function_name_or_unspecified() const -> string
virtual auto linenumber() const -> int=0
constexpr auto & left_quote_str
virtual auto filename() const -> string=0
auto is_empty(const Collection &c) -> Truth
Definition: is_empty.hpp:33
virtual auto character_position() const -> int=0
constexpr auto & right_quote_str