Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

boost::static_visitor

38 views
Skip to first unread message

Christopher Pisz

unread,
Aug 18, 2015, 12:44:46 PM8/18/15
to

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.

I've got this visitor so far:

// Write the current set of value name - value pairs
class WriteVisitor : boost::static_visitor<>
{
public:

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);
}
};



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.







--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

Öö Tiib

unread,
Aug 18, 2015, 6:07:45 PM8/18/15
to
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. ;)

Christopher Pisz

unread,
Aug 19, 2015, 11:15:47 AM8/19/15
to
You the man! Works great.
0 new messages