Dependency Injection (need help to compile on VS 2013)

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Dependency Injection (need help to compile on VS 2013)

Post by REDDemon »

I wrote a simple class for doing DI :). I'm gradually converting to this "pattern", and I wanted to know your opinions about the topic. I'm heavily changing the way I do coding.

https://code.google.com/p/infectorpp/

If you had any of the following problems when developing games you might want to take a look to the project (beware: requires c++11)

- Bored to pass a Context around
- Always have to relies on Device, Driver or super classes to retrieve what you need
- Usage of service locators (basically same of Device, a class you use to access other classes)
- Usage of singletons (Service locator in its worst shape)
- Difficult to change existing code even if you planned it well using design patterns
- Monolitic classes
- Memory leaks that are NOT located in stuff like data containers (anything that could be reference counted in example).
- You use exceptions and you always have to double check constructors to avoid memory leaks


The basic idea is to focus classes only on their role. Creation of devices and dependencies is "left to someone else (the IoC container)". The only classes that instantiate something are factories. I needed few months to grasp the concept so I don't pretend it will be easy for you. Basically if you need something, you ask for it in constructor, and someone will magically provide that dependency to you. You don't have to warry "were" to instantiate needed dependencies. This make the project more flexible and easy to change.

Using the "library" (6 methods and 1 class) does immediatly brings advantages, but you also need to understand how it works to have its full potential at use.

If anyone used Spring framework, this library is a subset of Spring functionalities (more polished, and adapted to C++ with the very essential features. user friendly and less error-prone, of course C++ lack certain features like reflection and so DI can't be done in the same way other frameworks do it in other languages.)
Last edited by REDDemon on Tue Sep 16, 2014 10:22 pm, edited 1 time in total.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Dependency Injection Pattern in games?

Post by REDDemon »

Lol actually I'm in a pretty funny situation, I can't affort a Win7 license, so I can't install VS 2012/2013 on my machine. Actually my library is c++11 compliant, but VS has some bugs that prevents compilation of my code. ( I did some workaround based on users feedback, but without VS 2013, I cannot be sure that now the code compiles fine also under MSVC)

I need someone very kind that try to compile the following package:

https://infectorpp.googlecode.com/svn/t ... -test2.zip

- Compile and run "UnitTest.cpp"
- Copypaste console output here
- No external dependencies (all required headers are inside "include" folder)
- Requires VS 2013 TP 2(constexpr required)
- If compilation fails copy past error so I can try a 2nd fix. :/

You will be mentioned in credits of course:)
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Dependency Injection (need help to compile on VS 2013)

Post by kklouzal »

Code: Select all

1>c:\users\kyle\desktop\thing\include\infectorpp\InfectorHelpers.hpp(47): error C2144: syntax error : 'bool' should be preceded by ';'
1>c:\users\kyle\desktop\thing\include\infectorpp\InfectorHelpers.hpp(52): error C2144: syntax error : 'bool' should be preceded by ';'
EDIT: After following the information here to get the new CTP http://blogs.msdn.com/b/vcblog/archive/ ... 3-ctp.aspx lots of compilation errors :P

Code: Select all

1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(160): error C2039: 'function' : is not a member of 'std'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(160): error C2065: 'function' : undeclared identifier
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(160): error C2062: type 'void' unexpected
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(160): error C2976: 'std::unordered_map' : too few template arguments
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\unordered_map(79) : see declaration of 'std::unordered_map'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(160): error C2059: syntax error : '>'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(161): error C2238: unexpected token(s) preceding ';'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(165): error C2039: 'function' : is not a member of 'std'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(165): error C2065: 'function' : undeclared identifier
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(165): error C2059: syntax error : ','
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(165): error C2143: syntax error : missing ',' before ')'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(165): error C2059: syntax error : '>'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(166): error C2238: unexpected token(s) preceding ';'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(167): error C2039: 'function' : is not a member of 'std'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(167): error C2065: 'function' : undeclared identifier
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(167): error C2059: syntax error : ','
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(167): error C2143: syntax error : missing ',' before ')'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(167): error C2059: syntax error : '>'
1>C:\Users\Kyle\Desktop\thing\include\Infectorpp\InfectorContainer.hpp(168): error C2238: unexpected token(s) preceding ';'
I know a few of these just require #include's but i'm just keeping you updated with the process I'm having to go through. :)

EDIT: After adding

Code: Select all

#include <functional>
to line 27 of UnitTest.cpp, only one compilation error:

Code: Select all

1>c:\users\kyle\desktop\thing\include\infectorpp\Infector_private.hpp(122): error C2280: 'std::unique_ptr<GoodClass,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function
Dream Big Or Go Home.
Help Me Help You.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Dependency Injection (need help to compile on VS 2013)

Post by REDDemon »

Many Thanks... the workaround :( still doesn't work. I tried to add "std::move", but seems that VS is actually unable to move the unique_ptr and instead it try only the copy constructor (even if it is never called with GCC and Clang). I see no workaround for that :(

I'll report another ticked for this code:/

Code: Select all

 
 
class ContainerTest{
public:
    template <typename T>
    std::shared_ptr<T> buildShared(){
        return std::move( std::make_shared<int>(4));
    }
 
    template <typename T>
    std::unique_ptr<T> buildUnique(){
        return std::move( std::unique_ptr<int>(new int(4)));
    }
};
 
template <typename OBJ>
class UniqueOrSharedTest{
        ContainerTest * ioc=nullptr;
    public:
        UniqueOrSharedTest(ContainerTest * ptr)
        :ioc(ptr){}
 
        operator std::shared_ptr<OBJ>(){
            return std::move(std::shared_ptr<OBJ>(std::move(ioc->buildShared<OBJ>())));
        }
 
        operator std::unique_ptr<OBJ>(){
            return std::move(std::unique_ptr<OBJ>(std::move(ioc->buildUnique<OBJ>())));
        }
};
 
class BuildMeFail{
public:
    BuildMeFail(std::shared_ptr<int> a, std::unique_ptr<int> b){
        std::cout<<"VS BUG"<<std::endl;
    }
};
 
void MoveConstructorAttemp(){
    ContainerTest p;
    BuildMeFail * a = new BuildMeFail(UniqueOrSharedTest<int>(&p),UniqueOrSharedTest<int>(&p));
    delete a;
}
 
EDIT:
bug reported ( I reported the link so I can follow it XD)
https://connect.microsoft.com/VisualStu ... -by-sfinae

I have 2 solutions:
- Change design & API to make infector not using implicit cast operator (that would be a whole new library, the implicit cast make the API really easy to use)
- Wait for a bugfix of VS
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply