Sign in to follow this  
dmatter

Unity variable no. of template argument - possible solution

Recommended Posts

After reading over my recent thread about virtual templates a passage (at the bottom) by MaulingMonkey got me thinking and after a little tinkering I found a way to have a variable number of template arguments. Its not exactly what I would call flexible and may not be useful for all circumstaces but it does work (I only wanted to see if I could achieve the task.) The technique requires (well not 'requires' but works better with) two macros and two simple meta-programs:
//USED TO INDICATE THE END OF THE ARGUMENT LIST
struct end_args;

//MACRO TO BEGIN THE VARARG LIST
#define template_varags(name) template <int i = 1>struct name{    typedef end_args next;};

//MACRO TO ADD A PARAMETER
#define param(to, num, t, val) template <>struct to<num>{    typedef to<num+1> next;    typedef t type;    static type value() { return val; }};

//META-PROGRAM TO FIND THE NUMBER OF PARAMETERS
template <class T, int i = 0>
struct num_args
{
    enum { result = num_args<typename T::next, i + 1>::result };
};

template <int i>
struct num_args<end_args, i> //SPECIALIZATION TO END RECURSION
{
    enum { result = i-1 };
};

//META-PROGRAM TO RETURN THE VALUE OF AN ARGUMENT
template<class T, int i>
struct get_arg
{
    typedef typename get_arg<typename T::next, i-1>::type type;
    static type get() { return get_arg<typename T::next, i-1>::get(); }
};

template <class T>
struct get_arg<T, 0> //SPECIALIZATION TO END RECURSION
{
    typedef typename T::type type;
    static type get() { return T::value(); }
};
Ok, now with that you can create templates with variable number of arguments. A program to output all the variable values to the console might look like this:
template <class T, int i = num_args<T>::result - 1>
struct print
{
    static void invoke()
    {
        cout << get_arg<T, (num_args<T>::result - 1) - i>::get() << endl;
        print<T, i-1>::invoke();
    }
};

template <class T>
struct print<T, 0>
{
    static void invoke()
    {
        cout << get_arg<T, num_args<T>::result - 1>::get() << "\n\n";
    }
};

//CREATE THE VARARG LIST (could go in a namespace to reduce name pollution)
template_varags(args)
    param(args, 0, int, 5)
    param(args, 1, float, 4.5)
    param(args, 2, char, 'z')

