函数拒绝在 Boost 测试函数中工作

发布于 2024-12-24 17:35:51 字数 4454 浏览 2 评论 0原文

我无法理解为什么在类构造函数中我可以调用这个函数,但是当在测试函数中调用时,它会错误地显示

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

我想做的是为我的项目创建一个插件引擎,同时使其可重用, C++ 文件正在确保其正常工作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2024-12-31 17:35:51

这看起来不像 Boost Test 的问题,本身,但它看起来像 addobjecttoengine 中的行...

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

...应该是...

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

.. .与FakeApplication 的构造函数相同。然后在 CheckAddonEngine.cpp:64 处无需执行任何转换。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文