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) prior to writing the contents of the field.true
, the loop is continued ( → Step 4.). true
returned.Definition at line 129 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 | |
virtual | ~Formatter () |
ALIB_API Boxes & | Acquire (const NCString &dbgFile, int dbgLine, const NCString &dbgFunc) |
virtual Formatter * | Clone ()=0 |
virtual ALIB_API void | CloneSettings (Formatter &reference) |
int | CountAcquirements () const |
template<typename... TArgs> | |
Formatter & | Format (AString &target, TArgs &&... args) |
ALIB_THREADS. | |
ALIB_API Formatter & | FormatArgs (AString &target) |
Formatter & | FormatArgs (AString &target, const Boxes &args) |
ALIB_API void | Release () |
defined(ALIB_DOX) | |
ALIB_API void | ReplaceDefault (Formatter *newFormatter) |
Public Method Index: inherited from ThreadLock | |
ALIB_API | ThreadLock (lang::Safeness safeness=lang::Safeness::Safe) |
ALIB_API | ~ThreadLock () |
ALIB_API void | Acquire (const NCString &dbgFile, int dbgLine, const NCString &dbgFunc) |
int | CountAcquirements () const |
Thread * | GetOwner () const |
lang::Safeness | GetSafeness () const |
bool | IsOwnedByCurrentThread () const |
ALIB_API void | Release () |
defined(ALIB_DOX) | |
ALIB_API void | SetSafeness (lang::Safeness safeness) |
bool | WillRelease () const |
Additional Inherited Members | |
Public Static Method Index: inherited from Formatter | |
static SPFormatter | AcquireDefault (const NCString &dbgFile, int dbgLine, const NCString &dbgFunc) |
static SPFormatter | GetDefault () |
Public Field Index: inherited from Formatter | |
NumberFormat | AlternativeNumberFormat |
NumberFormat | DefaultNumberFormat |
std::shared_ptr< Formatter > | Next |
Public Field Index: inherited from ThreadLock | |
NCString | DbgOwnerFile =nullptr |
NCString | DbgOwnerFunc =nullptr |
int | DbgOwnerLine |
uint16_t | DbgRecursionWarningThreshold =10 |
integer | DbgWarningAfterWaitTimeInMillis =2000L |
|
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 287 of file formatterstdimpl.hpp.
|
protected |
The number of arguments consumed by the current format string.
Definition at line 290 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 297 of file formatterstdimpl.hpp.
|
protected |
The list of arguments provided with method Format.
Definition at line 281 of file formatterstdimpl.hpp.
|
protected |
A string buffer, used for example, when writing aligned fields.
Definition at line 265 of file formatterstdimpl.hpp.
|
protected |
The format string as provided with method Format.
Definition at line 272 of file formatterstdimpl.hpp.
|
protected |
The name of the formatter as provided in the constructor. Used for generating error messages.
Definition at line 269 of file formatterstdimpl.hpp.
|
protected |
Counter for auto-indexed arguments.
Definition at line 293 of file formatterstdimpl.hpp.
|
protected |
The current (remaining) format string.
Definition at line 275 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 301 of file formatterstdimpl.hpp.
|
protected |
The target string as provided with method Format.
Definition at line 278 of file formatterstdimpl.hpp.
|
protected |
The length of the target string before adding the formatted contents.
Definition at line 284 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 43 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 181 of file formatterstdimpl.cpp.
|
protectedpure virtual |
Abstract method to search the next index of an argument placeholder in the remaining sub-string (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 55 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 456 of file formatterstdimpl.hpp.
|
protectedvirtual |
Overridable method to clean and reset the fields representing the current placeholder attributes (those with name prefix pha
) prior to parsing them.
The default implementation sets all pha-fields as documented per field.
Reimplemented in FormatterJavaStyle, and FormatterPythonStyle.
Definition at line 124 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 147 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 517 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 258 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.