ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
FormatterStdImpl Class Referenceabstract

Description:

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".

Note
This concept is implemented with class FormatterPythonStyle as the "Python format mini language" supports such custom format specifications. Class FormatterJavaStyle does not support this mechanism.

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:

  1. Method parameters are stored in fields targetString, formatString, arguments and argOffset. This way, the parameters can be accessed from any implemented method without the need of passing them as parameters once more.
    In addition, field parser is initialized. This Substring is used to parse the format string. Parsed portions will be consumed from the front of the string. Finally fields argsConsumed and nextAutoIdx are initialized to value 0.
  2. Start of the loop to find and process placeholders in the format string.
  3. Abstract method findPlaceholder is invoked. If this fails (no further placeholder was found) parsing stops. If, and only if, a placeholder was found before, method writeStringPortion is invoked for the rest of the string before exiting the function.
  4. Method writeStringPortion is invoked to write the current parser contents up to the placeholder position.
  5. Method resetPlaceholder is invoked, to reset the attributes that will be parsed in the next step. The values that are set are implementation specific and need to reflect the default formatting options if no specific options are given in the format string.
  6. Abstract Method parsePlaceholder is invoked to parse and consume tokens from string parser and while doing this, to store the parsed format attributes in the fields with name prefix pha (or extended attributes of a derived formatter type).
    If an argument (positional) index is found during parsing, then method setArgument is to be invoked by abstract method parsePlaceholder providing that index.
    If the format syntax of the formatter contains a separated format specification string (a substring of the placeholder string), then the method may store such format substring in field FormatSpec.
  7. Next, it is checked if an argument was set by parsePlaceholder. If not, setArgument is invoked providing -1 for the index to indicate auto-indexing.
    Note
    If auto-indexing should be implemented differently than done with default method setArgument, then a custom formatter might either override the method or, in the case no index is given in the format string, just set fields Arg and ArgIdx already in parsePlaceholder according to its own strategy
  8. Method preAndPostProcess is invoked with parameter startIdx equalling -1 (indicating pre-processing). This allows for example to insert tab fill-characters (tab stops) before writing the contents of the field.
  1. Method writeCustomFormat is invoked. This allows derived formatters to write arguments in a custom way. If the method returns true, the loop is continued ( → Step 4.).
    The default implementation checks whether box-function FFormat is defined for Arg. In this case, the interface is invoked and true returned.
  2. Again, if a format specification was stored in FormatSpec method parseStdFormatSpec is invoked which needs to set further attributes in the Placeholder object according to the standard format specification of the formatter.
  3. Now, as all fields that represent formatting attributes are well set (or kept with their defaulted value), method checkStdFieldAgainstArgument is invoked. This method is virtual but not abstract. Its default implementation checks the placeholder attributes against the provided argument type and raises an error if the argument does not fit to the placeholder format specification.
  4. Method writeStdArgument is invoked. This method is virtual but not abstract. Its default implementation writes the argument value formatted according to the attribute fields.
  5. Finally preAndPostProcess is invoked with parameter startIdx pointing to the first character in targetString of the argument written. Here, actions like case conversion might be done on the field written.
  6. End of loop ( → Step 4.)

Definition at line 127 of file formatterstdimpl.hpp.

#include <formatterstdimpl.hpp>

Inheritance diagram for FormatterStdImpl:
[legend]
Collaboration diagram for FormatterStdImpl:
[legend]

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>
FormatterFormat (AString &target, TArgs &&... args)
 
FormatterFormatArgs (AString &target)
 
template<typename TAllocator >
FormatterFormatArgs (AString &target, const boxing::TBoxes< TAllocator > &args)
 
virtual BoxesMAGetArgContainer ()
 
virtual BoxesMAReset ()
 
- 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 BoxesMAarguments
 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
 
AStringtargetString
 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 >