int main()
{
    cout << "num parameters: " << num_args<args<0> >::result << endl;
    cout << "\nall parameter values:\n";
    print <args<0> >::invoke();

    return 0;
}
I'm certain that someone else here can create a much more powerful way to acieve variable argument templates, but I'm curious for comments/pitfalls. Dave [Edited by - dmatter on August 29, 2005 3:03:44 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Rattrap
Try this Loki. Loki has a Template List that might be what you are looking for.


I recommend boost MPL, its much powerful than loki's type lists and provides complete analogues (and more) of C++ standard library containers (vector, list, set etc) & algorithms in land of compile-time. Boost MPL completely formalized template metaprogramming making it easy and fun to write metaprograms. Throw in a combo of Boost.Preprocessor and the sky's the limit [grin].

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Boost MPL completely formalized template metaprogramming making it easy and fun to write metaprograms.


Your Mileage May Vary. Void Where Prohibited. No Purchase Necessary. Do Not Taunt Happy Fun Ball. MPL Is An Equal Opportunity Employer.

Share this post


Link to post
Share on other sites
Rule one of MPL is you do not expect MPL to be fully supported by your compiler. Rule two of MPL is YOU DO NOT EXPECT MPL TO BE FULLY SUPPORTED BY YOUR COMPILER!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627774
    • Total Posts
      2979012
  • Similar Content

    • By Hacktion Architects
      Hey guys me and my team have made our first game! Would be awesome if you guys could download, play it and let us know what you think!

      3. 2. 1. GO! Fly through the local school grounds with three other drones vying for the fastest time. Racing past the football fields, weaving in between pillars and squeezing through tight corridors, in Drozone speed is everything.
      Race through checkpoints while keeping your eyes out for power ups that give you a big short burst of speed, but be careful not to lose control!
      And if you're having problems completing the course you will want to grab the shield power up to double your health.
      Pick your drone and start racing, will you be able to beat our fastest lap time?
      Trailer here:
      https://www.youtube.com/watch?v=tekETULy2Qk&feature=youtu.be
      Link to download:
      https://gamejolt.com/games/drozone/292176
      A game developed by a group of seven young indie devs, from Brisbane Australia, Drozone is focused on delivering the exhilaration of flying a drone in first person.
      The experience is built up around competition between players and seeing who can get the fastest time. Let us know what you’re fastest time is!
    • By Polycanic Studios

      Everybody's favorite hard hitting reality blood sport is back and is better than ever. Join your host Miss Midnight and enter the Graviators arena for a new and thrilling style of combat.
      Graviators is a 3rd person arena based brawler in which you can play online multiplayer and LAN multiplayer with up to 4 friends or test your skills in our single player mode. Choose your fighter, control your gravity and fight using each characters unique ranged and melee attacks. 

      Download now at: www.graviators.com or watch the Trailer
      Graviators is currently released but will be receiving updates to make improvements and fix any issues so please bear with us! If you want to know more about what we are up to, follow us here for regular devlogs or check out our Facebook or Twitter for updates. 
      If you want to get in contact with us, please feel free to comment here, polycanicstudios@gmail.com or send us a message on Facebook!
    • By INTwindwolf
      COMPANY AND THE PROJECT

      We are an indie game studio consisted of professional and friendly people. Additionally, we are a team of skilled artists and dedicated indie enthusiasts. Our current project is INT, developed on Unity Engine 5 for platforms Windows, Linux, and Mac.

      INT is a 3D Sci-fi RPG with a strong emphasis on story, role playing, and innovative RPG features such as randomized companions. The focus is on the journey through a war-torn world with fast-paced combat against hordes of enemies. The player must accomplish quests like a traditional RPG, complete objectives, and meet lively crew members who will aid in the player's survival. Throughout the game you can side and complete missions through criminal cartels, and the two major combatants, the UCE and ACP, of the Interstellar Civil War.
      Please note that all of our current positions are remote work. You will not be required to travel.
      For more information about us, follow the links listed below.
      INT Official website
      Steam Greenlight
      IndieDB page
      Also follow social media platforms for the latest news regarding our projects.
      Facebook
      Twitter
       
      TALENT NEEDED
      We are looking for an Animator to join the Art team to create and polish animations for the game. You will be collaborating with fellow members of the team, and follow instructions from the Project Lead and the Animation team Lead in crafting smooth, flowing animations.
      As an Animator for this project, your duties would include:
      Create rigs to be used for animations. Skin 3D models to rigs. Contribute to constructive team discussions. Attend regular team meetings.  
      REQUIREMENTS
      To be successful in this position, following requirements apply:
       
      Have working knowledge of 3D animation suites. Understand import/export requirements for Unity Engine integration. Excellent self-management skills. Excellent attention to detail. Excellent communication skills. Preferred requirement:
      Knowledge of the Unity Engine UMA character creation system would be an advantage.  
      REVENUE - SHARE
      This is the perfect opportunity to get into the game development industry. Being an Indie team we do not have the creative restrictions often imposed by publishers or other third parties. We are extremely conscientious of our work and continuously uphold a high level of quality throughout our project.
      We are unable to offer wages or per-item payments at this time. However revenue-sharing from crowd-funding is offered to team members who contribute 15-20 hours per week to company projects, as well as maintain constant communication and adhere to deadlines. Currently the crowd-funding campaign is scheduled for the year 2018. Your understanding is dearly appreciated.
       
      TO APPLY
      Please send your Cover Letter, CV, Portfolio (if applicable), and other relevant documents/information to this email: JohnHR@int-game.net
      Thank you for your time! We look forward to hearing from you!
      John Shen
      HR Lead
      Starboard Games LLC
    • By KARTHI
      Now I am making 2d game in my home. I am so confused with the resolution.
      Please anyone help me to choose the resolution
    • By KARTHI
      Free software
       
      1. Lumberyard (Game engine) open-source and free tool
      Amazon Lumberyard is a free cross-platform triple-A game engine developed by Amazon and based on the architecture of Cry Engine, which was licensed from Crytek in 2015.
       
      2. Sculptris (sculpture tool) open-source and free tool
      Sculptris is a virtual sculpting software program, with a primary focus on the concept of modeling clay. It entered active development in early December 2009, and the most recent release was in 2011.
       
      3. Make human (game model creator) open-source and free tool
      Make human is an open-source 3D computer graphics software middleware designed for the prototyping of photorealistic humanoids. It is developed by a community of programmers, artists, and academics interested in 3D modeling of characters.
       
      4. Ipi soft (motion capture software) not free tool
      iPi Motion Capture is a scalable markerless motion capture software tool that supports 1 or 2 Kinect cameras or 3 to 6 Sony PlayStation Eye cameras to track a human action and convert it into a motion capture file
       
       5. Blender (Complete tool) for modeling, texturing and so on (open-source and free tool)
       Blender is a professional, free and open-source 3D computer graphics software toolset used for creating animated films, visual effects, art, 3D printed models, interactive 3D applications and video games.
                 
      6. Audacity (music editor) open-source and free tool
      Audacity is a free open source digital audio editor and recording computer software application, available for Windows, OS X, Linux and other operating systems.
                 
      7. Awesome bump (bump map editor) open-source and free tool (optional)
      Awesome Bump is a free program written using Qt library designed to generate normal, height, specular or ambient occlusion textures from a single image.
       
      8. Faceware (facial animation) not free tool
      Faceware Technologies is an American company that designs facial animation and motion capture technology. The company was established under Image Metrics and became its own company at the beginning of 2012.
       
      9. GIMP (image editing) open-source and free tool
      GIMP is a free and open-source raster graphics editor used for image retouching and editing, free-form drawing, converting between different image formats, and more specialized tasks. Through this you can also create bump maps
       
      10. Meshlab (mesh repair) open-source and free tool (Optional)
      MeshLab is an advanced 3D mesh processing software system that is oriented to the management and processing of unstructured large meshes and provides a set of tools for editing, cleaning, healing.
       
      11. LibreOffice (create documents) open-source and free tool
      LibreOffice is a free and open source office suite, a project of The Document Foundation. It was forked from OpenOffice.org in 2010, which was an open-sourced version of the earlier StarOffice.
                 
      12. Atom (coding software) open-source and free tool
      Atom is a free and open-source text and source code editor for macOS, Linux, and Microsoft Windows with support for plug-ins written in Node.js, and embedded Git Control, developed by GitHub.
                 
       
      Useful websites
      free image
                 
      Reference images will be found on Pinterest
       
      Free Sounds
       
      1.      Freesound.org
      2.      99Sounds.org
      3.      NoiseForFun.com
      4.      Incompetech.com
      5.      OpenGameArt.org
      6.      RaisedBeaches.com
      7.      Musopen.org
      8.      PlayonLoop.com
      9.      Bensound.com
      10.   SoundJay.com
      11.   Dig.ccmixter.org
      12.   Soundgator.com
      13.   Pacdv.com
      14.   Freesfx.co.uk
      15.   Soundtrack.imphenzia.com
      16.   Bxfr.net
       
      Download the free music tracks from these websites
       
      1. http://incompetech.com/music/
      2. http://dig.ccmixter.org/
      3. http://www.joshwoodward.com
      4. http://www.youtube.com/audiolibrary
       
      I hope this information will help full to you. I am got so stress to collect this data so don't waste it 
      🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤗🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔
       
       
  • Popular Now