0

I cant understand why in the class constructor I can call this function but when called in the test function, it errors out with

E:\Projects\NasuTek-Plugin-Engine\tests\CheckAddonEngine.cpp:64: error: conversion from 'std::auto_ptr<FakeSettableFeaturePlugin>' to 'std::auto_ptr<FakeFeature>' is ambiguous

C++ File

#include <ObjectEngine.h>
#include <memory>

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

class FakeFeature : public PObject {
    public:
        inline virtual const char *returnSomthingCool() { return "Somthing Cool"; }
};

class FakeFeaturePlugin : public FakeFeature {
    public:
        inline const char *returnSomthingCool() { return "Somthing Cool From a Plugin Object"; }
        inline std::string getName() { return "Fake Feature Plugin Object"; }
};

class FakeSettableFeaturePlugin : public FakeFeature {
    public:
        inline const char *returnSomthingCool() { return _value; }
        inline char *setSomthingCool(const char *value) { _value = const_cast<char *>(value); }
        inline std::string getName() { return "Fake Feature Settable Plugin Object"; }
    private:
        char *_value;
};

typedef ObjectEngine<FakeFeature> FakeFeatureEngine;

class FakeApplication {
    public:
        inline FakeApplication(){
            _engine = new FakeFeatureEngine();

            std::auto_ptr<FakeFeature> pluginToAdd (new FakeFeaturePlugin);

            _engine->addObject(pluginToAdd); // Essencially what im doing where it errors, both classes inherit FakeFeature
        }

        inline ~FakeApplication() {
            delete _engine;
        }

        FakeFeatureEngine *_engine;
};

BOOST_AUTO_TEST_CASE(getengineobject) {
    FakeApplication *application = new FakeApplication();
    FakeFeature &feature = application->_engine->getObject("Fake Feature Plugin Object");

    BOOST_CHECK_EQUAL(feature.getName(), "Fake Feature Plugin Object");
    BOOST_CHECK_EQUAL(feature.returnSomthingCool(), "Somthing Cool From a Plugin Object");

    delete application;
}

BOOST_AUTO_TEST_CASE(addobjecttoengine) {
    FakeApplication *application = new FakeApplication();

    std::auto_ptr<FakeSettableFeaturePlugin> plugin (new FakeSettableFeaturePlugin);
    plugin.get()->setSomthingCool("Bro I Set this to this value ^_^");

    application->_engine->addObject(plugin); // This is the line that fails

    FakeFeature &feature = application->_engine->getObject("Fake Feature Settable Plugin Object");

    BOOST_CHECK_EQUAL(feature.getName(), "Fake Feature Settable Plugin Object");
    BOOST_CHECK_EQUAL(feature.returnSomthingCool(), "Bro I Set this to this value ^_^");

    delete application;
}

.h File

/**
  @file ObjectEngine.h
  @brief Header File with Plugin Engine Templates to make adding a plugin interface to your app easier
  */

#ifndef NASUTEKPLUGINENGINE_H
#define NASUTEKPLUGINENGINE_H

#include <boost/ptr_container/ptr_list.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <stdio.h>
#include <string>

class PObject {
    public:
        inline PObject() {}
        virtual std::string getName() = 0;
};

template<typename TObjectType>
class ObjectEngine {
    public:
        ObjectEngine() {}
        ~ObjectEngine() {
            _objects.clear();
        }
        void addObject(std::auto_ptr<TObjectType> obj) {
            if(boost::is_base_of<PObject, TObjectType>::value) {
                _objects.push_back(obj.release());
            }
        }
        TObjectType &getObject(std::string objectName) {
            typename boost::ptr_list<TObjectType>::iterator i;
            for(i = _objects.begin(); i != _objects.end(); i++) {
                if((*i).getName() == objectName) {
                    return *i;
                }
            }
            throw "Object does not exist.";
        }
        bool objectExist(std::string objectName) {
            typename boost::ptr_list<TObjectType *>::iterator i;
            for(i = _objects.begin(); i != _objects.end(); i++) {
                if((*i).getName() == objectName) {
                    return true;
                }
            }
            return false;
        }
    private:
        boost::ptr_list<TObjectType> _objects;
};

#endif // NASUTEKPLUGINENGINE_H

What I'm trying to do is create a plugin engine for my projects while making it reusable, and the C++ file is making sure its working right.

DrHouse
  • 99
  • 1
  • 8
  • Did you mean "`std::auto_ptr plugin(new FakeSettableFeaturePlugin);`", in `addobjecttoengine` the same as in `FakeApplication`? – johnsyweb Dec 31 '11 at 04:41
  • Its not the same class perse, but both `FakeSettableFeaturePlugin` from the line thats failing, and `FakeFeaturePlugin` from the line that works, all inherit `FakeFeature`. and the add function wants `std::auto_ptr`. – DrHouse Dec 31 '11 at 05:01
  • 1
    OOOOOOOOH I see my mistake now, Johnsyweb was correct, it was to be FakeFeature, I overlooked that. Thanks. – DrHouse Dec 31 '11 at 07:49
  • No worries. I've added this as an answer for you to accept. – johnsyweb Dec 31 '11 at 08:01

1 Answers1

3

This doesn't look like a problem with Boost Test, per se, but it looks like the line in addobjecttoengine...

std::auto_ptr<FakeSettableFeaturePlugin> plugin (new FakeSettableFeaturePlugin);

...should be...

std::auto_ptr<FakeFeature> plugin(new FakeSettableFeaturePlugin);

...the same as in FakeApplication's constructor. Then there's no conversion to perform at CheckAddonEngine.cpp:64.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247