How to call a function on header inclusion?











up vote
3
down vote

favorite












I'm working on a simple framework for making 2D games. It uses components and systems, which will vary from game to game.



To make it easy for other parts of the engine to loop over all possible systems and / or components, I'd like them to let themselves be known the moment one of them is included (Each has their own header file), in a way creating a list of all possible component types and system types.



I've currently solved this by having a Register struct which is put at the bottom after a system or component definition, passing that component / system pointer as an argument to the constructer of the Register struct, i.e.:



std::vector<Component*> Components    
struct Register{
Register(Component* newComponent){
Components.push_back(newComponent);
}
}


Which is then used at the bottom of each component's header:



 Register 2DPosReg(&2DPos);


Which makes sure that before we get to our main code all components are listed in Components. In the same fashion I also add the names of these components and some other details to some global vectors.
However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.



Is there any other way where including the header will make itself 'known' to the rest of the code?



I'd like to avoid my previous solution where I had a long Register(&2DPos, &Vel, &Acc, ...etc) function that would register all options, as any changes to the used components would require re-editing this function.



(Also, first stackoverflow question, apologies if it's long / has beginners mistakes)










share|improve this question









New contributor




Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • IMO your code architecture looks suspicious. What exactly is Component in your code? The way it is now it looks like you can only have a single component of each type, which does not seem very useful. Or are you trying to create a type-information like system that only cares about which types of components exist in code?
    – UnholySheep
    2 days ago










  • Correct, this only stores information on the possible types of components and systems. Each component has it's own unordered_map where it gets stored based on it's ID.
    – Lapin
    2 days ago










  • By the way, no need to apologize. If more first timers put in the effort you've put into phrasing your question, SO would be a better place
    – StoryTeller
    2 days ago










  • The unforgettable-factory may be what you want.
    – user2709407
    2 days ago






  • 1




    It is just fine, the 4 or 8 bytes it costs is nothing to worry about. So cheap that you'd consider doing this for each individual component. But it belongs in a .cpp file, not a .h file. Focus a bit on how you package and discover the component. A shared library (aka dll) and either a config list of plugin names or scanning a directory are obvious approaches.
    – Hans Passant
    2 days ago















up vote
3
down vote

favorite












I'm working on a simple framework for making 2D games. It uses components and systems, which will vary from game to game.



To make it easy for other parts of the engine to loop over all possible systems and / or components, I'd like them to let themselves be known the moment one of them is included (Each has their own header file), in a way creating a list of all possible component types and system types.



I've currently solved this by having a Register struct which is put at the bottom after a system or component definition, passing that component / system pointer as an argument to the constructer of the Register struct, i.e.:



std::vector<Component*> Components    
struct Register{
Register(Component* newComponent){
Components.push_back(newComponent);
}
}


Which is then used at the bottom of each component's header:



 Register 2DPosReg(&2DPos);


Which makes sure that before we get to our main code all components are listed in Components. In the same fashion I also add the names of these components and some other details to some global vectors.
However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.



Is there any other way where including the header will make itself 'known' to the rest of the code?



I'd like to avoid my previous solution where I had a long Register(&2DPos, &Vel, &Acc, ...etc) function that would register all options, as any changes to the used components would require re-editing this function.



(Also, first stackoverflow question, apologies if it's long / has beginners mistakes)










share|improve this question









New contributor




Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • IMO your code architecture looks suspicious. What exactly is Component in your code? The way it is now it looks like you can only have a single component of each type, which does not seem very useful. Or are you trying to create a type-information like system that only cares about which types of components exist in code?
    – UnholySheep
    2 days ago










  • Correct, this only stores information on the possible types of components and systems. Each component has it's own unordered_map where it gets stored based on it's ID.
    – Lapin
    2 days ago










  • By the way, no need to apologize. If more first timers put in the effort you've put into phrasing your question, SO would be a better place
    – StoryTeller
    2 days ago










  • The unforgettable-factory may be what you want.
    – user2709407
    2 days ago






  • 1




    It is just fine, the 4 or 8 bytes it costs is nothing to worry about. So cheap that you'd consider doing this for each individual component. But it belongs in a .cpp file, not a .h file. Focus a bit on how you package and discover the component. A shared library (aka dll) and either a config list of plugin names or scanning a directory are obvious approaches.
    – Hans Passant
    2 days ago













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'm working on a simple framework for making 2D games. It uses components and systems, which will vary from game to game.



To make it easy for other parts of the engine to loop over all possible systems and / or components, I'd like them to let themselves be known the moment one of them is included (Each has their own header file), in a way creating a list of all possible component types and system types.



I've currently solved this by having a Register struct which is put at the bottom after a system or component definition, passing that component / system pointer as an argument to the constructer of the Register struct, i.e.:



std::vector<Component*> Components    
struct Register{
Register(Component* newComponent){
Components.push_back(newComponent);
}
}


Which is then used at the bottom of each component's header:



 Register 2DPosReg(&2DPos);


Which makes sure that before we get to our main code all components are listed in Components. In the same fashion I also add the names of these components and some other details to some global vectors.
However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.



Is there any other way where including the header will make itself 'known' to the rest of the code?



I'd like to avoid my previous solution where I had a long Register(&2DPos, &Vel, &Acc, ...etc) function that would register all options, as any changes to the used components would require re-editing this function.



(Also, first stackoverflow question, apologies if it's long / has beginners mistakes)










share|improve this question









New contributor




Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm working on a simple framework for making 2D games. It uses components and systems, which will vary from game to game.



To make it easy for other parts of the engine to loop over all possible systems and / or components, I'd like them to let themselves be known the moment one of them is included (Each has their own header file), in a way creating a list of all possible component types and system types.



I've currently solved this by having a Register struct which is put at the bottom after a system or component definition, passing that component / system pointer as an argument to the constructer of the Register struct, i.e.:



std::vector<Component*> Components    
struct Register{
Register(Component* newComponent){
Components.push_back(newComponent);
}
}


Which is then used at the bottom of each component's header:



 Register 2DPosReg(&2DPos);


Which makes sure that before we get to our main code all components are listed in Components. In the same fashion I also add the names of these components and some other details to some global vectors.
However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.



Is there any other way where including the header will make itself 'known' to the rest of the code?



I'd like to avoid my previous solution where I had a long Register(&2DPos, &Vel, &Acc, ...etc) function that would register all options, as any changes to the used components would require re-editing this function.



(Also, first stackoverflow question, apologies if it's long / has beginners mistakes)







c++ game-engine c++17






share|improve this question









New contributor




Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago





















New contributor




Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Lapin

185




185




New contributor




Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Lapin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • IMO your code architecture looks suspicious. What exactly is Component in your code? The way it is now it looks like you can only have a single component of each type, which does not seem very useful. Or are you trying to create a type-information like system that only cares about which types of components exist in code?
    – UnholySheep
    2 days ago










  • Correct, this only stores information on the possible types of components and systems. Each component has it's own unordered_map where it gets stored based on it's ID.
    – Lapin
    2 days ago










  • By the way, no need to apologize. If more first timers put in the effort you've put into phrasing your question, SO would be a better place
    – StoryTeller
    2 days ago










  • The unforgettable-factory may be what you want.
    – user2709407
    2 days ago






  • 1




    It is just fine, the 4 or 8 bytes it costs is nothing to worry about. So cheap that you'd consider doing this for each individual component. But it belongs in a .cpp file, not a .h file. Focus a bit on how you package and discover the component. A shared library (aka dll) and either a config list of plugin names or scanning a directory are obvious approaches.
    – Hans Passant
    2 days ago


















  • IMO your code architecture looks suspicious. What exactly is Component in your code? The way it is now it looks like you can only have a single component of each type, which does not seem very useful. Or are you trying to create a type-information like system that only cares about which types of components exist in code?
    – UnholySheep
    2 days ago










  • Correct, this only stores information on the possible types of components and systems. Each component has it's own unordered_map where it gets stored based on it's ID.
    – Lapin
    2 days ago










  • By the way, no need to apologize. If more first timers put in the effort you've put into phrasing your question, SO would be a better place
    – StoryTeller
    2 days ago










  • The unforgettable-factory may be what you want.
    – user2709407
    2 days ago






  • 1




    It is just fine, the 4 or 8 bytes it costs is nothing to worry about. So cheap that you'd consider doing this for each individual component. But it belongs in a .cpp file, not a .h file. Focus a bit on how you package and discover the component. A shared library (aka dll) and either a config list of plugin names or scanning a directory are obvious approaches.
    – Hans Passant
    2 days ago
















IMO your code architecture looks suspicious. What exactly is Component in your code? The way it is now it looks like you can only have a single component of each type, which does not seem very useful. Or are you trying to create a type-information like system that only cares about which types of components exist in code?
– UnholySheep
2 days ago




IMO your code architecture looks suspicious. What exactly is Component in your code? The way it is now it looks like you can only have a single component of each type, which does not seem very useful. Or are you trying to create a type-information like system that only cares about which types of components exist in code?
– UnholySheep
2 days ago












Correct, this only stores information on the possible types of components and systems. Each component has it's own unordered_map where it gets stored based on it's ID.
– Lapin
2 days ago




Correct, this only stores information on the possible types of components and systems. Each component has it's own unordered_map where it gets stored based on it's ID.
– Lapin
2 days ago












By the way, no need to apologize. If more first timers put in the effort you've put into phrasing your question, SO would be a better place
– StoryTeller
2 days ago




By the way, no need to apologize. If more first timers put in the effort you've put into phrasing your question, SO would be a better place
– StoryTeller
2 days ago












The unforgettable-factory may be what you want.
– user2709407
2 days ago




The unforgettable-factory may be what you want.
– user2709407
2 days ago




1




1




It is just fine, the 4 or 8 bytes it costs is nothing to worry about. So cheap that you'd consider doing this for each individual component. But it belongs in a .cpp file, not a .h file. Focus a bit on how you package and discover the component. A shared library (aka dll) and either a config list of plugin names or scanning a directory are obvious approaches.
– Hans Passant
2 days ago




It is just fine, the 4 or 8 bytes it costs is nothing to worry about. So cheap that you'd consider doing this for each individual component. But it belongs in a .cpp file, not a .h file. Focus a bit on how you package and discover the component. A shared library (aka dll) and either a config list of plugin names or scanning a directory are obvious approaches.
– Hans Passant
2 days ago












1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted











However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.




You are correct with your assessment of the solution's aesthetic qualities. Unfortunately C++ doesn't have a better mechanism to accomplish what you are after.



After all, even the C++ standard library has to employ this technique when it wants to instrument code for execution after header inclusion.



Though, since you did mark this C++17, and you intended to put the object declaration in a header, you need to make it an inline variable:



inline Register whatevs(...);


It should produce one object per-header file.






share|improve this answer























  • Thank you! I did not know about the inline keyword usage in C++17, good to know.
    – Lapin
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Lapin is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203937%2fhow-to-call-a-function-on-header-inclusion%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted











However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.




You are correct with your assessment of the solution's aesthetic qualities. Unfortunately C++ doesn't have a better mechanism to accomplish what you are after.



After all, even the C++ standard library has to employ this technique when it wants to instrument code for execution after header inclusion.



Though, since you did mark this C++17, and you intended to put the object declaration in a header, you need to make it an inline variable:



inline Register whatevs(...);


It should produce one object per-header file.






share|improve this answer























  • Thank you! I did not know about the inline keyword usage in C++17, good to know.
    – Lapin
    2 days ago















up vote
4
down vote



accepted











However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.




You are correct with your assessment of the solution's aesthetic qualities. Unfortunately C++ doesn't have a better mechanism to accomplish what you are after.



After all, even the C++ standard library has to employ this technique when it wants to instrument code for execution after header inclusion.



Though, since you did mark this C++17, and you intended to put the object declaration in a header, you need to make it an inline variable:



inline Register whatevs(...);


It should produce one object per-header file.






share|improve this answer























  • Thank you! I did not know about the inline keyword usage in C++17, good to know.
    – Lapin
    2 days ago













up vote
4
down vote



accepted







up vote
4
down vote



accepted







However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.




You are correct with your assessment of the solution's aesthetic qualities. Unfortunately C++ doesn't have a better mechanism to accomplish what you are after.



After all, even the C++ standard library has to employ this technique when it wants to instrument code for execution after header inclusion.



Though, since you did mark this C++17, and you intended to put the object declaration in a header, you need to make it an inline variable:



inline Register whatevs(...);


It should produce one object per-header file.






share|improve this answer















However, it seems unnecesarily messy to create a temporary object that never gets used just to execute code in it's constructor.




You are correct with your assessment of the solution's aesthetic qualities. Unfortunately C++ doesn't have a better mechanism to accomplish what you are after.



After all, even the C++ standard library has to employ this technique when it wants to instrument code for execution after header inclusion.



Though, since you did mark this C++17, and you intended to put the object declaration in a header, you need to make it an inline variable:



inline Register whatevs(...);


It should produce one object per-header file.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









StoryTeller

87.9k12174243




87.9k12174243












  • Thank you! I did not know about the inline keyword usage in C++17, good to know.
    – Lapin
    2 days ago


















  • Thank you! I did not know about the inline keyword usage in C++17, good to know.
    – Lapin
    2 days ago
















Thank you! I did not know about the inline keyword usage in C++17, good to know.
– Lapin
2 days ago




Thank you! I did not know about the inline keyword usage in C++17, good to know.
– Lapin
2 days ago










Lapin is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Lapin is a new contributor. Be nice, and check out our Code of Conduct.













Lapin is a new contributor. Be nice, and check out our Code of Conduct.












Lapin is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203937%2fhow-to-call-a-function-on-header-inclusion%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff