Page tree
Skip to end of metadata
Go to start of metadata

The central class encapsulating variant data types.

#include "variant.h"

Overview

FB::variant holds variant data and provides convenient access to it. It is constructible from the most common data types and allows convenient conversion between the supported types.

We also have convenience support for dealing with lists and maps of variant.

Supported types

In essence, FB::variant is a template based datatype that can hold literally any type. However, when it comes to actually interfacing with the browser, there are a limited number of types that will be accepted:

note that if you use JSAPIAuto you can use these as your parameter or return types and type conversion will be done automatically. Otherwise you need to use convert_cast to get to the type you want from a variant.

FB::variant

When used in JSAPIAuto it will accept any type

long

There are no "int" values; this is because IE only sends a long, and allowing an int could cause loss of data with no warning

double

Any floating point numbers from the browser will return as double

bool

boolean values in javascript translate to bool in c++

std::string

Non-unicode string types

std::wstring

Wide string (supporting UNICODE)

FB::JSObjectPtr

Any objects or methods from javascript can be converted to a FB::JSObject, which is actually a typedef for boost::shared_ptr<JSObject> (as of 1.3)

FB::JSAPIPtr

This is usually a ptr to a FB::JSAPI object you create in C++; your root JSAPI is an example. It will never come from the browser, only be passed to the browser

std::vector<type>

Any javascript array object can be converted to a vector if the supplied type is valid (on this list)

std::map<std::string, type>

Any javascript object can be converted to a map with a string indexer if the supplied type is valid (on this list)

FB::VariantList

this is a typedef for std::vector<FB::variant>

FB::VariantMap

this is a typedef for std::map<std::string, std::variant>

FB::CatchAll

When used as the last parameter in a method on your JSAPIAuto class, this will catch 0 or more arguments as FB::variant. This is basically a std::vector<FB::variant> in real use

Constructors

...

is_of_type()

template<typename T> bool is_of_type() const

  • returns true if the held data is of type T

Example:

std::string str = "foo";
FB::variant var(str);
assert(var.is_of_type<std::string>());

get_type()

const std::type_info& get_type() const

  • returns the type_info for the held data

Example:

std::string str = "foo";
FB::variant var(str);
assert(var.get_type() == typeid(std::string));

convert_cast()

template<typename T> const T& convert_cast() const

  • extends cast() with conversions to container types
  • returns a const reference to the held data on success
  • throws FB::bad_variant_cast on failure

cast()

template<typename T> const T& cast()

  • returns a const reference to the held data on success
  • throws FB::bad_variant_cast on failure

empty()

bool empty() const

  • returns true if the variant doesn't currently hold any data, false otherwise

reset()

void reset()

  • no-op if empty()
  • if not empty, the held data is freed
  • afterwards, empty() is true

Examples

FB::variant var1(42);

try {
    long l = var1.convert_cast<long>();
    std::string s = var1.convert_cast<std::string>();
} catch(FB::variant::bad_cast& e) {
    std::cerr << "ouch: " << e.what();
}

std::string s("hi");
FB::variant var2(s);
assert(var2.get_type() == typeid(std::string));

  • No labels

1 Comment

  1. Unknown User (pnd1305)

    变量数据类型类,方便数据类型之间的转换。本意上varent变量类是个模板,支持所有类型数据,但基于js只支持有限类型:

    注如使用JSAPIAuto对象,这些类型参数可以直接使用而自动转换,其它的要用useconvert_cast转换