OpenClonk
StdBitfieldAdapt< T > Struct Template Reference

#include <StdAdaptors.h>

Collaboration diagram for StdBitfieldAdapt< T >:
[legend]

Public Types

typedef StdBitfieldEntry< T > Entry
 

Public Member Functions

 StdBitfieldAdapt (T &rVal, const Entry *pNames)
 
void CompileFunc (StdCompiler *pComp) const
 
template<class D >
bool operator== (const D &nValue) const
 
template<class D >
StdBitfieldAdapt< T > & operator= (const D &nValue)
 

Public Attributes

T & rVal
 
const EntrypNames
 

Detailed Description

template<class T>
struct StdBitfieldAdapt< T >

Definition at line 876 of file StdAdaptors.h.

Member Typedef Documentation

◆ Entry

template<class T>
typedef StdBitfieldEntry<T> StdBitfieldAdapt< T >::Entry

Definition at line 878 of file StdAdaptors.h.

Constructor & Destructor Documentation

◆ StdBitfieldAdapt()

template<class T>
StdBitfieldAdapt< T >::StdBitfieldAdapt ( T &  rVal,
const Entry pNames 
)
inline

Definition at line 880 of file StdAdaptors.h.

880 : rVal(rVal), pNames(pNames) { assert(pNames); }
const Entry * pNames
Definition: StdAdaptors.h:881

Member Function Documentation

◆ CompileFunc()

template<class T>
void StdBitfieldAdapt< T >::CompileFunc ( StdCompiler pComp) const
inline

Definition at line 883 of file StdAdaptors.h.

References StdStrBuf::getData(), StdCompiler::isDeserializer(), StdCompiler::isVerbose(), mkParAdapt(), StdBitfieldEntry< T >::Name, StdCompiler::RCT_Idtf, StdCompiler::SEP_VLINE, StdCompiler::Separator(), StdCompiler::String(), StdBitfieldEntry< T >::Val, StdCompiler::Value(), and StdCompiler::Warn().

884  {
885  // simply write for non-verbose compilers
886  if (!pComp->isVerbose())
887  {
888  pComp->Value(rVal);
889  return;
890  }
891  // writing?
892  if (!pComp->isDeserializer())
893  {
894  T val = rVal, orig_val = rVal;
895  // Write until value is comsumed
896  bool fFirst = true;
897  for (const Entry *pName = pNames; pName->Name; pName++)
898  if ((pName->Val & val) == pName->Val)
899  {
900  // Avoid writing meaningless none-values (e.g. Category=C4D_None|C4D_Object)
901  if (orig_val && !pName->Val) continue;
902  // Put "|"
903  if (!fFirst) pComp->Separator(StdCompiler::SEP_VLINE);
904  // Put name
905  pComp->String(const_cast<char **>(&pName->Name), StdCompiler::RCT_Idtf);
906  fFirst = false;
907  // Remove bits
908  val &= ~pName->Val;
909  }
910  // Anything left is written as number, or a simple 0 in case no default was used
911  if (val || fFirst)
912  {
913  // Put "|"
914  if (!fFirst) pComp->Separator(StdCompiler::SEP_VLINE);
915  // Put value
916  pComp->Value(val);
917  }
918  }
919  // reading?
920  else
921  {
922  T val = 0;
923  // Read
924  do
925  {
926 #ifdef STDCOMPILER_EXCEPTION_WORKAROUND
927  T tmp;
928  // Try to read as number
929  if (pComp->ValueSafe(tmp))
930  val |= tmp;
931  else
932  {
933 #else
934  // Try to read as number
935  try
936  {
937  T tmp;
938  pComp->Value(tmp);
939  val |= tmp;
940  }
941  catch (StdCompiler::NotFoundException *pEx)
942  {
943  delete pEx;
944 #endif
945  // Try to read as string
946  StdStrBuf Name;
947  pComp->Value(mkParAdapt(Name, StdCompiler::RCT_Idtf));
948  // Search in name list
949  const Entry *pName = pNames;
950  for (; pName->Name; pName++)
951  if (Name == pName->Name)
952  {
953  val |= pName->Val;
954  break;
955  }
956  // Not found? Warn
957  if (!pName->Name)
958  pComp->Warn("Unknown bit name: %s", Name.getData());
959  }
960  // Expect separation
961  } while (pComp->Separator(StdCompiler::SEP_VLINE));
962  // Write value back
963  rVal = val;
964  }
965  }
virtual bool Separator(Sep eSep=SEP_SEP)
Definition: StdCompiler.h:119
virtual void String(char *szString, size_t iMaxLength, RawCompileType eType=RCT_Escaped)=0
virtual bool isVerbose()
Definition: StdCompiler.h:62
void Value(const T &rStruct)
Definition: StdCompiler.h:161
const char * getData() const
Definition: StdBuf.h:442
virtual bool isDeserializer()
Definition: StdCompiler.h:53
StdBitfieldEntry< T > Entry
Definition: StdAdaptors.h:878
void Warn(const char *szWarning,...)
Definition: StdCompiler.cpp:21
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:476
const char * Name
Definition: StdAdaptors.h:870
const Entry * pNames
Definition: StdAdaptors.h:881
Here is the call graph for this function:

◆ operator=()

template<class T>
template<class D >
StdBitfieldAdapt<T>& StdBitfieldAdapt< T >::operator= ( const D &  nValue)
inline

Definition at line 968 of file StdAdaptors.h.

968 { rVal = nValue; return *this; }

◆ operator==()

template<class T>
template<class D >
bool StdBitfieldAdapt< T >::operator== ( const D &  nValue) const
inline

Definition at line 967 of file StdAdaptors.h.

967 { return rVal == nValue; }

Member Data Documentation

◆ pNames

template<class T>
const Entry* StdBitfieldAdapt< T >::pNames

Definition at line 881 of file StdAdaptors.h.

◆ rVal

template<class T>
T& StdBitfieldAdapt< T >::rVal

Definition at line 881 of file StdAdaptors.h.


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