Create bsoncxx::builder::basic::document from a string? - mongo-cxx-driver

Is there a way to create a bsoncxx::builder::basic::document from a std::string?
I know how to build a document using mongocxx functions such as make_document, kvp, make_array, as well as through the streaming functions.
But what I want to know is if we can create a std::string representation of a document and make that into a bsoncxx::builder::basic::document?

Assuming that this std::string contains JSON, then yes. You can invoke bsoncxx::from_json.
If, somehow, the std::string buffer contains BSON data rather than JSON, you could copy the data inside the string into a new buffer and use one of the first two bsoncxx::document::value::value constructors, which take ownership of a buffer.
You could also construct a temporary bsoncxx::document::view over the buffer in the std::string by passing the underlying .data() and .length() of the string to the two argument bsoncxx::view::view constructor, and then pass that view along to the appropriate bsoncxx::value constructor, which will copy the data in the view.

Related

The argument type 'ListOf5' can't be assigned to the parameter type 'ListOf5'

I need to implement an abstract class function, which own a an specific data type. But I need inside my logic layer to make the attribute which is going to be passed as a dynamic data type. But when i Pass it to the function, i am sure that its data type will be as needed. So, i type (product.value.pickedImages) as ListOf5) . But it does an Exception.
The Abstract Class Code Is:
Future<Either<FireStoreServerFailures, List<String>>> uploadProductImages(
{required ListOf5<File> images});
The Implementation Code Is:
Future<Option<List<String>>> _uploadImagesToFirestorage() async {
return await productRepo
.uploadProductImages(
images: (product.value.pickedImages) as ListOf5<File>) // Exception
}
The Exception Is:
The argument type 'ListOf5 < dynamic>' can't be assigned to the
parameter type 'ListOf5 < File>'.
You are trying to cast the List from List<dynamic> to List<String>.
Instead, you should cast each item, using something like this:
void main() {
List<dynamic> a = ['qwerty'];
print(List<String>.from(a));
}
Not sure about the implementation of this ListOf5 though...
The cast (product.value.pickedImages) as ListOf5<File> fails.
It fails because product.value.pickedImages is-not-a ListOf5<File>, but instead of ListOf5<dynamic> (which may or may not currently contain only File objects, but that's not what's being checked).
Unlike a language like Java, Dart retains the type arguments at run-time(it doesn't do "erasure"), so a ListOf5<dynamic> which contains only File objects is really different from a ListOf5<File> at run-time.
You need to convert the ListOf5<dynamic> to a ListOf5<File>.
How to do that depends on the type ListOf5, which I don't know.
For a normal List, the two most common options are:
(product.value.pickedImages).cast<File>(). Wraps the existing list and checks on each read that you really do read a File. It throws if you ever read a non-File from the original list. Perfectly fine if you'll only read the list once.
List<File>.of(product.value.pickedImages). Creates a new List<File> containing the values of product.value.pickedImages, and throws if any of the values are not File objects. Requires more memory (because it copies the list), but fails early in case there is a problem, and for small lists, the overhead is unlikely to be significant. If you read the resulting list many times, it'll probably be more efficient overall.
If the ListOf5 class provides similar options, you can use those. If not, you might have to build a new ListOf5 manually, casting each element of the existing ListOf5<dynamic> yourself.
(If the ListOf5 class is your own, you can choose to add such functionality to the class).

Without knowing the userdata implementation, can I use its fields from inside C++?

I can obtain a userdata from inside my C++ code. I want to know if I can cast it to something so I can dereference its fields and invoke its methods without going through Lua.
In other words:
Once I obtain an userdata and put it in the Lua stack, how can I get it out and use it as an object of a certain class? I know all fields and methods that I'm interested in (their Lua names), but not necessarily all fields and method the userdata underlying implementation provides.
Is there a way to somehow create an interface that would know the offsets of each member?
I believe the question is, how do I know the offset of each member in the Lua view of the userdata, so I can create a struct to cast the void * returned by lua_touserdata to?
Userdata is just a binary blob. Array of bytes. How those bytes will be interpreted - depends entirely on interface provided with that userdata, be it metatable with methods, or some other agreements.
If that userdata created by your code, then you either know what type of native object is there (knowing that some func generate or accept only specific type), or you can place type identifier as first bytes of userdata blob, so you can check that id and switch/cast userdata pointer to a specific class.
If that userdata created by someone's code - you can only rely on methods provided within its metatable (you can access it from native code), or just treat is as array of bytes, without possibly knowing actual type of data stored there. Or do some hackish guesses, making it working in some cases, and crashing badly if your guess wasn't correct.

How to return a struct from an imported DLL-function in MQL4?

Is there a way to return a struct from an imported function in MQL4, without having to pass it as a parameter and making a memcpy?
Be cautious with any kind of DLL-interfacing, MQL4 Documentation states:
Passing ParametersAll parameters of simple types are passed by values unless it is explicitly indicated that they are passed by reference. When a string is passed, the address of the buffer of the copied string is passed; if a string is passed by reference, the address of the buffer of this string without copying it is passed to the function imported from DLL.Structures that contain dynamic arrays[], strings, classes, other complex structures, as well as static or dynamic arrays[] of the enumerated objects, can't be passed as a parameter to an imported function.When passing an array to DLL, the address of the beginning of the data buffer is always passed (irrespective of the AS_SERIES flag). A function inside a DLL knows nothing about the AS_SERIES flag, the passed array is a static array of an undefined length; an additional parameter should be used for specifying the array size.
More glitches apply... Then how to make it work?
Maybe a straight, heterogeneous multi-party distributed processing, which communicates rather results than function calls, independent of all nightmares of maintaining just DLL-imported functions API changes, is a way safer way to go. Using this approach for the last few years and since than have no problems with New-MQL4.56789 string-s that seized to remain string-s and silently started to become struct-s etc.
Worth to know about.
Anyway, welcome and enjoy the Wild Worlds of MQL4 -- may enjoy to click and read other posts on issues in MQL4/DLL integration and/or signalling/messaging in MQL4 domains. Feel free to ask more

Controlling the ref type in a Vapi file

I'm trying to write a Vapi file for MessagePack and am having a couple of issues, the first being that the resulting msgpack_object_print is incorrect because of the reference type of one of the parameters. The header file expects
void msgpack_object_print(FILE* out, msgpack_object o);
and my Vapi file contains
[CCode (instance_pos = 1.1)]
public void print (Posix.FILE out);
which generates the C output
msgpack_object_print (_tmp13_, &obj);
where obj is type msgpack_object *. This creates the error
examples/simple.c:173:34: error: incompatible type for argument 2 of ‘msgpack_object_print’
and it disappears if I remove the & from the generated C. So I'm wondering what my Vapi should contain to result in the correct output?
You can designated your msgpack_object class as [SimpleType] and it will be copied by value rather than by reference.
I have written a partial VAPI for MessagePack if you want to contribute back by using and testing it.
https://github.com/valum-framework/vala-extra-vapis/blob/msgpack/msgpack.vapi
Like already said, you need to use the [SimpleType] annotation on the class to have your type passed by value.
EDIT: Just adding that for bindings, it's a good thing to keep them in nemequ/vala-extra-vapis repository.

Extract function body

How can I extract function body (as a string)? For example I call C function, extract function from stack, check if type is LUA_TFUNCTION and what do I need to do to get its body?
When the function is on the stack, it has already been compiled. The best you can try to do is a lua_dump and then decode the bytecode.
You can call lua_getinfo with a string parameter of "S", then check the "source" member of the lua_Debug structure. If that string starts with '#', it's a filename, and you'll need to re-read the file if you want the source (Lua only read the file incrementally to load the function and never saved it as a string). Otherwise, its contents will be the string loaded as the chunk the function was defined in.
Note that in either case the source returned will be the entire chunk that defined the function in question. You can narrow the string down to only that function using the other fields defined in the structure: note, however, that this is not a guarantee that you will be able to load that string back in to get the same behavior (the function definition may refer to variables defined in an outer scope, for instance).
The Debug library can do this. The Lua C API doesn't have it, you'd want to call a Lua function for this purpose.

Resources