views:

161

answers:

2

Is there a way of supplying enum values as method-args in pococapsule without resorting to factory-methods?

Let say I have a class that take an enum value in its constructor

class A
{
    A(myEnum val);
}

Using Pococapsule xml configuration: I would like to express something like this:

<bean id="A" class="A">
    <method-arg type="MyEnum" value="MyEnum::Value1" />
</bean>

However, since pococapsule's basic types only includes built in types such as short, char, etc this is not possible.
How would I go about to instantiate a class A using pococapsule?

I could resort to using factory methods something like this:

MyEnum GetMyEnumValue1()
{
    return MyEnum::Value1;
}

<bean id="A" class="A">
    <method-arg factory-method="GetMyEnumValue1" />
</bean>

Which isn't very practical. I would have to implement a new factory method for every possible value of each and every enum used.

Some would argue that enum:s shouldn't be passed in constructors or setter methods as it is a sign of a class doing to much. Yes I agree. However, there is a lot of third party code and c++ frameworks out there that uses this style, so I need to be able to do this.


Edit: The issue was resolved on Pococapsule's discussion forum. The work-around in this specific case was to have factory methods perform the desired action. It is not as flexible as declaring enum-use in the xml-config file, but it moved the project forward. Thanks Ke for your help.

A: 

In C/C++ enums are able to be passed as int implicitly, therefore, you can simply do:

You can also use the DSM feature to define your own extend schema that supports your specific enum (it should be similar to the user example in examples/basic-ioc/ext-schema where DSM is used to typesafely support user defined Map type).

-Ke

Ke Jin
Thank you for the Reply Ke Jin,I've tried to get hold of you but couldn't find your email on the PocoCapsule website, nor on your blog.The real world case I'm trying to use it on is a framework that has the same method overloaded with several different enums. Using your suggestion on using long doesn't work because the compiler finds the wrong overloaded method.If you had the time I would appreciate if you could contact me by email: z[at]consoft.se.I feel the problem might be too complex to solve through discussion here (correct answer will be posted when found).
Zen
A: 

(repost, as the XML code was filtered out in previous reply)

In C/C++ enums are able to be passed as int implicitly, therefore, you can simply have type="long" in the method-arg element.

You can also use the DSM feature to define your own extend schema that supports your specific enum (it should be similar to the user example in examples/basic-ioc/ext-schema where DSM is used to typesafely support user defined Map type).

-Ke

Ke Jin