From 6dbb4d19a860937ec1c78df01b1371272e1de8de Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 23 Jan 2015 12:08:42 +0100 Subject: Improved SourceLocation class. --- src/core/common/Location.cpp | 9 +++++++++ src/core/common/Location.hpp | 29 +++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) (limited to 'src/core/common') diff --git a/src/core/common/Location.cpp b/src/core/common/Location.cpp index 6f9250a..7a75347 100644 --- a/src/core/common/Location.cpp +++ b/src/core/common/Location.cpp @@ -19,5 +19,14 @@ #include "Location.hpp" namespace ousia { + +/* Global Functions */ + +const SourceLocation NullSourceLocation; + +void SourceContext NullSourceContextCallback(const SourceLocation &location) +{ + return SourceContext{}; +} } diff --git a/src/core/common/Location.hpp b/src/core/common/Location.hpp index 4ce01a8..57892cc 100644 --- a/src/core/common/Location.hpp +++ b/src/core/common/Location.hpp @@ -29,6 +29,7 @@ #define _OUSIA_LOCATION_HPP_ #include +#include #include #include @@ -272,7 +273,7 @@ public: /** * Default constructor. */ - SourceLocation() : sourceId(InvalidSourceId) {}; + SourceLocation() : sourceId(InvalidSourceId){}; /** * Constructor, binds the SourceLocation to the given source file. @@ -346,6 +347,11 @@ public: } }; +/** + * NullSourceLocation is an empty SourceLocation instance. + */ +extern const SourceLocation NullSourceLocation; + /** * Represents the context of a SourceLocation instance. Used to build error * messages. @@ -435,6 +441,11 @@ struct SourceContext { */ bool isValid() const { return range.isValid() && hasLine() && hasColumn(); } + /** + * Returns true if a valid (non-empty) filename is set. + */ + bool hasFile() const { return !filename.empty(); } + /** * Returns true, if the start line number is valid, false otherwise. * @@ -455,10 +466,20 @@ struct SourceContext { * location. * * @param location is the location for which the context should be looked up. - * @param data is used defined data associated with the callback. + * @return the corresponding SourceContext. */ -using SourceContextCallback = SourceContext (*)(const SourceLocation &location, - void *data); +using SourceContextCallback = + std::function; + +/** + * Function to be used as default value for the SourceContextCallback. Returns + * an invalid SourceContext. + * + * @param location is the location for which the context should be looked up. + * @return an empty, invalid SourceContext. + */ +void SourceContext NullSourceContextCallback(const SourceLocation &location); + } #endif /* _OUSIA_LOCATION_HPP_ */ -- cgit v1.2.3