Evo C++ Library v0.5.1
Classes | Public Types | Public Member Functions | Public Attributes | Static Public Attributes | List of all members
StringFlt< T, PADDING > Struct Template Reference

#include <evo/string.h>

Inheritance diagram for StringFlt< T, PADDING >:
Inheritance graph
[legend]

Detailed Description

template<class T = double, int PADDING = 1>
struct evo::StringFlt< T, PADDING >

String fixed-size buffer for formatting a floating point number.

Template Parameters
TFloating point type
PADDINGAdditional padding for buffer, defaults to 1 for terminator
Example

This example uses printf() as a function that requires a terminated string.

#include <evo/string.h>
#include <stdio.h>
using namespace evo;
int main() {
StringFlt<double> str1(1.23);
printf("%s\n", str1.data());
printf("%s\n", str2.set(66.77));
return 0;
}

Output:

1.23
66.77

Classes

struct  NumInfo
 Advanced: Special structure used to avoid automatically allocating memory when a bigger buffer is needed. More...
 

Public Types

typedef ListBase< char, StrSizeTListBaseType
 List base type (this) More...
 
typedef StrSizeT Size
 Size type. More...
 
typedef StringFlt< T, PADDING > ThisType
 This type. More...
 

Public Member Functions

 StringFlt ()
 Constructor intializes to null. More...
 
 StringFlt (T num, int precision=fPREC_AUTO, bool terminated=true)
 Constructor intializes with set(). More...
 
 ~StringFlt ()
 Destructor. More...
 
char * data ()
 Get formatted string pointer. More...
 
ThisTypeset ()
 Set as null. More...
 
char * set (T num, int precision=fPREC_AUTO, bool terminated=true, NumInfo *info=NULL)
 Set floating point number and format to string. More...
 
Size size () const
 Get formatting string size. More...
 

Public Attributes

char buffer [BUF_SIZE]
 String buffer More...
 
char * data_
 Data pointer, NULL if null. More...
 
StrSizeT size_
 Data size as item count, 0 if empty or null. More...
 

Static Public Attributes

static const int BUF_SIZE = FloatT<T>::MAXDIGITS_AUTO + PADDING
 Buffer size. More...
 

Member Typedef Documentation

◆ ListBaseType

typedef ListBase<char ,StrSizeT > ListBaseType
inherited

List base type (this)

◆ Size

typedef StrSizeT Size

Size type.

◆ ThisType

typedef StringFlt<T,PADDING> ThisType

This type.

Constructor & Destructor Documentation

◆ StringFlt() [1/2]

StringFlt ( )
inline

Constructor intializes to null.

◆ StringFlt() [2/2]

StringFlt ( num,
int  precision = fPREC_AUTO,
bool  terminated = true 
)
inline

Constructor intializes with set().

  • For special cases where stack buffer isn't big enough, this allocates the needed memory internally (which is freed by destructor)
Parameters
numNumber to set/format
precisionFormatting precision (number of fractional digits), 0 for none, fPREC_AUTO for automatic
terminatedWhether to add terminator after number

◆ ~StringFlt()

~StringFlt ( )
inline

Destructor.

Member Function Documentation

◆ data()

char* data ( )
inline

Get formatted string pointer.

  • This is null until set(T,int,bool,uint) is called
Returns
String pointer, NULL if null

◆ set() [1/2]

ThisType& set ( )
inline

Set as null.

Returns
This

◆ set() [2/2]

char* set ( num,
int  precision = fPREC_AUTO,
bool  terminated = true,
NumInfo info = NULL 
)
inline

Set floating point number and format to string.

  • For optimizing a special case where more memory is needed:
    • 1. Pass info=NULL (the default): this allocates the needed memory internally (which is freed by destructor) and formatting always succeeds
    • 2. Advanced: Pass an object pointer for info: this populates the info object and returns NULL, call info->format() to finish formatting with your own buffer
      • This allows optimization with your own buffer to minimize memory allocs
    • These cases don't apply when precision=fPREC_AUTO as the stack buffer is always big enough for automatic precision formatting (which uses scientific notation if needed)
Parameters
numNumber to set/format
precisionFormatting precision (number of fractional digits), 0 for none, fPREC_AUTO for automatic
infoUsed when more memory is needed: NULL to alloc internally and free in destructor, otherwise this is populated and NULL is returned
terminatedWhether to add terminator after number
infoNULL by default, otherwise a non-NULL value enables optimization for a special case (see above)
Returns
Pointer to formatted string in buffer, NULL if info != NULL and a buffer alloc is needed (see above)

◆ size()

Size size ( ) const
inline

Get formatting string size.

  • Padding is not included in size
Returns
string size in bytes, 0 if null

Member Data Documentation

◆ BUF_SIZE

const int BUF_SIZE = FloatT<T>::MAXDIGITS_AUTO + PADDING
static

Buffer size.

◆ buffer

char buffer[BUF_SIZE]

String buffer

◆ data_

char * data_
inherited

Data pointer, NULL if null.

◆ size_

StrSizeT size_
inherited

Data size as item count, 0 if empty or null.


The documentation for this struct was generated from the following file: