int main() { std::vector<Variant> bunchOStuff; bunchOStuff.push_back(make_variant(5)); bunchOStuff.push_back(make_variant(2.34)); bunchOStuff.push_back(make_variant("This is a string")); for (auto& x : bunchOStuff) { switch (x->getType()) { case Variant_Type::Type::doubleType: std::cout << x->asDouble(); break; case Variant_Type::Type::intType: std::cout << x->asInt(); break; case Variant_Type::Type::stringType: std::cout << x->asString(); break; } std::cout << "\n"; } return 0; }
Datum variant_cast_in(PG_FUNCTION_ARGS) { VariantInt vi = palloc0(sizeof(*vi)); vi->isnull = PG_ARGISNULL(0); vi->typid = get_fn_expr_argtype(fcinfo->flinfo, 0); vi->typmod = get_fn_expr_argtypmod(fcinfo->flinfo, 0); Assert(!fcinfo->flinfo->fn_strict); /* Must be callable on NULL input */ if (!OidIsValid(vi->typid)) elog(ERROR, "could not determine data type of input"); /* Validate that we're casting to a registered variant */ if( PG_ARGISNULL(1) ) elog( ERROR, "Target typemod must not be NULL" ); variant_get_variant_name(PG_GETARG_INT32(1), vi->typid, false); if( !vi->isnull ) vi->data = PG_GETARG_DATUM(0); /* Since we're casting in, we'll call for INFunc_input, even though we don't need it */ PG_RETURN_VARIANT( make_variant(vi, fcinfo, IOFunc_input) ); }
static Variant variant_in_int(FunctionCallInfo fcinfo, char *input, int variant_typmod) { VariantCache *cache; bool isnull; Oid intTypeOid = InvalidOid; int32 typmod = 0; text *orgType; text *orgData; VariantInt vi = palloc0(sizeof(*vi)); /* Eventually getting rid of this crap, so segregate it */ intTypeOid = getIntOid(); FmgrInfo proc; Datum composite; HeapTupleHeader composite_tuple; Oid typioparam; Oid typIoFunc; /* Cast input data to our internal composite type */ getTypeInputInfo(intTypeOid, &typIoFunc, &typioparam); fmgr_info_cxt(typIoFunc, &proc, fcinfo->flinfo->fn_mcxt); composite=InputFunctionCall(&proc, input, typioparam, typmod); /* Extract data from internal composite type */ composite_tuple=DatumGetHeapTupleHeader(composite); orgType = (text *) GetAttributeByNum( composite_tuple, 1, &isnull ); if (isnull) elog(ERROR, "original_type of variant must not be NULL"); orgData = (text *) GetAttributeByNum( composite_tuple, 2, &vi->isnull ); /* End crap */ #ifdef LONG_PARSETYPE parseTypeString(text_to_cstring(orgType), &vi->typid, &vi->typmod, false); #else parseTypeString(text_to_cstring(orgType), &vi->typid, &vi->typmod); #endif /* * Verify we've been handed a valid typmod */ variant_get_variant_name(variant_typmod, vi->typid, false); cache = get_cache(fcinfo, vi, IOFunc_input); if (!vi->isnull) /* Actually need to be using stringTypeDatum(Type tp, char *string, int32 atttypmod) */ vi->data = InputFunctionCall(&cache->proc, text_to_cstring(orgData), cache->typioparam, vi->typmod); return make_variant(vi, fcinfo, IOFunc_input); }