Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
openlcb::CDIUtils Class Reference

Classes

struct  CDINodeRep
 
struct  NodeInfo
 Allocation data we hold about a Data Element in its userinfo structure. More...
 
struct  SXmlStringDeleter
 Helper class for deleting sxml strings. More...
 
struct  XMLDocDeleter
 Helper class for unique_ptr to delete an XML document correctly. More...
 

Public Types

enum class  DataType {
  UNKNOWN = 0 , GROUP , INT , FLOAT ,
  STRING , EVENTID
}
 Used to classify elements. More...
 
typedef std::unique_ptr< XMLDoc, XMLDocDeleterxmldoc_ptr_t
 Smart pointer class for holding XML documents.
 
typedef std::unique_ptr< const SXML_CHAR, SXmlStringDeleterxmlstring_t
 Smart pointer class for holding C strings from sxml.
 

Static Public Member Functions

static XMLNode * find_child_or_null (const XMLNode *parent, const char *tag)
 Searches the list of children for the first child with a specific tag.
 
static string find_node_name (const XMLNode *node, const char *def)
 Finds the name of a CDI element.
 
static string find_node_description (const XMLNode *node)
 Find the description of a CDI element.
 
static int find_numeric_element (const XMLNode *parent, const char *tag, bool *found)
 
static int find_node_min (const XMLNode *node, bool *found)
 Finds the min value of the xml integer, if there isn't one it returns a null pointer.
 
static int find_node_max (const XMLNode *node, bool *found)
 Finds the max value of the xml integer, if there isn't one it returns a null pointer.
 
static int find_node_default (const XMLNode *node, bool *found)
 Finds the default value of the xml integer, if there isn't one it returns a null pointer.
 
static void prepare_doc (XMLDoc *doc)
 Clears out all user info structure pointers.
 
static void cleanup_doc (XMLDoc *doc)
 Deletes all userinfo structures allocated in a doc.
 
template<class T , typename... Args>
static void new_userinfo (T **info, XMLNode *node, Args &&... args)
 Allocates a new object of type T for the node as userinfo; calls T's constructor with args..., stores the resulting pointer in the userinfo pointer of node.
 
template<class T >
static void get_userinfo (T **info, const XMLNode *node)
 Retrieve the userinfo structure from an XML node.
 
static int get_numeric_attribute (const XMLNode *node, const char *attr_name, int def=0)
 Helper function to find and convert an attribute to a number.
 
static DataType get_type_from_node (XMLNode *child)
 Classifies XML elements to node types.
 
static void layout_segment (XMLNode *segment)
 Allocates all userinfo structures within a segment and performs the offset layout algorithm.
 
static int get_replication (const XMLNode *group)
 

Private Member Functions

 CDIUtils ()
 Static class; never instantiated.
 

Detailed Description

Definition at line 43 of file CDIUtils.hxx.

Member Typedef Documentation

◆ xmldoc_ptr_t

typedef std::unique_ptr<XMLDoc, XMLDocDeleter> openlcb::CDIUtils::xmldoc_ptr_t

Smart pointer class for holding XML documents.

Definition at line 61 of file CDIUtils.hxx.

◆ xmlstring_t

typedef std::unique_ptr<const SXML_CHAR, SXmlStringDeleter> openlcb::CDIUtils::xmlstring_t

Smart pointer class for holding C strings from sxml.

Appropriately frees the string upon going out of scope.

Definition at line 74 of file CDIUtils.hxx.

Member Enumeration Documentation

◆ DataType

enum class openlcb::CDIUtils::DataType
strong

Used to classify elements.

Definition at line 265 of file CDIUtils.hxx.

Member Function Documentation

◆ cleanup_doc()

static void openlcb::CDIUtils::cleanup_doc ( XMLDoc *  doc)
inlinestatic

Deletes all userinfo structures allocated in a doc.

Parameters
docdocument to clean up.

Definition at line 188 of file CDIUtils.hxx.

◆ find_child_or_null()

static XMLNode * openlcb::CDIUtils::find_child_or_null ( const XMLNode *  parent,
const char *  tag 
)
inlinestatic

Searches the list of children for the first child with a specific tag.

Parameters
parentis the node whose children to search.
tagis which child to look for.
Returns
nullptr if not found, else the first occurrence of tag in the children.

Definition at line 81 of file CDIUtils.hxx.

◆ find_node_default()

static int openlcb::CDIUtils::find_node_default ( const XMLNode *  node,
bool found 
)
inlinestatic

Finds the default value of the xml integer, if there isn't one it returns a null pointer.

Parameters
nodeis a CDI element (segment, group, or config element).
foundwill be set to true if there was a defaultimum found, false if the XML did not contain the respective tag.
Returns
contents of the <default> tag, unspecified if !found.

Definition at line 167 of file CDIUtils.hxx.

