TreeNode::SetValue

Description

Allows recursive access for setting values to all the leaves of the TreeNode


Allows recursive access for setting values to all the leaves of the TreeNode


Allows recursive access for setting values to all the leaves of the TreeNode


Allows recursive access for setting values to all the leaves of the TreeNode

Syntax

BOOL SetValue( int nval, LPCSTR lpcszPropName = NULL, BOOL bRecursive = FALSE, BOOL bAll = FALSE )

BOOL SetValue( double dval, LPCSTR lpcszPropName = NULL, BOOL bRecursive = FALSE, BOOL bAll = FALSE )

BOOL SetValue( string strval, LPCSTR lpcszPropName = NULL, BOOL bRecursive = FALSE, BOOL bAll = FALSE )

BOOL SetValue( const vectorbase & vval, LPCSTR lpcszPropName = NULL, BOOL bRecursive = FALSE, BOOL bAll = FALSE )

Parameters

nval
[input] The value to be set
lpcszPropName
[input] tag name of leaves to set values to, if NULL will set values to "this" TreeNode object
bRecursive
[input] if True, will check all leaves, not just children, but there descendants too, for a tag name match, else will only affect the immediate children
bAll
[input] When TRUE will change all matches, else will terminate after the first match

dval
[input] The value to be set
lpcszPropName
[input] tag name of leaves to set values to, if NULL will set values to "this" TreeNode object
bRecursive
[input] if True, will check all leaves, not just children, but there descendants too, for a tag name match, else will only affect the immediate children
bAll
[input] When TRUE will change all matches, else will terminate after the first match

strval
[input] The value to be set
lpcszPropName
[input] tag name of leaves to set values to, if NULL will set values to "this" TreeNode object
bRecursive
[input] if True, will check all leaves, not just children, but there descendants too, for a tag name match, else will only affect the immediate children
bAll
[input] When TRUE will change all matches, else will terminate after the first match

vval
[input] The value to be set
lpcszPropName
[input] tag name of leaves to set values to, if NULL will set values to "this" TreeNode object
bRecursive
[input] if True, will check all leaves, not just children, but there descendants too, for a tag name match, else will only affect the immediate children
bAll
[input] When TRUE will change all matches, else will terminate after the first match

Return

TRUE if successful FALSE if fail


TRUE if successful FALSE if fail


TRUE if successful FALSE if fail


TRUE if successful FALSE if fail

Examples

EX1

// The example shows how to set integer values
void TreeNode_SetValue_ex1()
{
    Tree tr;
    TreeNode tn1, tn2;
    tn1 = tr.AddNode("Num", 1);
    tn2 = tr.AddNode("Str", 2);
    tr.SetValue("123", "Num");
    out_tree(tr);
    /*
                                 tr
               /        \
        Num(123)        Str()
    */
}

EX2

// The example shows how to set double values
void TreeNode_SetValue_ex2()
{
    Tree tr;
    TreeNode tn1;
    tn1 = tr.AddNumericNode(123.456, "Num", 1);
    tr.SetValue("0.123", "Num");
    out_tree(tr);
    /*
               tr
               |
            Num(0.123)
    */
}

EX3

// Example of non recursive set string value
// Only the immediate children will be affected
void TreeNode_SetValue_ex3()
{
    Tree tr;
    TreeNode tn1, tn10, tn11, tn12;
    tn1 = tr.AddTextNode("aaa", "node1", 1);
    tn10 = tn1.AddTextNode("bbb", "node0", 10);
    tn11 = tn1.AddTextNode("ccc", "node1", 11);
    tn12 = tn1.AddTextNode("ddd", "node1", 12);
    out_str("Origin Tree:");
    out_tree(tr);
    /* 
 
                                                tr
                                                |
                                        tn1(aaa)
                                 /      |      \
                tn10(bbb)       tn11(ccc)       tn12(ddd)
    */
    tn1.SetValue("NewValue1", "node1", FALSE, FALSE);    // Change tn11's value
    out_str("SetValue([NOT Recursive | First match]):");
    out_tree(tr);
    
    tn1.SetValue("NewValue2", "node1", FALSE, TRUE);     // If bRecursive is false, bAll is failure
    out_str("SetValue([NOT Recursive | First match]):");
    out_tree(tr);
}

EX4

// Example of recursive set string value. Set value just for the leave nodes.
void TreeNode_SetValue_ex4()
{
    Tree myTree1;
    TreeNode tn1, tn11, tn12, tn13, tn111;
    tn1 = myTree1.AddTextNode("aaa", "node1");
    tn11 = tn1.AddTextNode("bbb", "node2");
    tn12 = tn1.AddTextNode("ccc", "TargetNode");
    tn111 = tn11.AddTextNode("ddd", "TargetNode");
    out_str("Origin Tree:");
    out_tree(myTree1);
    /* 
                    myTree1
                       |
                    tn1(aaa)
                        /     \
                tn11(bbb)       tn12(ccc)
                        /
                tn111(ddd)
    */
 
    tn1.SetValue("NewValue1", "TargetNode", TRUE, FALSE); // Changed: tn111(ddd)
    out_str("SetValue() [Recursive | First match]:");
    out_tree(myTree1);
    
    tn1.SetValue("NewValue2", "TargetNode", TRUE, TRUE); // Changed: tn12(ccc), tn111(ddd)
    out_str("SetValue() [Recursive | All match]:");
    out_tree(myTree1);
}

EX5

// The example shows how to Set/Get vector value
void TreeNode_SetValue_ex5()
{
 
    vector<double> vValues1;
    for(int nn = 0; nn < 4; ++nn)
        vValues1.Add( rnd() ); 
 
    Tree tr;
    TreeNode tn = tr.AddNode("Var", 1); 
    if( tr.SetValue(vValues1, "Var") )  // Assign a vector to Var node
    {
        out_str("Assign vector to tn node");
    } 
 
    // Get the value of node "Var"
    vector<double> vValues2;
    if( tr.GetValue(vValues2, "Var") )
    { 
        for(int ii = 0; ii < vValues2.GetSize(); ++ii)      
        {       
            printf("NO.%d = %f\n", ii+1, vValues2[ii]);     
        }
    }
}

Remark

See Also

TreeNode::GetValue

header to Include

origin.h