Fix style issues with C++ reflection.
Change-Id: I29c5e25864afd6f33a43762be6d66260c509084b
diff --git a/slang_rs_reflection_cpp.cpp b/slang_rs_reflection_cpp.cpp
index a5a1446..fe1aa46 100644
--- a/slang_rs_reflection_cpp.cpp
+++ b/slang_rs_reflection_cpp.cpp
@@ -41,37 +41,35 @@
namespace slang {
-RSReflectionCpp::RSReflectionCpp(const RSContext *con) :
- RSReflectionBase(con) {
-
+RSReflectionCpp::RSReflectionCpp(const RSContext *con)
+ : RSReflectionBase(con) {
}
RSReflectionCpp::~RSReflectionCpp() {
-
}
bool RSReflectionCpp::reflect(const string &OutputPathBase,
const string &InputFileName,
const string &OutputBCFileName) {
- mInputFileName = InputFileName;
- mOutputPath = OutputPathBase;
- mOutputBCFileName = OutputBCFileName;
- mClassName = string("ScriptC_") + stripRS(InputFileName);
+ mInputFileName = InputFileName;
+ mOutputPath = OutputPathBase;
+ mOutputBCFileName = OutputBCFileName;
+ mClassName = string("ScriptC_") + stripRS(InputFileName);
- makeHeader("android::renderscriptCpp::ScriptC");
- std::vector< std::string > header(mText);
- mText.clear();
+ makeHeader("android::renderscriptCpp::ScriptC");
+ std::vector< std::string > header(mText);
+ mText.clear();
- makeImpl("android::renderscriptCpp::ScriptC");
- std::vector< std::string > cpp(mText);
- mText.clear();
+ makeImpl("android::renderscriptCpp::ScriptC");
+ std::vector< std::string > cpp(mText);
+ mText.clear();
- writeFile(mClassName + ".h", header);
- writeFile(mClassName + ".cpp", cpp);
+ writeFile(mClassName + ".h", header);
+ writeFile(mClassName + ".cpp", cpp);
- return true;
+ return true;
}
typedef std::vector<std::pair<std::string, std::string> > ArgTy;
@@ -82,283 +80,280 @@
bool RSReflectionCpp::makeHeader(const std::string &baseClass) {
- startFile(mClassName + ".h");
+ startFile(mClassName + ".h");
- write("");
- write("#include \"RenderScript.h\"");
- write("using namespace android::renderscriptCpp;");
- write("");
+ write("");
+ write("#include \"RenderScript.h\"");
+ write("using namespace android::renderscriptCpp;");
+ write("");
- // Imports
- //for(unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++)
- //out() << "import " << Import[i] << ";" << std::endl;
- //out() << std::endl;
+ // Imports
+ //for(unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++)
+ //out() << "import " << Import[i] << ";" << std::endl;
+ //out() << std::endl;
- if(!baseClass.empty()) {
- write("class " + mClassName + " : public " + baseClass + " {");
+ if (!baseClass.empty()) {
+ write("class " + mClassName + " : public " + baseClass + " {");
+ } else {
+ write("class " + mClassName + " {");
+ }
+
+ write("private:");
+ uint32_t slot = 0;
+ incIndent();
+ for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(),
+ E = mRSContext->export_vars_end(); I != E; I++, slot++) {
+ const RSExportVar *ev = *I;
+ RSReflectionTypeData rtd;
+ ev->getType()->convertToRTD(&rtd);
+ if (!ev->isConst()) {
+ write(string(rtd.type->c_name) + " __" + ev->getName() + ";");
+ }
+ }
+ decIndent();
+
+ write("public:");
+ incIndent();
+ write(mClassName + "(android::sp<android::renderscriptCpp::RS> rs," +
+ " const char *cacheDir, size_t cacheDirLength);");
+ write("virtual ~" + mClassName + "();");
+ write("");
+
+
+ // Reflect export variable
+ slot = 0;
+ for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(),
+ E = mRSContext->export_vars_end(); I != E; I++, slot++) {
+ const RSExportVar *ev = *I;
+ RSReflectionTypeData rtd;
+ ev->getType()->convertToRTD(&rtd);
+
+ if (!ev->isConst()) {
+ write(string("void set_") + ev->getName() + "(" + rtd.type->c_name +
+ " v) {");
+ stringstream tmp;
+ tmp << slot;
+ write(string(" setVar(") + tmp.str() + ", &v, sizeof(v));");
+ write(string(" __") + ev->getName() + " = v;");
+ write("}");
+ }
+ write(string(rtd.type->c_name) + " get_" + ev->getName() + "() const {");
+ if (ev->isConst()) {
+ const clang::APValue &val = ev->getInit();
+ bool isBool = !strcmp(rtd.type->c_name, "bool");
+ write(string(" return ") + genInitValue(val, isBool) + ";");
} else {
- write("class " + mClassName + " {");
+ write(string(" return __") + ev->getName() + ";");
}
-
- write("private:");
- uint32_t slot = 0;
- incIndent();
- for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(),
- E = mRSContext->export_vars_end(); I != E; I++, slot++) {
-
- const RSExportVar *ev = *I;
- RSReflectionTypeData rtd;
- ev->getType()->convertToRTD(&rtd);
- if(!ev->isConst()) {
- write(string(rtd.type->c_name) + " __" + ev->getName() + ";");
- }
- }
- decIndent();
-
- write("public:");
- incIndent();
- write(mClassName + "(android::sp<android::renderscriptCpp::RS> rs," +
- " const char *cacheDir, size_t cacheDirLength);");
- write("virtual ~" + mClassName + "();");
+ write("}");
write("");
+ }
+ // Reflect export for each functions
+ for (RSContext::const_export_foreach_iterator
+ I = mRSContext->export_foreach_begin(),
+ E = mRSContext->export_foreach_end(); I != E; I++) {
+ const RSExportForEach *ef = *I;
+ if (ef->isDummyRoot()) {
+ write("// No forEach_root(...)");
+ continue;
+ }
- // Reflect export variable
- slot = 0;
- for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(),
- E = mRSContext->export_vars_end(); I != E; I++, slot++) {
+ stringstream tmp;
+ tmp << "void forEach_" << ef->getName() << "(";
+ if (ef->hasIn() && (ef->hasOut() || ef->hasReturn())) {
+ tmp << "android::sp<const android::renderscriptCpp::Allocation> ain";
+ tmp << ", android::sp<const android::renderscriptCpp::Allocation> aout";
+ } else if (ef->hasIn()) {
+ tmp << "android::sp<const android::renderscriptCpp::Allocation> ain";
+ } else {
+ tmp << "android::sp<const android::renderscriptCpp::Allocation> aout";
+ }
- const RSExportVar *ev = *I;
+ if (ef->getParamPacketType()) {
+ for (RSExportForEach::const_param_iterator i = ef->params_begin(),
+ e = ef->params_end(); i != e; i++) {
RSReflectionTypeData rtd;
- ev->getType()->convertToRTD(&rtd);
-
- if(!ev->isConst()) {
- write(string("void set_") + ev->getName() + "(" + rtd.type->c_name + " v) {");
- stringstream tmp;
- tmp << slot;
- write(string(" setVar(") + tmp.str() + ", &v, sizeof(v));");
- write(string(" __") + ev->getName() + " = v;");
- write("}");
- }
- write(string(rtd.type->c_name) + " get_" + ev->getName() + "() const {");
- if(ev->isConst()) {
- const clang::APValue &val = ev->getInit();
- bool isBool = !strcmp(rtd.type->c_name, "bool");
- write(string(" return ") + genInitValue(val, isBool) + ";");
- } else {
- write(string(" return __") + ev->getName() + ";");
- }
- write("}");
- write("");
+ (*i)->getType()->convertToRTD(&rtd);
+ tmp << rtd.type->c_name << " " << (*i)->getName();
+ }
}
-
- // Reflect export for each functions
- for (RSContext::const_export_foreach_iterator I = mRSContext->export_foreach_begin(),
- E = mRSContext->export_foreach_end(); I != E; I++) {
-
- const RSExportForEach *ef = *I;
- if (ef->isDummyRoot()) {
- write("// No forEach_root(...)");
- continue;
- }
-
- stringstream tmp;
- tmp << "void forEach_" << ef->getName() << "(";
- if(ef->hasIn() && (ef->hasOut() || ef->hasReturn())) {
- tmp << "android::sp<const android::renderscriptCpp::Allocation> ain";
- tmp << ", android::sp<const android::renderscriptCpp::Allocation> aout";
- } else if(ef->hasIn()) {
- tmp << "android::sp<const android::renderscriptCpp::Allocation> ain";
- } else {
- tmp << "android::sp<const android::renderscriptCpp::Allocation> aout";
- }
-
- if(ef->getParamPacketType()) {
- for(RSExportForEach::const_param_iterator i = ef->params_begin(),
- e = ef->params_end(); i != e; i++) {
-
- RSReflectionTypeData rtd;
- (*i)->getType()->convertToRTD(&rtd);
- tmp << rtd.type->c_name << " " << (*i)->getName();
- }
- }
- tmp << ") const;";
- write(tmp);
- }
+ tmp << ") const;";
+ write(tmp);
+ }
- // Reflect export function
- for (RSContext::const_export_func_iterator
- I = mRSContext->export_funcs_begin(),
- E = mRSContext->export_funcs_end(); I != E; I++) {
- const RSExportFunc *ef = *I;
+ // Reflect export function
+ for (RSContext::const_export_func_iterator
+ I = mRSContext->export_funcs_begin(),
+ E = mRSContext->export_funcs_end(); I != E; I++) {
+ const RSExportFunc *ef = *I;
- stringstream ss;
- makeFunctionSignature(ss, false, ef);
- write(ss);
- }
+ stringstream ss;
+ makeFunctionSignature(ss, false, ef);
+ write(ss);
+ }
- decIndent();
- write("};");
- return true;
+ decIndent();
+ write("};");
+ return true;
}
bool RSReflectionCpp::writeBC() {
- FILE *pfin = fopen(mOutputBCFileName.c_str(), "rb");
- if (pfin == NULL) {
- fprintf(stderr, "Error: could not read file %s\n", mOutputBCFileName.c_str());
- return false;
- }
+ FILE *pfin = fopen(mOutputBCFileName.c_str(), "rb");
+ if (pfin == NULL) {
+ fprintf(stderr, "Error: could not read file %s\n",
+ mOutputBCFileName.c_str());
+ return false;
+ }
- unsigned char buf[16];
- int read_length;
- write("static const unsigned char __txt[] = {");
- incIndent();
- while ((read_length = fread(buf, 1, sizeof(buf), pfin)) > 0) {
- string s;
- for(int i = 0; i < read_length; i++) {
- char buf2[16];
- sprintf(buf2, "0x%02x,", buf[i]);
- s += buf2;
- }
- write(s);
+ unsigned char buf[16];
+ int read_length;
+ write("static const unsigned char __txt[] = {");
+ incIndent();
+ while ((read_length = fread(buf, 1, sizeof(buf), pfin)) > 0) {
+ string s;
+ for (int i = 0; i < read_length; i++) {
+ char buf2[16];
+ snprintf(buf2, sizeof(buf2), "0x%02x,", buf[i]);
+ s += buf2;
}
- decIndent();
- write("};");
- write("");
- return true;
+ write(s);
+ }
+ decIndent();
+ write("};");
+ write("");
+ return true;
}
bool RSReflectionCpp::makeImpl(const std::string &baseClass) {
- startFile(mClassName + ".h");
+ startFile(mClassName + ".h");
+ write("");
+ write("#include \"" + mClassName + ".h\"");
+ write("");
+
+ writeBC();
+
+ // Imports
+ //for(unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++)
+ //out() << "import " << Import[i] << ";" << std::endl;
+ //out() << std::endl;
+
+ write("\n");
+ stringstream ss;
+ ss << mClassName << "::" << mClassName
+ << "(android::sp<android::renderscriptCpp::RS> rs, "
+ "const char *cacheDir, size_t cacheDirLength) :\n"
+ << " ScriptC(rs, __txt, sizeof(__txt), \""
+ << mClassName << "\", " << mClassName.length()
+ << ", cacheDir, cacheDirLength) {";
+ write(ss);
+ incIndent();
+ //...
+ decIndent();
+ write("}");
+ write("");
+
+ write(mClassName + "::~" + mClassName + "() {");
+ write("}");
+ write("");
+
+ // Reflect export for each functions
+ uint32_t slot = 0;
+ for (RSContext::const_export_foreach_iterator
+ I = mRSContext->export_foreach_begin(),
+ E = mRSContext->export_foreach_end(); I != E; I++, slot++) {
+ const RSExportForEach *ef = *I;
+ if (ef->isDummyRoot()) {
+ write("// No forEach_root(...)");
+ continue;
+ }
+
+ stringstream tmp;
+ tmp << "void " << mClassName << "::forEach_" << ef->getName() << "(";
+ if (ef->hasIn() && (ef->hasOut() || ef->hasReturn())) {
+ tmp << "android::sp<const android::renderscriptCpp::Allocation> ain";
+ tmp << ", android::sp<const android::renderscriptCpp::Allocation> aout";
+ } else if (ef->hasIn()) {
+ tmp << "android::sp<const android::renderscriptCpp::Allocation> ain";
+ } else {
+ tmp << "android::sp<const android::renderscriptCpp::Allocation> aout";
+ }
+ tmp << ") const {";
+ write(tmp);
+ tmp.str("");
+
+ tmp << " forEach(" << slot << ", ";
+ if (ef->hasIn() && (ef->hasOut() || ef->hasReturn())) {
+ tmp << "ain, aout, NULL, 0);";
+ } else if (ef->hasIn()) {
+ tmp << "ain, NULL, 0);";
+ } else {
+ tmp << "aout, NULL, 0);";
+ }
+ write(tmp);
+
+ write("}");
write("");
- write("#include \"" + mClassName + ".h\"");
- write("");
+ }
- writeBC();
+ slot = 0;
+ // Reflect export function
+ for (RSContext::const_export_func_iterator
+ I = mRSContext->export_funcs_begin(),
+ E = mRSContext->export_funcs_end(); I != E; I++) {
+ const RSExportFunc *ef = *I;
- // Imports
- //for(unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++)
- //out() << "import " << Import[i] << ";" << std::endl;
- //out() << std::endl;
-
- write("\n");
stringstream ss;
- ss << mClassName << "::" << mClassName
- << "(android::sp<android::renderscriptCpp::RS> rs, "
- "const char *cacheDir, size_t cacheDirLength) :\n"
- << " ScriptC(rs, __txt, sizeof(__txt), \""
- << mClassName << "\", " << mClassName.length()
- << ", cacheDir, cacheDirLength) {";
+ makeFunctionSignature(ss, true, ef);
write(ss);
- incIndent();
- //...
- decIndent();
+ ss.str("");
+
+ ss << " invoke(" << slot << ", NULL, 0);";
+ write(ss);
+
write("}");
write("");
- write(mClassName + "::~" + mClassName + "() {");
- write("}");
- write("");
+ slot++;
+ }
- // Reflect export for each functions
- uint32_t slot = 0;
- for (RSContext::const_export_foreach_iterator I = mRSContext->export_foreach_begin(),
- E = mRSContext->export_foreach_end(); I != E; I++, slot++) {
-
- const RSExportForEach *ef = *I;
- if (ef->isDummyRoot()) {
- write("// No forEach_root(...)");
- continue;
- }
-
- stringstream tmp;
- tmp << "void " << mClassName << "::forEach_" << ef->getName() << "(";
- if(ef->hasIn() && (ef->hasOut() || ef->hasReturn())) {
- tmp << "android::sp<const android::renderscriptCpp::Allocation> ain";
- tmp << ", android::sp<const android::renderscriptCpp::Allocation> aout";
- } else if(ef->hasIn()) {
- tmp << "android::sp<const android::renderscriptCpp::Allocation> ain";
- } else {
- tmp << "android::sp<const android::renderscriptCpp::Allocation> aout";
- }
- tmp << ") const {";
- write(tmp);
- tmp.str("");
-
- tmp << " forEach(" << slot << ", ";
- if(ef->hasIn() && (ef->hasOut() || ef->hasReturn())) {
- tmp << "ain, aout, NULL, 0);";
- } else if(ef->hasIn()) {
- tmp << "ain, NULL, 0);";
- } else {
- tmp << "aout, NULL, 0);";
- }
- write(tmp);
-
- write("}");
- write("");
- }
-
- slot = 0;
- // Reflect export function
- for (RSContext::const_export_func_iterator I = mRSContext->export_funcs_begin(),
- E = mRSContext->export_funcs_end(); I != E; I++) {
-
- const RSExportFunc *ef = *I;
-
- stringstream ss;
- makeFunctionSignature(ss, true, ef);
- write(ss);
- ss.str("");
-
- ss << " invoke(" << slot << ", NULL, 0);";
- write(ss);
-
- write("}");
- write("");
-
- slot++;
- }
-
- decIndent();
- return true;
+ decIndent();
+ return true;
}
void RSReflectionCpp::makeFunctionSignature(
- std::stringstream &ss,
- bool isDefinition,
- const RSExportFunc *ef) {
- ss << "void ";
- if (isDefinition) {
- ss << mClassName << "::";
- }
- ss << "invoke_" << ef->getName() << "(";
+ std::stringstream &ss,
+ bool isDefinition,
+ const RSExportFunc *ef) {
+ ss << "void ";
+ if (isDefinition) {
+ ss << mClassName << "::";
+ }
+ ss << "invoke_" << ef->getName() << "(";
- if(ef->getParamPacketType()) {
- bool FirstArg = true;
- for(RSExportFunc::const_param_iterator i = ef->params_begin(),
- e = ef->params_end(); i != e; i++) {
-
- RSReflectionTypeData rtd;
- (*i)->getType()->convertToRTD(&rtd);
- if (!FirstArg) {
- ss << ", ";
- } else {
- FirstArg = false;
- }
- ss << rtd.type->c_name << " " << (*i)->getName();
- }
+ if (ef->getParamPacketType()) {
+ bool FirstArg = true;
+ for (RSExportFunc::const_param_iterator i = ef->params_begin(),
+ e = ef->params_end(); i != e; i++) {
+ RSReflectionTypeData rtd;
+ (*i)->getType()->convertToRTD(&rtd);
+ if (!FirstArg) {
+ ss << ", ";
+ } else {
+ FirstArg = false;
+ }
+ ss << rtd.type->c_name << " " << (*i)->getName();
}
+ }
- if (isDefinition) {
- ss << ") {";
- } else {
- ss << ");";
- }
+ if (isDefinition) {
+ ss << ") {";
+ } else {
+ ss << ");";
+ }
}
-
-}
+} // namespace slang
diff --git a/slang_rs_reflection_cpp.h b/slang_rs_reflection_cpp.h
index df1dda5..226bc5b 100644
--- a/slang_rs_reflection_cpp.h
+++ b/slang_rs_reflection_cpp.h
@@ -22,27 +22,23 @@
namespace slang {
class RSReflectionCpp : public RSReflectionBase {
-protected:
+ public:
+ explicit RSReflectionCpp(const RSContext *);
+ virtual ~RSReflectionCpp();
+
+ bool reflect(const std::string &OutputPathBase,
+ const std::string &InputFileName,
+ const std::string &OutputBCFileName);
-public:
- RSReflectionCpp(const RSContext *);
- virtual ~RSReflectionCpp();
+ private:
+ bool makeHeader(const std::string &baseClass);
+ bool makeImpl(const std::string &baseClass);
+ void makeFunctionSignature(std::stringstream &ss, bool isDefinition,
+ const RSExportFunc *ef);
+ bool writeBC();
- bool reflect(const std::string &OutputPathBase,
- const std::string &InputFileName,
- const std::string &OutputBCFileName);
-
-
-private:
- bool makeHeader(const std::string &baseClass);
- bool makeImpl(const std::string &baseClass);
- void makeFunctionSignature(std::stringstream &ss, bool isDefinition,
- const RSExportFunc *ef);
- bool writeBC();
-
- bool startScriptHeader();
-
+ bool startScriptHeader();
}; // class RSReflection
} // namespace slang