On Tuesday, 18 August 2015 19:44:46 UTC+3, Christopher Pisz wrote:
> I am trying to use a boost::variant type to set values that go into the
> Windows registry in my wrapper class, since they can be strings or 32bit
> ints or 64 bit ints.
>
> According to thier write up, you use a visitor pattern with it. I've
> never used the visitor pattern.
>
> I can't seem to figure out how to pass other needed data to the class
> that implements the visitor pattern and how to get a return value from it.
Typically parameters are passed with constructor of visitor object and
return value type as template argument of base type. I try to comment inline.
>
> I've got this visitor so far:
>
> // Write the current set of value name - value pairs
> class WriteVisitor : boost::static_visitor<>
That to be "static_visitor<LONG>"
> {
> public:
Constructor from 'HKEY' and store it as member. IOW:
WriteVisitor( HKEY registryKey )
: registryKey_(registryKey)
{}
HKEY registryKey_;
> LONG operator() (HKEY registryKey, int & value) const
> {
> return RegSetValueExA(registryKey, NULL, 0, REG_DWORD,
> reinterpret_cast<const BYTE *>(&value), sizeof(int));
> }
>
> LONG operator() (HKEY registryKey, std::string & value) const
> {
> return RegSetValueExA(registryKey, NULL, 0, REG_SZ,
> reinterpret_cast<const BYTE *>(value.c_str()), value.size() + 1);
> }
Change these to unary operators that use 'registryKey_' member instead
of first argument.
> };
>
>
>
> and I've got this type I want to pass to it as "value"
> typedef boost::variant<std::string, int> RegistryValueType;
>
>
> and I want to get the LONG result.
>
> I'm not sure how to call it or if I made the visitor correctly. Help pls.
If you did the changes then something like:
RegistryValueType rval = ...; // fill the ...
HKEY hkey = ...; // fill the ...
if ( boost::apply_visitor( WriteVisitor(hkey), rval ) != ERROR_SUCCESS )
{
// handle windows api error ...
}
It might do the trick. ;)