0

I have a code that uses Field built in function in java and i could not find a way to replace it in c++ the code is shown below,

import java.lang.reflect.Field; 
 
public class ParameterValue { 
    public String objectPath; 
    public Object objectReference; 
    public String fieldPath; 
    public String fieldPathNoCase; 
    public Field field; 
 public double value; 
  
 public ParameterValue(String path, ObjectTree tree, Field fieldInfo) { 
  objectPath = path; 
  objectReference = tree.getObject(path); 
  field = fieldInfo; 
  fieldPath = objectPath + "." + field.getName(); 
  fieldPathNoCase = fieldPath.toLowerCase();  
  read(); 
 } 
 
 public int getPrecision() { 
if (field.getType().getName() == "float" || field.getType().getName() == "double") 
   return 2; 
  else 
   return 0; 
 } 
  
 public double getPrecisionMultiplier() { 
  return Math.pow(10, getPrecision()); 
 } 
  
 public void read() { 
  String type = field.getType().getName(); 
  try { 
   if (type.equals("double")) 
    value = field.getDouble(objectReference); 
   else if (type.equals("float")) 
    value = field.getFloat(objectReference); 
   else if (type.equals("int")) 
    value = field.getInt(objectReference); 
   else if (type.equals("byte")) 
    value = field.getByte(objectReference); 
   else 
    throw new RuntimeException(); 
  } catch (IllegalAccessException e) { 
   throw new RuntimeException(e); 
  } 
  value = Math.round(value * getPrecisionMultiplier()) / getPrecisionMultiplier(); 
 } 
  
 public void write() { 
  String type = field.getType().getName(); 
  try { 
   if (type.equals("double")) 
    field.setDouble(objectReference, value); 
   else if (type.equals("float")) 
    field.setFloat(objectReference, (float)value); 
   else if (type.equals("int")) 
    field.setInt(objectReference, (int)Math.round(value)); 
   else if (type.equals("byte")) 
    field.setByte(objectReference, (byte)Math.round(value)); 
   else 
    throw new RuntimeException(); 
  } catch (IllegalAccessException e) { 
   throw new RuntimeException(e); 
  } 
 } 
  
 public void rebind(ObjectTree tree) { 
  objectReference = tree.getObject(objectPath); 
 } 
}

What i have understood from the code is that i need to find a class that can convert the value in it to Double, Float,etc. I have looked for something that can do this but i was not able to do so. reference of the code: https://www.programcreek.com/java-api-examples/index.php?source_dir=SecugenPlugin-master/src/sourceafis/simple/Fingerprint.java#

A M A
  • 45
  • 7

1 Answers1

1

As per my knowledge there is no equivalent class in C++. Now for your requirement first you list out what are all a java.lang.reflect.Field class provides in java. Once you listed all the utility methods, just sort list all methods that you really requires in your C++ application. Once done you do create a C++ class with the same name and methods types and implement the logic by yourself if possible.

Abhijit Pritam Dutta
  • 5,010
  • 2
  • 6
  • 16
  • You can't just implement reflections by yourself. That's a language feature, not something you implement. You need totally different logic to achieve similar goal in C++. – Jaa-c Apr 17 '18 at 12:41
  • I was able to create a Field class that serves my SDK and it runs fine. Thank you for the idea. – A M A Apr 17 '18 at 13:22