Files
pip/main.cpp

202 lines
5.6 KiB
C++

/*
PIP - Platform Independent Primitives
Test program
Copyright (C) 2014 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//#define PIP_DEBUG
/*#include "pip.h"
class ElementA: public PIObject {
PIOBJECT(ElementA)
// ...
};
ADD_NEW_TO_COLLECTION(ab_group, ElementA)
class ElementB: public PIObject {
PIOBJECT(ElementB)
// ...
};
ADD_NEW_TO_COLLECTION(ab_group, ElementB)
class ElementC: public PIObject {
PIOBJECT(ElementC)
// ...
};
ADD_NEW_TO_COLLECTION(c_group, ElementC)
class ElementD: public PIObject {
PIOBJECT(ElementD)
// ...
};
*/
#include "pip.h"
#include "pivariant.h"
/*
Test::PIVariant<> * pv;
template <typename T>
Test::PIVariant<T> newVariant(const T & v) {return Test::PIVariant<T>(v);}
template <typename T>
Test::PIVariant<T> * castVariant(Test::__PIVariantBase * v, const T & t) {return static_cast<Test::PIVariant<T> * >(v);}
*/
#include "picodeparser.h"
#define S(a, b) a#b
enum TypeFlag {NoFlag, Const = 0x01, Static = 0x02, Mutable = 0x04, Volatile = 0x08, Inline = 0x10, Virtual = 0x20};
typedef PIFlags<TypeFlag> TypeFlags;
struct TypeInfo {
TypeInfo() {flags = 0;}
PIString name;
PIString type;
TypeFlags flags;
};
struct FunctionInfo {
PIString name;
TypeInfo return_type;
PIVector<TypeInfo> arguments;
};
struct ClassInfo {
PIString name;
PIStringList parents;
PIVector<TypeInfo> variables;
PIVector<FunctionInfo> functions;
};
inline PICout operator <<(PICout s, const TypeInfo & v) {
if (v.flags[Inline]) s << "inline ";
if (v.flags[Virtual]) s << "virtual ";
if (v.flags[Static]) s << "static ";
if (v.flags[Const]) s << "const ";
if (v.flags[Mutable]) s << "mutable ";
if (v.flags[Volatile]) s << "volatile ";
s << v.type;
if (!v.name.isEmpty())
s << " " << v.name;
return s;
}
inline PICout operator <<(PICout s, const ClassInfo & v) {
s.setControl(0, true);
s << "class " << v.name;
if (!v.parents.isEmpty()) {
s << ": ";
bool first = true;
piForeachC (PIString & i, v.parents) {
if (first) first = false;
else s << ", ";
s << i;
}
s << " {\n";
piForeachC (FunctionInfo & i, v.functions) {
s << Tab << i.return_type << " " << i.name << "(";
bool fa = true;
piForeachC (TypeInfo & a, i.arguments) {
if (fa) fa = false;
else s << ", ";
s << a;
}
s << ");\n";
}
if (!v.functions.isEmpty() && !v.variables.isEmpty())
s << "\n";
piForeachC (TypeInfo & i, v.variables) {
s << Tab << i << ";\n";
}
s << "}\n";
}
s.restoreControl();
return s;
}
int main (int argc, char * argv[]) {
PIString s("this\t :is \n(SPARTA)!");
piCout << s.findCWord("this");
piCout << s.findCWord("is");
piCout << s.findCWord("SPARTA");
piCout << s.findCWord("SPARTA!");
if (argc < 2) return 0;
PICodeParser cd;
//cd.includeDirectory("../qpicalculator");
cd.parseFile(argv[1]);
piForeachC (PICodeParser::Enum & e, cd.enums)
piCout << e.name << e.members;
ClassInfo ci;
PICodeParser::Entity * e = cd.findEntityByName("PITimer");
if (!e) return 0;
ci.name = e->name;
piForeachC (PICodeParser::Entity * p, e->parents)
ci.parents << p->name;
piForeachC (PICodeParser::Member & m, e->members) {
TypeInfo ni;
ni.name = m.name;
ni.type = m.type;
if (m.attributes[PICodeParser::Const]) ni.flags |= Const;
if (m.attributes[PICodeParser::Static]) ni.flags |= Static;
if (m.attributes[PICodeParser::Mutable]) ni.flags |= Mutable;
if (m.attributes[PICodeParser::Volatile]) ni.flags |= Volatile;
if (m.attributes[PICodeParser::Inline]) ni.flags |= Inline;
if (m.attributes[PICodeParser::Virtual]) ni.flags |= Virtual;
ci.variables << ni;
}
piForeachC (PICodeParser::Member & m, e->functions) {
FunctionInfo fi;
fi.name = m.name;
fi.return_type.type = m.type;
if (m.attributes[PICodeParser::Const]) fi.return_type.flags |= Const;
if (m.attributes[PICodeParser::Static]) fi.return_type.flags |= Static;
piForeachC (PIString & a, m.arguments_full) {
TypeInfo ni;
PIString arg(a);
arg.prepend(" ");
if (arg.find(" const ") >= 0) {
ni.flags |= Const;
arg.replaceAll(" const ", " ");
}
int ts = 0;
for (ts = arg.size_s() - 1; ts >= 0; --ts)
if (!_isCChar(arg[ts]) && !(arg[ts].isDigit())) break;
ni.name = arg.takeRight(arg.size_s() - ts - 1).trim();
ni.type = arg.trim();
fi.arguments << ni;
}
ci.functions << fi;
}
piCout << NewLine << ci;
//piCout << v.toType<float>();
//piCout << v.toType<float>().toType<PIString>();
//PIFile::remove("ki");
/*PIConfig conf("protocols_commod.conf");
piCout << conf.allTree();
conf.setValue("rmd.123", 456);*/
/*PITimer tm;
piCout << tm.debug() << tm.properties();
tm.setDebug(false);
piCout << tm.debug() << tm.properties();
tm.setDebug(true);
piCout << tm.debug() << tm.properties();*/
//PIObject * ser = (PIObject * )PIIODevice::createFromFullPath("file://OM2:38400:7");
//piCout << ser << NewLine << ser->properties();
}