◆ find_node_description()

static string openlcb::CDIUtils::find_node_description ( const XMLNode *  node)
inlinestatic

Find the description of a CDI element.

Parameters
nodeis a CDI element (segment, group, or config element).
Returns
finds a child tag and returns its contents. Returns empty string if no was found.

Definition at line 113 of file CDIUtils.hxx.

◆ find_node_max()

static int openlcb::CDIUtils::find_node_max ( const XMLNode *  node,
bool found 
)
inlinestatic

Finds the max value of the xml integer, if there isn't one it returns a null pointer.

Parameters
nodeis a CDI element (segment, group, or config element).
foundwill be set to true if there was a maximum found, false if the XML did not contain the respective tag.
Returns
contents of the <max> tag, unspecified if !found.

Definition at line 156 of file CDIUtils.hxx.

◆ find_node_min()

static int openlcb::CDIUtils::find_node_min ( const XMLNode *  node,
bool found 
)
inlinestatic

Finds the min value of the xml integer, if there isn't one it returns a null pointer.

Parameters
nodeis a CDI element (segment, group, or config element).
foundwill be set to true if there was a minimum found, false if the XML did not contain the respective tag.
Returns
contents of the <min> tag, unspecified if !found.

Definition at line 145 of file CDIUtils.hxx.

◆ find_node_name()

static string openlcb::CDIUtils::find_node_name ( const XMLNode *  node,
const char *  def 
)
inlinestatic

Finds the name of a CDI element.

Parameters
nodeis a CDI element (segment, group, or config element).
defis the default to return if no <name> element exists or if it has no text.
Returns
finds a child tag <name> and returns its contents. Returns empty string if no <name> was found.

Definition at line 100 of file CDIUtils.hxx.

◆ find_numeric_element()

static int openlcb::CDIUtils::find_numeric_element ( const XMLNode *  parent,
const char *  tag,
bool found 
)
inlinestatic

Definition at line 125 of file CDIUtils.hxx.

◆ get_numeric_attribute()

static int openlcb::CDIUtils::get_numeric_attribute ( const XMLNode *  node,
const char *  attr_name,
int  def = 0 
)
inlinestatic

Helper function to find and convert an attribute to a number.

Parameters
nodeis the XML node of the element whose attribute we're looking for
attr_nameis a C-string for the attribute name we're looking for
defis the default value that will be returned if the attribute is not found.
Returns
attribute value, or def if not found, or zero if the attribute is found but the value is not convertible to an integer.

Definition at line 250 of file CDIUtils.hxx.

◆ get_replication()

static int openlcb::CDIUtils::get_replication ( const XMLNode *  group)
inlinestatic
Returns
the number of replicas a group has, if it is a repeated group, or 1 if it is a non-repeated group.
Parameters
groupis the XML node of the <group> element to query.

Definition at line 392 of file CDIUtils.hxx.

◆ get_type_from_node()

static DataType openlcb::CDIUtils::get_type_from_node ( XMLNode *  child)
inlinestatic

Classifies XML elements to node types.

Parameters
childis an XML element under a group.
Returns
node type or UNKNOWN

Definition at line 277 of file CDIUtils.hxx.

◆ get_userinfo()

template<class T >
static void openlcb::CDIUtils::get_userinfo ( T **  info,
const XMLNode *  node 
)
inlinestatic

Retrieve the userinfo structure from an XML node.

Parameters
infowill be set to the userinfo structure using an unchecked cast to T. This variable must be of the same (or compatible) type as what the userinfo has been allocated to.
nodeis the XML element node whose userinfo we are trying to fetch

Definition at line 223 of file CDIUtils.hxx.

◆ layout_segment()

static void openlcb::CDIUtils::layout_segment ( XMLNode *  segment)
inlinestatic

Allocates all userinfo structures within a segment and performs the offset layout algorithm.

Parameters
segmentis the XML node of the <segment> element.

Definition at line 305 of file CDIUtils.hxx.

◆ new_userinfo()

template<class T , typename... Args>
static void openlcb::CDIUtils::new_userinfo ( T **  info,
XMLNode *  node,
Args &&...  args 
)
inlinestatic

Allocates a new object of type T for the node as userinfo; calls T's constructor with args..., stores the resulting pointer in the userinfo pointer of node.

Parameters
infooutput argument for the userinfo structure pointer.
nodewhich XML node the userinfo should point at.
args...forwarded as the constructor arguments for T (can be empty).

Definition at line 207 of file CDIUtils.hxx.

◆ prepare_doc()

static void openlcb::CDIUtils::prepare_doc ( XMLDoc *  doc)
inlinestatic

Clears out all user info structure pointers.

This is necessary to use the new_userinfo call below. Call this after the XML has been successfully parsed.

Parameters
docdocument to prepare up.

Definition at line 176 of file CDIUtils.hxx.


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