SourceForge.net Logo

val_example.cpp

// val_example.cpp

#include "assert.h"
#include <iostream>

#include "val.hpp"

int main()
{
    eval::val v1(new std::string("hello world"), eval::TRANSFER_OWNERSHIP);
    eval::val v2(v1); // make a shallow copy - reference counted

    int a = 8;
    eval::val v3(a, eval::COPY_VALUE);
    eval::val v4 = v3; // make a real copy
    int *the_value = eval::val_cast_ptr<int>(v3);
    assert(*the_value == 8);
    assert(v3.coerce_string() == std::string("8")); // works with ostreamable types
    assert(v3.coerce_int() == 8); // works with numeric types (e.g. int, double, ...)

   
    try
    {
        eval::val v5(8, eval::COPY_VALUE);
        int *x;
        x = eval::val_cast_ptr<int>(v5); // const int, so can only cast to const int
        assert(0);
    }
    catch(eval::bad_val_cast& e)
    {
        // val_cast_ptr can only succeed if the types match _exactly_.
        // eval::execution_context provides conversion casts.
    }
    
    // For globals or local variables declared on the stack:
    eval::val v6(&std::cout, eval::UNMANAGED_PTR);


    // Other variants exist, taking arrays or shared pointer arguments.

    return 0;
}

Generated on Sat Sep 20 20:02:34 2008 for eval by  doxygen 1.5.6