FormatterformatLoop (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< FormatterNext
 
- Public Field Index: inherited from DbgCriticalSections
CallerInfo DCSAcq
 Source location of acquirement.
 
AssociatedLockDCSLock {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
 

Friends And Related Entity Details:

◆ lang::basecamp::BaseCamp

friend class lang::basecamp::BaseCamp
friend

Definition at line 129 of file formatterstdimpl.hpp.

Enumeration Details:

◆ PHTypes

enum class PHTypes
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.

Field Details:

◆ argOffset

int argOffset
protected

The offset of the first argument to use. Provided with method Format.

Definition at line 285 of file formatterstdimpl.hpp.

◆ argsConsumed

int argsConsumed
protected

The number of arguments consumed by the current format string.

Definition at line 288 of file formatterstdimpl.hpp.

◆ argumentCountStartsWith1

bool argumentCountStartsWith1
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.

◆ arguments

const BoxesMA* arguments
protected

The list of arguments provided with method Format.

Definition at line 279 of file formatterstdimpl.hpp.

◆ fieldBuffer

AString fieldBuffer
protected

A string buffer, used for example, when writing aligned fields.

Definition at line 263 of file formatterstdimpl.hpp.

◆ formatString

String formatString
protected

The format string as provided with method Format.

Definition at line 270 of file formatterstdimpl.hpp.

◆ formatterName

const String formatterName
protected

The name of the formatter as provided in the constructor. Used for generating error messages.

Definition at line 267 of file formatterstdimpl.hpp.

◆ nextAutoIdx

int nextAutoIdx
protected

Counter for auto-indexed arguments.

Definition at line 291 of file formatterstdimpl.hpp.

◆ parser

Substring parser
protected

The current (remaining) format string.

Definition at line 273 of file formatterstdimpl.hpp.

◆ placeholder

PlaceholderAttributes placeholder
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.

◆ targetString

AString* targetString
protected

The target string as provided with method Format.

Definition at line 276 of file formatterstdimpl.hpp.

◆ targetStringStartLength

integer targetStringStartLength
protected

The length of the target string before adding the formatted contents.

Definition at line 282 of file formatterstdimpl.hpp.

Constructor(s) / Destructor Details:

◆ FormatterStdImpl()

FormatterStdImpl ( const String & formatterClassName)

Constructor.

Parameters
formatterClassNameThe 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.

Here is the call graph for this function:

Method Details:

◆ checkStdFieldAgainstArgument()

bool checkStdFieldAgainstArgument ( )
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.

Exceptions
Ifthe argument type contradicts the replacement field type, exception IncompatibleTypeCode is thrown.
Returns
true if OK, false if replacement should be aborted.

Reimplemented in FormatterJavaStyle, and FormatterPythonStyle.

Definition at line 172 of file formatterstdimpl.cpp.

Here is the call graph for this function:

◆ findPlaceholder()

virtual integer findPlaceholder ( )
protectedpure virtual

Abstract method to search the next index of an argument placeholder in the remaining substring (field parser) of the format string.

Returns
The index found, -1 if not found.

Implemented in FormatterJavaStyle, and FormatterPythonStyle.

◆ format()

int format ( AString & targetString,
const String & formatString,
const BoxesMA & arguments,
int argOffset )
overrideprotectedvirtual

Implemented abstract format method which invokes a set of new abstract methods as described in the main documentation of this class.

Parameters
targetStringAn AString that takes the result.
formatStringThe format string.
argumentsThe objects to convert.
argOffsetThe first object in arguments to use.
Returns
The number of args consumed.

Implements Formatter.

Definition at line 46 of file formatterstdimpl.cpp.

Here is the call graph for this function:

◆ parsePlaceholder()

virtual bool parsePlaceholder ( )
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.

Returns
true on success, false on errors.

Implemented in FormatterJavaStyle, and FormatterPythonStyle.

◆ parseStdFormatSpec()

virtual bool parseStdFormatSpec ( )
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.

Returns
true on success, false on errors.

Implemented in FormatterJavaStyle, and FormatterPythonStyle.

◆ preAndPostProcess()

virtual bool preAndPostProcess ( integer startIdx,
AString * target = nullptr )
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.

Note
The reason why this method is not implemented as two different ones is that derived classes might do some more complicated parsing of parts of the placeholder string in this method. In this case, the parsing is needed to be implemented only once, while the finally parsed commands are only conditionally executed depending if executed as pre or post phase.
Parameters
startIdxIf -1 pre-processing is indicated, otherwise post-processing and the index of the start of the field written in targetString is given.
targetThe target string, only if different from field targetString, which indicates intermediate phase.
Returns
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.

◆ resetPlaceholder()

void resetPlaceholder ( )
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.

Note
Derived classes (aka the specific formatter classes) are to invoke this (parent) implementation first and then to make some own adjustments to meet the defaults that apply to the formatting specification implemented by the derived class and - if this applies - also to reset extended attributes of the derived formatter type.

Reimplemented in FormatterJavaStyle, and FormatterPythonStyle.

Definition at line 115 of file formatterstdimpl.cpp.

Here is the call graph for this function:

◆ setArgument()

bool setArgument ( int pos)
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.

Parameters
posThe index of the argument. If -1 is given, the index is auto-incremented using field nextAutoIdx.
Returns
true on success, false on errors.

Definition at line 138 of file formatterstdimpl.cpp.

Here is the call graph for this function:

◆ writeCustomFormat()

bool writeCustomFormat ( )
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.

Returns
true if Arg was written, false otherwise.

Definition at line 515 of file formatterstdimpl.cpp.

Here is the call graph for this function:

◆ writeStdArgument()

void writeStdArgument ( )
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.

Here is the call graph for this function:

◆ writeStringPortion()

virtual void writeStringPortion ( integer length)
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.

Parameters
lengthThe number of characters to write.

Implemented in FormatterJavaStyle, and FormatterPythonStyle.


The documentation for this class was generated from the following files: