This is a base class for ALib built-in formatters. The class implements abstract method Formatter::format and introduces a set of new abstract methods that have to be implemented by descendants.
Derived types need to set default values for attributes in fields DefaultNumberFormat and AlternativeNumberFormat within their constructor once - according to defaults specified by the formatting syntax. This should not be repeated per format operation. This way users of the type are allowed to change such default setting (even if they this may the formatter deviates from the standard it implements).
All values aggregated in member placeholder, together comprise the set of formatting attributes which can be modified by placeholder semantics of the format string. Derived types might use extended attributes. Implementations provided with ALib define such extended attributes using a corresponding additional inner type.
When parsing a placeholder of a format string, abstract method parsePlaceholder may set field FormatSpec to reflect a format-specific portion the placeholder string. It will be checked if the argument supports box-function FFormat, and if so, this string is passed to the box-function. If the argument does not support the interface, method parseStdFormatSpec is invoked to now parse this portion of the placeholder string in a default way.
This concept allows customized format specifications for custom argument types! As an example, a format specification for date/time argument types may support a custom format string like "yyyy-MM-dd HH:mm:ss"
.
The following describes the formatting process in detail (the implementation of method format) and this way helps to understand what is required from the implementation of the abstract methods:
0
.pha
(or extended attributes of a derived formatter type).-1
for the index to indicate auto-indexing. -1
(indicating pre-processing). This allows for example to insert tab fill-characters (tab stops) before writing the contents of the field.true
, the loop is continued ( → Step 4.). true
returned.Definition at line 127 of file formatterstdimpl.hpp.
#include <formatterstdimpl.hpp>
Inner Type Index: | |
struct | PlaceholderAttributes |
Public Method Index: | |
FormatterStdImpl (const String &formatterClassName) | |
Public Method Index: inherited from Formatter | |
Formatter () | |
Default Constructor. | |
virtual | ~Formatter () |
virtual SPFormatter | Clone ()=0 |
virtual ALIB_API void | CloneSettings (Formatter &reference) |
template<typename... TArgs> | |
Formatter & | Format (AString &target, TArgs &&... args) |
Formatter & | FormatArgs (AString &target) |
template<typename TAllocator > | |
Formatter & | FormatArgs (AString &target, const boxing::TBoxes< TAllocator > &args) |
virtual BoxesMA & | GetArgContainer () |
virtual BoxesMA & | Reset () |
Public Method Index: inherited from DbgCriticalSections | |
ALIB_FORCE_INLINE | DbgCriticalSections (const char *name) |
ALIB_FORCE_INLINE | ~DbgCriticalSections () |
Destructor. Checks that this instance is unused. | |
ALIB_API void | Acquire (const CallerInfo &ci) const |
ALIB_API void | AcquireShared (const CallerInfo &ci) const |
ALIB_API void | doAssert (bool cond, const CallerInfo &ciAssert, const CallerInfo &ci, const char *headline) const |
ALIB_API void | Release (const CallerInfo &ci) const |
ALIB_API void | ReleaseShared (const CallerInfo &ci) const |
ALIB_FORCE_INLINE void | yieldOrSleep () const |
Protected Type Index: | |
enum class | PHTypes { NotGiven , String , Character , IntBase10 , IntBinary , IntOctal , IntHex , Float , Bool , HashCode , Fill } |
Denotes the type of placeholders (respectively the values they represent). More... | |
Protected Field Index: | |
int | argOffset |
The offset of the first argument to use. Provided with method Format. | |
int | argsConsumed |
The number of arguments consumed by the current format string. | |
bool | argumentCountStartsWith1 |
const BoxesMA * | arguments |
The list of arguments provided with method Format. | |
AString | fieldBuffer |
A string buffer, used for example, when writing aligned fields. | |
String | formatString |
The format string as provided with method Format. | |
const String | formatterName |
int | nextAutoIdx |
Counter for auto-indexed arguments. | |
Substring | parser |
The current (remaining) format string. | |
PlaceholderAttributes | placeholder |
AString * | targetString |
The target string as provided with method Format. | |
integer | targetStringStartLength |
The length of the target string before adding the formatted contents. | |
Protected Field Index: inherited from Formatter | |
MonoAllocator | allocator |
This allocator is (exclusively) used for field boxes. | |
BoxesMA | boxes |
AString | formatStringBuffer |
A buffer used for conversion of the next argument if it is not of a string-type. | |
Protected Method Index: | |
virtual bool | checkStdFieldAgainstArgument () |
virtual integer | findPlaceholder ()=0 |
virtual ALIB_API int | format (AString &targetString, const String &formatString, const BoxesMA &arguments, int argOffset) override |
virtual bool | parsePlaceholder ()=0 |
virtual bool | parseStdFormatSpec ()=0 |
virtual bool | preAndPostProcess (integer startIdx, AString *target=nullptr) |
virtual void | resetPlaceholder () |
virtual bool | setArgument (int pos) |
virtual bool | writeCustomFormat () |
virtual void | writeStdArgument () |
virtual void | writeStringPortion (integer length)=0 |
Protected Method Index: inherited from Formatter | |
template<typename TAllocator > | |
Formatter & | formatLoop (AString &target, const boxing::TBoxes< TAllocator > &args) |
virtual void | initializeFormat () |
Additional Inherited Members | |
Public Static Field Index: inherited from Formatter | |
static ALIB_API SPFormatter | Default |
static ALIB_API threads::RecursiveLock | DefaultLock |
Public Static Field Index: inherited from DbgCriticalSections | |
static ALIB_API const char * | ASSERTION_FORMAT |
Public Field Index: inherited from Formatter | |
NumberFormat | AlternativeNumberFormat |
NumberFormat | DefaultNumberFormat |
SharedPtr< Formatter > | Next |
Public Field Index: inherited from DbgCriticalSections | |
CallerInfo | DCSAcq |
Source location of acquirement. | |
AssociatedLock * | DCSLock {nullptr} |
const char * | DCSName |
The name of this DCS. Used for debug-output. | |
std::atomic< int > | DCSReaderCnt {0} |
Tracks enter/exit calls of readers. | |
CallerInfo | DCSRel |
Source location of the last "reader" seen. | |
CallerInfo | DCSSAcq |
Source location of acquirement. | |
CallerInfo | DCSSRel |
Source location of the last "reader" seen. | |
std::atomic< int > | DCSWriterCnt {0} |
Tracks enter/exit calls (including readers) | |
int | DCSYieldOrSleepTimeInNS = -1 |
|
friend |
Definition at line 129 of file formatterstdimpl.hpp.
|
strongprotected |
Denotes the type of placeholders (respectively the values they represent).
Enumerator | |
---|---|
NotGiven | The default. |
String | String-type requested. |
Character | Converts a given character or integer number to the corresponding unicode character before printing. |
IntBase10 | Outputs a given number in base 10. The default. |
IntBinary | Outputs a given number in base 2. |
IntOctal | Outputs a given number in base 8. |
IntHex | Outputs a given number in base 16. |
Float | Outputs a number in floating point format. |
Bool | Writes "true" or "false". |
HashCode | Writes raw box data as hex. |
Fill | Writes FillChar x-times. Used with python-style conversion {!Fill[C]}. |
Definition at line 136 of file formatterstdimpl.hpp.
|
protected |
The offset of the first argument to use. Provided with method Format.
Definition at line 285 of file formatterstdimpl.hpp.
|
protected |
The number of arguments consumed by the current format string.
Definition at line 288 of file formatterstdimpl.hpp.
|
protected |
If false
the formatters specification expects argument to be numbered from 0..N
. If true
from 1..N
.
Definition at line 295 of file formatterstdimpl.hpp.
|
protected |
The list of arguments provided with method Format.
Definition at line 279 of file formatterstdimpl.hpp.
|
protected |
A string buffer, used for example, when writing aligned fields.
Definition at line 263 of file formatterstdimpl.hpp.
|
protected |
The format string as provided with method Format.
Definition at line 270 of file formatterstdimpl.hpp.
|
protected |
The name of the formatter as provided in the constructor. Used for generating error messages.
Definition at line 267 of file formatterstdimpl.hpp.
|
protected |
Counter for auto-indexed arguments.
Definition at line 291 of file formatterstdimpl.hpp.
|
protected |
The current (remaining) format string.
Definition at line 273 of file formatterstdimpl.hpp.
|
protected |
If false
the formatters specification expects argument to be numbered from 0..N
. If true
from 1..N
.
Definition at line 299 of file formatterstdimpl.hpp.
|
protected |
The target string as provided with method Format.
Definition at line 276 of file formatterstdimpl.hpp.
|
protected |
The length of the target string before adding the formatted contents.
Definition at line 282 of file formatterstdimpl.hpp.
FormatterStdImpl | ( | const String & | formatterClassName | ) |
Constructor.
formatterClassName | The name of the derived class. Used to generate error messages including a link into the online documentation. (Therefore has to be the exact name. |
Definition at line 34 of file formatterstdimpl.cpp.
|
protectedvirtual |
Virtual method invoked after parseStdFormatSpec and before writeStdArgument(). The default implementation checks the settings of placeholder attribute values (fields with prefix pha
), which were set by parsePlaceholder and optionally by parseStdFormatSpec, against the type of the argument given.
If type and format information is missing in the format string, reasonable default values are set depending on the type of the argument.
If | the argument type contradicts the replacement field type, exception IncompatibleTypeCode is thrown. |
true
if OK, false
if replacement should be aborted. Reimplemented in FormatterJavaStyle, and FormatterPythonStyle.
Definition at line 172 of file formatterstdimpl.cpp.
|
protectedpure virtual |
Abstract method to search the next index of an argument placeholder in the remaining substring (field parser) of the format string.
-1
if not found. Implemented in FormatterJavaStyle, and FormatterPythonStyle.
|
overrideprotectedvirtual |
Implemented abstract format method which invokes a set of new abstract methods as described in the main documentation of this class.
targetString | An AString that takes the result. |
formatString | The format string. |
arguments | The objects to convert. |
argOffset | The first object in arguments to use. |
Implements Formatter.
Definition at line 46 of file formatterstdimpl.cpp.
|
protectedpure virtual |
Abstract method to parse the format definition at the start of string parser and set the placeholder attributes accordingly.
Field FormatSpec might be set by this method to portion of the placeholder format string. If so, methods writeCustomFormat and parseStdFormatSpec are used to then parse this portion of the placeholder string.
true
on success, false
on errors. Implemented in FormatterJavaStyle, and FormatterPythonStyle.
|
protectedpure virtual |
Abstract method to parse the format specification for standard types (those that are not processed by writeCustomFormat). This method may be left empty (just return constant true
) if method parsePlaceholder will never sets field FormatSpec.
true
on success, false
on errors. Implemented in FormatterJavaStyle, and FormatterPythonStyle.
|
inlineprotectedvirtual |
Virtual method to do pre- and post- processing of the field written. Pre-processing could for example be adding tabulator spaces, letter case conversions,
A negative given index startIdx indicates the pre-processing phase. If target is given, this indicates an "intermediate phase": The argument has been written, but no alignment or cutting has been done, yet. This phase should usually be ignored, but is for example important for search and replacement actions. If a field has a custom format implementation (e.g., time and date values), then the intermediate phase is never called.
startIdx | If -1 pre-processing is indicated, otherwise post-processing and the index of the start of the field written in targetString is given. |
target | The target string, only if different from field targetString, which indicates intermediate phase. |
false
, if the placeholder should be skipped (nothing is written for it). true
otherwise. Reimplemented in FormatterJavaStyle, and FormatterPythonStyle.
Definition at line 454 of file formatterstdimpl.hpp.
|
protectedvirtual |
Overridable method to clean and reset the fields representing the current placeholder attributes (those with name prefix pha
) before parsing them.
The default implementation sets all pha-fields as documented per field.
Reimplemented in FormatterJavaStyle, and FormatterPythonStyle.
Definition at line 115 of file formatterstdimpl.cpp.
|
protectedvirtual |
Helper method (overridable) that usually is invoked by the implementation of parsePlaceholder when an argument index is read from the format string,
If this does not happen, method format will invoke this method providing -1
for value of parameter pos to automatically choose the next argument.
Consequently, this method sets the fields Arg and ArgIdx. For auto-values, it increments nextAutoIdx. Finally, this method is responsible for the correct book-keeping of argsConsumed.
pos | The index of the argument. If -1 is given, the index is auto-incremented using field nextAutoIdx. |
true
on success, false
on errors. Definition at line 138 of file formatterstdimpl.cpp.
|
protectedvirtual |
Virtual method that may write an argument using a custom method/format. The default implementation checks if object Arg supports an own format specifier by disposing about box-function FFormat. If so, the function is invoked with passing FormatSpec, the result of the formatting is written directly into the targetString and true
is returned. The latter causes method format (which invokes this method) to continue with the next replacement field.
If false
is returned, method format continues the field processing by invoking parseStdFormatSpec, checkStdFieldAgainstArgument and writeStdArgument.
true
if Arg was written, false
otherwise. Definition at line 515 of file formatterstdimpl.cpp.
|
protectedvirtual |
Virtual method to write the argument. The default implementation should be sufficient for most derived formatter implementations, but of course can be overridden and extended.
Definition at line 250 of file formatterstdimpl.cpp.
|
protectedpure virtual |
Implementations of this abstract virtual method need to copy the given amount of characters from sting parser to AString targetString. With that "escaped" placeholder field characters (for example these are "{{"
in python style or "%%"
in JAVA style) as well as other escape sequences defined with the format are to be replaced with this method.
length | The number of characters to write. |
Implemented in FormatterJavaStyle, and FormatterPythonStyle.