本文整理汇总了C++中variant类的典型用法代码示例。如果您正苦于以下问题:C++ variant类的具体用法?C++ variant怎么用?C++ variant使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了variant类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: set_value
void scrollbar_widget::set_value(const std::string& key, const variant& v)
{
if(key == "on_scroll") {
ffl_handler_ = get_environment()->create_formula(v["on_scroll"]);
} else if(key == "up_arrow") {
up_arrow_ = widget_factory::create(v, get_environment());
} else if(key == "down_arrow") {
down_arrow_ = widget_factory::create(v, get_environment());
} else if(key == "handle") {
handle_ = widget_factory::create(v, get_environment());
} else if(key == "handle_bottom") {
handle_bot_ = widget_factory::create(v, get_environment());
} else if(key == "handle_top") {
handle_top_ = widget_factory::create(v, get_environment());
} else if(key == "background") {
background_ = widget_factory::create(v, get_environment());
} else if(key == "range") {
std::vector<int> range = v.as_list_int();
ASSERT_EQ(range.size(), 2);
set_range(range[0], range[1]);
} else if(key == "position") {
window_pos_ = v.as_int();
clip_window_position();
}
widget::set_value(key, v);
}
示例2: is_valid
void variant::swap(variant& other)
{
if (this == &other)
return;
const bool is_this_valid = is_valid();
const bool is_other_valid = other.is_valid();
if (!is_this_valid && !is_other_valid)
return;
if (is_this_valid && is_other_valid)
{
detail::variant_data tmp_data;
detail::variant_policy_func tmp_policy_func = other.m_policy;
other.m_policy(detail::variant_policy_operation::SWAP, other.m_data, tmp_data);
m_policy(detail::variant_policy_operation::SWAP, m_data, other.m_data);
other.m_policy = m_policy;
tmp_policy_func(detail::variant_policy_operation::SWAP, tmp_data, m_data);
m_policy = tmp_policy_func;
}
else
{
detail::variant_data& full_data = is_this_valid ? m_data : other.m_data;
detail::variant_data& empty_data = is_this_valid ? other.m_data : m_data;
detail::variant_policy_func full_policy_func = is_this_valid ? m_policy : other.m_policy;
full_policy_func(detail::variant_policy_operation::SWAP, full_data, empty_data);
std::swap(m_policy, other.m_policy);
}
}
示例3: set_value
void dropdown_widget::set_value(const std::string& key, const variant& v)
{
if(key == "on_change") {
on_change_ = boost::bind(&dropdown_widget::change_delegate, this, _1);
change_handler_ = get_environment()->create_formula(v);
} else if(key == "on_select") {
on_select_ = boost::bind(&dropdown_widget::select_delegate, this, _1, _2);
select_handler_ = get_environment()->create_formula(v);
} else if(key == "item_list") {
list_ = v.as_list_string();
current_selection_ = 0;
} else if(key == "selection") {
current_selection_ = v.as_int();
} else if(key == "type") {
std::string s = v.as_string();
if(s == "combo" || s == "combobox") {
type_ = DROPDOWN_COMBOBOX;
} else if(s == "list" || s == "listbox") {
type_ = DROPDOWN_LIST;
} else {
ASSERT_LOG(false, "Unreognised type: " << s);
}
}
widget::set_value(key, v);
}
示例4: type_error
variant variant::operator/(const variant& v) const
{
if(type_ == TYPE_DECIMAL || v.type_ == TYPE_DECIMAL) {
int denominator = v.as_decimal();
if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());
}
long long long_int = as_decimal();
long_int *= 10000;
long_int /= denominator;
if( long_int%10 >= 5) {
long_int /= 10;
++long_int;
} else
long_int/=10;
return variant( static_cast<int>(long_int) , variant::DECIMAL_VARIANT);
}
const int numerator = as_int();
const int denominator = v.as_int();
if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());;
}
return variant(numerator/denominator);
}
示例5: from_variant
int from_variant(variant node)
{
if(node.is_string()) {
return from_string(node.as_string());
}
return node.as_int(-1);
}
示例6: set
void BlendMode::set(const variant& node)
{
if(node.is_string()) {
const std::string& blend = node.as_string();
if(blend == "add") {
set(BlendModeConstants::BM_ONE, BlendModeConstants::BM_ONE);
} else if(blend == "alpha_blend") {
set(BlendModeConstants::BM_SRC_ALPHA, BlendModeConstants::BM_ONE_MINUS_SRC_ALPHA);
} else if(blend == "colour_blend" || blend == "color_blend") {
set(BlendModeConstants::BM_SRC_COLOR, BlendModeConstants::BM_ONE_MINUS_SRC_COLOR);
} else if(blend == "modulate") {
set(BlendModeConstants::BM_DST_COLOR, BlendModeConstants::BM_ZERO);
} else if(blend == "src_colour one" || blend == "src_color one") {
set(BlendModeConstants::BM_SRC_COLOR, BlendModeConstants::BM_ONE);
} else if(blend == "src_colour zero" || blend == "src_color zero") {
set(BlendModeConstants::BM_SRC_COLOR, BlendModeConstants::BM_ZERO);
} else if(blend == "src_colour dest_colour" || blend == "src_color dest_color") {
set(BlendModeConstants::BM_SRC_COLOR, BlendModeConstants::BM_DST_COLOR);
} else if(blend == "dest_colour one" || blend == "dest_color one") {
set(BlendModeConstants::BM_DST_COLOR, BlendModeConstants::BM_ONE);
} else if(blend == "dest_colour src_colour" || blend == "dest_color src_color") {
set(BlendModeConstants::BM_DST_COLOR, BlendModeConstants::BM_SRC_COLOR);
} else {
ASSERT_LOG(false, "BlendMode: Unrecognised scene_blend mode " << blend);
}
} else if(node.is_list() && node.num_elements() >= 2) {
ASSERT_LOG(node[0].is_string() && node[1].is_string(),
"BlendMode: Blend mode must be specified by a list of two strings.");
set(parse_blend_string(node[0].as_string()), parse_blend_string(node[1].as_string()));
} else {
ASSERT_LOG(false, "BlendMode: Setting blend requires either a string or a list of greater than two elements." << node.to_debug_string());
}
}
示例7: create_model_map
std::string Visitor::visit(variant & object) {
create_model_map();
Derived *base = (Derived *)*object;
std::string classname;
if (!object) {
printf("visit was empty!\n");
return "";
}
if (ClassMap::is_recognized_classname(object.type().name())) {
classname = base->classname();
} else {
if (object.isString() ||
object.isType<bool>())
classname = "quoted";
else if (object.isType<std::vector<variant> >())
classname = "array";
else
classname = "literal";
}
method_dict_map_t method_dict_map = method_dictionary();
if (method_dict_map[classname]) {
return (this->*method_dict_map[classname])(object);
}
else {
printf("Visitor::visit failed to find method: %s(%s)\n", classname.c_str(), object.type().name());
return "";
}
}
示例8:
world::world(const variant& node)
: view_distance_(node["view_distance"].as_int(default_view_distance)),
seed_(node["seed"].as_int(0))
{
ASSERT_LOG(node.has_key("shader"), "Must have 'shader' attribute");
ASSERT_LOG(node["shader"].is_string(), "'shader' attribute must be a string");
shader_ = gles2::shader_program::get_global(node["shader"].as_string())->shader();
if(node.has_key("lighting")) {
lighting_.reset(new graphics::lighting(shader_, node["lighting"]));
}
if(node.has_key("objects")) {
for(int n = 0; n != node["objects"].num_elements(); ++n) {
add_object(new user_voxel_object(node["objects"][n]));
}
}
if(node.has_key("skybox")) {
skybox_.reset(new graphics::skybox(node["skybox"]));
}
if(node.has_key("chunks")) {
logic_.reset(new logical_world(node));
build_fixed(node["chunks"]);
} else {
build_infinite();
}
}
示例9: checked_
checkbox::checkbox(const variant& v, game_logic::formula_callable* e)
: checked_(false), button(v,e)
{
hpadding_ = v["hpad"].as_int(12);
if(v.has_key("padding")) {
ASSERT_LOG(v["padding"].num_elements() == 2, "Incorrect number of padding elements specifed." << v["padding"].num_elements());
hpadding_ = v["padding"][0].as_int();
}
checked_ = v["checked"].as_bool(false);
variant label_var = v["label"];
label_ = (label_var.is_map() || label_var.is_callable()) ? "" : label_var.as_string_default("Checkbox");
label_widget_ = (label_var.is_map() || label_var.is_callable())
? widget_factory::create(label_var, e)
: widget_ptr(new graphical_font_label(label_, "door_label", 2));
ASSERT_LOG(get_environment() != 0, "You must specify a callable environment");
click_handler_ = get_environment()->create_formula(v["on_click"]);
onclick_ = boost::bind(&checkbox::click, this, _1);
set_click_handler(boost::bind(&checkbox::on_click, this));
set_label(create_checkbox_widget(label_widget_,
checked_,
button_resolution(),
hpadding_));
if(v.has_key("width") || v.has_key("height")) {
set_dim(v["width"].as_int(width()), v["height"].as_int(height()));
}
}
示例10: manhattan_distance
template<> decimal manhattan_distance(const variant& p1, const variant& p2) {
const std::vector<decimal>& v1 = p1.as_list_decimal();
const std::vector<decimal>& v2 = p2.as_list_decimal();
decimal x1 = v1[0] - v2[0];
decimal x2 = v1[1] - v2[1];
return (x1 < 0 ? -x1 : x1) + (x2 < 0 ? -x2 : x2);
}
示例11: variant
variant variant::operator*(const variant& v) const
{
if(type_ == TYPE_DECIMAL || v.type_ == TYPE_DECIMAL) {
return variant(as_decimal() * v.as_decimal());
}
if(type_ == TYPE_LIST) {
int ncopies = v.as_int();
if(ncopies < 0) {
ncopies *= -1;
}
const std::vector<variant>& items = list_->elements;
std::vector<variant> res;
res.reserve(items.size()*ncopies);
for(int n = 0; n != ncopies; ++n) {
for(int m = 0; m != items.size(); ++m) {
res.push_back(items[m]);
}
}
return variant(&res);
}
return variant(as_int() * v.as_int());
}
示例12: as_decimal
bool variant::operator==(const variant& v) const
{
if(type_ != v.type_) {
if(type_ == TYPE_DECIMAL || v.type_ == TYPE_DECIMAL) {
return as_decimal() == v.as_decimal();
}
return false;
}
switch(type_) {
case TYPE_NULL: {
return v.is_null();
}
case TYPE_STRING: {
return string_->str == v.string_->str;
}
case TYPE_INT: {
return int_value_ == v.int_value_;
}
case TYPE_DECIMAL: {
return decimal_value_ == v.decimal_value_;
}
case TYPE_LIST: {
if(num_elements() != v.num_elements()) {
return false;
}
for(size_t n = 0; n != num_elements(); ++n) {
if((*this)[n] != v[n]) {
return false;
}
}
return true;
}
case TYPE_MAP: {
return map_->elements == v.map_->elements;
}
case TYPE_CALLABLE_LOADING: {
return false;
}
case TYPE_CALLABLE: {
return callable_->equals(v.callable_);
}
case TYPE_FUNCTION: {
return fn_ == v.fn_;
}
}
assert(false);
return false;
}
示例13: f
std::string formula_ai::evaluate(const std::string& formula_str)
{
try{
game_logic::formula f(formula_str, &function_table_);
game_logic::map_formula_callable callable(this);
callable.add_ref();
//formula_debugger fdb;
const variant v = f.evaluate(callable,NULL);
if (ai_ptr_) {
variant var = execute_variant(v, *ai_ptr_, true );
if ( !var.is_empty() ) {
return "Made move: " + var.to_debug_string();
}
}
return v.to_debug_string();
}
catch(formula_error& e) {
e.line = 0;
handle_exception(e);
throw;
}
}
示例14: parse_dir
std::vector<CastlePart> parse_dir(const variant& node)
{
std::vector<CastlePart> res;
const std::vector<std::string> keys = { "bl", "br", "l", "r", "tl", "tr" };
ASSERT_LOG(node.is_map(), "must be a map type.");
for(auto key : keys) {
ASSERT_LOG(node.has_key(key), "Must have attribute '" << key << "' in definition.");
const variant& dir = node[key];
ASSERT_LOG(dir.has_key("rect"), "Attribute '" << key << "' must have 'rect' definition.");
CastlePart cp;
cp.r_ = rect(dir["rect"]);
if(dir.has_key("border")) {
ASSERT_LOG(dir["border"].num_elements() == 4, "'border' attribute must be list of 4 integers.");
for(int n = 0; n != 4; ++n) {
cp.border_[n] = dir["border"][n].as_int32();
}
} else {
for(int n = 0; n != 4; ++n) {
cp.border_[n] = 0;
}
}
if(dir.has_key("offset")) {
cp.offs_ = point(dir["offset"]);
} else {
cp.offs_.x = cp.offs_.y = 0;
}
res.emplace_back(cp);
}
return res;
}
示例15: variant
variant variant::operator-(const variant& v) const
{
if(type_ == TYPE_DECIMAL || v.type_ == TYPE_DECIMAL) {
return variant( as_decimal() - v.as_decimal() , DECIMAL_VARIANT);
}
return variant(as_int() - v.as_int());
}