Click here to Skip to main content
15,889,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to fetch value of certain key from back-end.
In back-end the structures are defined and values are initialized. Consider this as structure defined in back-end:

C++
struct person{
    string nme;
    string adrs;
    int id;
};

C++
person p1 = {"steve","ABC street",23};

The key address corresponds to value of p1.adrs in back-end.
Now the key address is to be mapped to (p1 & adrs) in external file and should get the value "ABC street".
My question is how mapping is to be done for key and its particular structure member in external file and how to fetch value for that key.

What I have tried:

I have implemented that mapping using JSON.

C++
{
 "address":"p1.adrs",
 "name":"p1.nme",
  ........
}

Now p1.adrs will be a string and as CPP do not support Run time reflection, I cannot do this.
Posted
Updated 22-Feb-18 18:18pm
v2
Comments
Richard MacCutchan 20-Feb-18 12:31pm    
I am not sure I understand what the JSON text has to do with this. The obvious answer is for the backend to store these data in some form of database file. Any set can then be fetched by passing the relevant key.

Your struct may directly provide the mapping using, for instance, a std::map with std::string as key and a union (or std::variant, if you can use it) as value.


A simple example (you need a C++17 compiler).
[update]
C++
#include <iostream>
#include <unordered_map>
#include <variant>
using namespace std;


class person
{
  unordered_map<string, variant<string, int> > msv; // this is the storage for the variables
public:
  person(string nme, string adrs, int id)
  {
    msv["nme"] = nme;
    msv["adrs"] = adrs;
    msv["id"] = id;
  }
  variant <string, int> operator[](const string & key)
  {
    auto it = msv.find(key);
    if ( it != msv.end())
      return it->second;
    throw( out_of_range("key not found")); // an alternative could be returning an empty variant
  }
};


int main()
{
  person p("steve","ABC street",23);

  cout << get<string>(p["nme"]) << ", " << get<int>(p["id"]) << endl; // OK, accessing existing fields

  cout << "'" << get<string>(p["foo"]) << "'" << endl; // Error, accessing missing field
}

[/update]
 
Share this answer
 
v2
Comments
gvsk 20-Feb-18 22:05pm    
Can you please provide example code for small use case?
I have did this:

#include <iostream>
#include <string>
#include

using namespace std;

struct Chassis
{
string InLED;
};

int main()
{
Chassis chassis1={"on"};

string item="IndicatorLED";

map<string, Chassis> cha1;
cha1["IndicatorLED"] = chassis1;

if(cha1.find(item) == cha1.end()) {cout <<"not there";}

else {cout <<cha1[item].InLED ;}

return 0;
}

Here I can map that string variable("IndicatorLED") to structure but cannot map it to that member (InLED).
The requirement is to map string variable to particular member in the structure.
CPallini 21-Feb-18 3:14am    
Actually you could (just map a pointer to it) however it would be awkward.
I meant re-design your data structure to provide map accessed members.
gvsk 21-Feb-18 3:32am    
Sorry, I dint understand. Can you please provide some sample code
CPallini 21-Feb-18 3:36am    
The simplest approach is using directly map<string,string>, instead of struct person.
In such a way every 'variable' becomes a string. If it is not acceptable then you have to use map<string, Variable> where Variable is a union or, using C++17, a variant.
gvsk 21-Feb-18 3:52am    
I really appreciate your answers. But I am new to CPP. I want to implement that string variant mapping. Can you Please provide me with an example.
Thank you very much..
myfile.cpp

C++
#ifndef MYFILE_H
#define MYFILE_H
#include <iostream>
#include <algorithm>
#include<vector>
#include <string>
#include <map>

using namespace std;

struct Chassis
{
    string InLED;
    string AstTg;
};

struct Manager
{
    string MngrType;
    int count;
};

  const Chassis chassis1={"On","null"}; 
  const Manager manager1={"BMC",23};    

  const map<string, const string>cha1={{"IndicatorLED", chassis1.InLED},{"AssetTag",chassis1.AstTg},{"ManagerType",manager1.MngrType}};

   const map<string, int>cha2={{"Count",manager1.count}};

void func(string);

#endif


myfile.cpp
C++
#include <iostream>
#include <string>
#include "myfile.h"
#include <map>


using namespace std;

void func(string item)
{ 

 
    if(cha1.find(item) == cha1.end()) {if (cha2.find(item) == cha2.end()){} else {cout<< item<<":"<<cha2.at(item)<<endl;} }
    
    else {cout<< item<<":"<<cha1.at(item)<<endl;}

    
}


main.cpp

#include <iostream>
#include <string>
using namespace std;
#include "myfile.h"

int main() {
string item="IndicatorLED";
func (item);


string item1="AssetTag";
func(item1);

string item2="ManagerType";
func(item2);

string item3="Count";
func(item3);
}


This is requirement. But it will be more better if I declare mappings in separate file (here declared in .h file).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900