TextLabel

Files CCore/inc/TextLabel.h CCore/src/TextLabel.cpp

TextLabel is a lightweight text representing class. It does not hold any resources and has no a life-time limit (destructor is trivial). We need such class, because it can be used in such context, where memory allocation is impossible (for example, the memory heap is not initialized yet).


using TextLabelFunc = StrLen (*)(Handle ctx,PtrLen<char> buf) ;

class TextLabel
 {
   ....
   
  public: 
   
   // constructors
  
   TextLabel();
   
   TextLabel(const char *persistent_zstr);
   
   TextLabel(TextLabelFunc func,Handle ctx);
   
   template <class Enum>
   TextLabel(Enum e);
   
   // methods
   
   StrLen getStr(PtrLen<char> buf) const;
   
   // print object
   
   using PrintOptType = StrPrintOpt ;
   
   template <class P> 
   void print(P &out,PrintOptType opt) const;
 };

If you have a TextLabel object, you can build its string, using the method getStr(), you should supply a temporary buffer, where the string will possibly be constructed. You can also print the string, string print attributes are supported. The method print() uses a temporary buffer of the length TextBufLen. We assume that the string, represented by a TextLabel object, has a reasonably small length.

There are four ways to build a TextLabel object.

You can construct an empty string object using the default constructor.

You can build a TextLabel from a persistent C zero-terminated string (i.e. from a string literal).

The third way is to build a TextLabel from an enum. The enum value range must fit in the int value range. The function GetTextDesc() must be defined for this enum.


enum E
 {
  E1,
  E2,
  E3
 };

const char * GetTextDesc(E e);

....

TextLabel label(E1);

Finally, the more general way is to build a TextLabel from the TextLabelFunc and the Handle. Just remember, the handle must be persistent, it cannot have a limited life-time.

The next class is NumTextLabel.


unsigned GetTextLabelNumber(unsigned &Next);

template <class T>
class NumTextLabel : public TextLabel
 {
   ....
    
  public:
  
   NumTextLabel();
 };

It is designed to generate such labels like "Sem1", "Sem2", ... . Use it as following:


class Name
 {
   static Mutex NextMutex;

   static unsigned Next;

  public:

   static const char * GetText() { return "ObjName"; }

   static unsigned GetNumber() { Mutex::Lock lock(NextMutex); return GetTextLabelNumber(Next); }
 };

Mutex Name::NextMutex;

unsigned Name::Next=0;

NumTextLabel<Name> label; // "ObjName1" , "ObjName2" , ...