Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 253601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:46:32+00:00 2026-05-11T21:46:32+00:00

In my epic quest of making C++ do things it shouldn’t, I am trying

  • 0

In my epic quest of making C++ do things it shouldn’t, I am trying to put together a compile time generated class.

Based on a preprocessor definition, such as (rough concept)

CLASS_BEGIN(Name)  
    RECORD(xyz)  
    RECORD(abc)

    RECORD_GROUP(GroupName)  
        RECORD_GROUP_RECORD(foo)  
        RECORD_GROUP_RECORD(bar)  
    END_RECORDGROUP   
END_CLASS

While I am fairly sure I generate a class that reads the data from the file system using this sort of structure (Maybe even doing it using Template Metaprogramming), I don’t see how I can generate both the functions to access the data and the function to read the data.

I would want to end up with a class something like this

class Name{
    public:
    xyz_type getxyz();
    void setxyz(xyz_type v);

    //etc

    list<group_type> getGroupName();

    //etc

    void readData(filesystem){
         //read xyz
         //read abc
         //etc
    }
};

Does anyone have any idea if this is even possible?

–EDIT–

To clarify the intended usage for this. I have files in a standard format I want to read. The format is defined already, so it is not open to change. Each file can contain any number records, each of which can contain any number sub records.

The numerous record types each contain a diffrent set of sub records, but they can be are defined. So for example the Heightmap record must contain a Heightmap, but can optional contain normals.

So I would want to define a Record for that like so:

CLASS_BEGIN(Heightmap)  
    RECORD(VHDT, Heightmap, std::string) //Subrecord Name, Readable Name, Type  
    RECORD_OPTIONAL(VNML, Normals, std::string)  
END_CLASS  

For which I would want to output something with the functionality of a class like this:

class Heightmap{
    public:
    std::string getHeightmap(){
        return mHeightmap->get<std::string>();
    }
    void setHeightmap(std::string v){
        mHeight->set<std::string>(v);
    }

    bool hasNormal(){
        return mNormal != 0;
    }
    //getter and setter functions for normals go here

    private:
    void read(Record* r){
        mHeightmap = r->getFirst(VHDT);
        mNormal = r->getFirst(VNML);
    }


    SubRecord* mHeightmap, mNormal;
}

The issue I am having is that I need every preprocessor definition twice. Once for defining the function definition within the class, and once for creating the read function. As the preprocessor is purely functional, I cannot push the data to a queue and generate the class on the END_CLASS marco definition.

I cannot see a way around this issue, but wondered if anyone who has a greater understanding of C++ did.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:46:32+00:00Added an answer on May 11, 2026 at 9:46 pm

    You might be able to solve this problem using boost tuples. It will result in a design which is different to what you are thinking of now, but it should allow you to solve the problem in a generic way.

    The following example defines a record of the form “std::string,bool” and then reads that data in from a stream.

    #include "boost/tuple/tuple.hpp"
    #include <iostream>
    #include <sstream>
    
    using namespace ::boost::tuples;
    

    The functions are used to read the data from an istream. The first overload stops the iteration through the tuple after we reach the last record type:

    //
    // This is needed to stop when we have no more fields
    void read_tuple (std::istream & is, boost::tuples::null_type )
    {
    }
    
    template <typename TupleType>
    void read_tuple (std::istream & is, TupleType & tuple)
    {
      is >> tuple.template get_head ();
      read_tuple (is, tuple.template get_tail ());
    }
    

    The following class implements the getter member for our Record. Using the RecordKind as our key we get the specific member that we’re interested in.

    template <typename TupleType>
    class Record
    {
    private:
      TupleType m_tuple;
    
    public:
      //
      // For a given member - get the value
      template <unsigned int MBR>
      typename element <MBR, TupleType>::type & getMember ()
      {
        return m_tuple.template get<MBR> ();
      }
    
      friend std::istream & operator>> (std::istream & is
                                      , Record<TupleType> & record)
      {
        read_tuple (is, record.m_tuple);
      }
    };
    

    The next type is the meta description for our record. The enumeration gives us a symbolic name that we can use to access the members, ie. the field names. The tuple then defines the types of those fields:

    struct HeightMap
    {
      enum RecordKind
      {
        VHDT
        , VNML
      };
    
      typedef boost::tuple < std::string
                           , bool
                         > TupleType;
    };
    

    Finally, we construct a record and read in some data from a stream:

    int main ()
    {
      Record<HeightMap::TupleType> heightMap;
      std::istringstream iss ( "Hello 1" );
    
      iss >> heightMap;
    
      std::string s = heightMap.getMember < HeightMap::VHDT > ();
      std::cout << "Value of s: " << s << std::endl;
    
    
      bool b = heightMap.getMember < HeightMap::VNML > ();
      std::cout << "Value of b: " << b << std::endl;
    }    
    

    And as this is all template code, you should be able to have records nested in records.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: This question was an epic failure, but here's the working solution. It's based
I'm trying to install EPIC using the Pulse Explorer for Eclipse (as I'm rather
For a long time I've been trying different languages to find the feature-set I
In the epic quest of me getting my Create and Edit methods working for
I'm having an epic amount of difficulty trying to get a result from a
Several users in this epic question put the following in the .vimrc : Necesary
On a recent Java project, we needed a free Java based real-time data plotting
I have Epic Editor which returns a handle to the window (see Java code
I have Eclipse SDK Version: 3.5.2 with EPIC 0.5.46 installed on Ubuntu Linux .
I am using Eclipse GALILEO running on Linux Ubuntu with EPIC plugin to Run/Debug

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.