diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index 5947e73a2..815af1162 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -26,7 +26,7 @@ set(base_SOURCES application.cpp application.thpp array.cpp configerror.cpp console.cpp context.cpp convert.cpp debuginfo.cpp dictionary.cpp dynamicobject.cpp dynamicobject.thpp dynamictype.cpp exception.cpp fifo.cpp filelogger.cpp filelogger.thpp json.cpp logger.cpp logger.thpp - netstring.cpp networkstream.cpp object.cpp objectlock.cpp process.cpp + netstring.cpp networkstream.cpp object.cpp objectlock.cpp primitivetype.cpp process.cpp ringbuffer.cpp scriptfunction.cpp scriptfunctionwrapper.cpp scriptutils.cpp scriptvariable.cpp serializer.cpp socket.cpp stacktrace.cpp statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp diff --git a/lib/base/array.cpp b/lib/base/array.cpp index b352e6ec4..3a6f4ccde 100644 --- a/lib/base/array.cpp +++ b/lib/base/array.cpp @@ -20,10 +20,13 @@ #include "base/array.hpp" #include "base/objectlock.hpp" #include "base/debug.hpp" +#include "base/primitivetype.hpp" #include using namespace icinga; +REGISTER_PRIMITIVE_TYPE(Array); + /** * Restrieves a value from an array. * diff --git a/lib/base/dictionary.cpp b/lib/base/dictionary.cpp index cec0961a1..b87f1e64f 100644 --- a/lib/base/dictionary.cpp +++ b/lib/base/dictionary.cpp @@ -20,10 +20,13 @@ #include "base/dictionary.hpp" #include "base/objectlock.hpp" #include "base/debug.hpp" +#include "base/primitivetype.hpp" #include using namespace icinga; +REGISTER_PRIMITIVE_TYPE(Dictionary); + /** * Compares dictionary keys using the less operator. */ diff --git a/lib/base/primitivetype.cpp b/lib/base/primitivetype.cpp new file mode 100644 index 000000000..306070c63 --- /dev/null +++ b/lib/base/primitivetype.cpp @@ -0,0 +1,61 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * + * * + * 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 2 * + * 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, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#include "base/primitivetype.hpp" + +using namespace icinga; + +REGISTER_PRIMITIVE_TYPE(int); +REGISTER_PRIMITIVE_TYPE(double); +REGISTER_PRIMITIVE_TYPE(bool); + +PrimitiveType::PrimitiveType(const String& name) + : m_Name(name) +{ } + +String PrimitiveType::GetName(void) const +{ + return m_Name; +} + +const Type *PrimitiveType::GetBaseType(void) const +{ + return NULL; +} + +int PrimitiveType::GetAttributes(void) const +{ + return 0; +} + +int PrimitiveType::GetFieldId(const String& name) const +{ + return -1; +} + +Field PrimitiveType::GetFieldInfo(int id) const +{ + throw std::runtime_error("Invalid field ID."); +} + +int PrimitiveType::GetFieldCount(void) const +{ + return 0; +} + diff --git a/lib/base/primitivetype.hpp b/lib/base/primitivetype.hpp new file mode 100644 index 000000000..8e82edaa9 --- /dev/null +++ b/lib/base/primitivetype.hpp @@ -0,0 +1,59 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * + * * + * 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 2 * + * 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, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#ifndef PRIMITIVETYPE_H +#define PRIMITIVETYPE_H + +#include "base/i2-base.hpp" +#include "base/type.hpp" +#include "base/initialize.hpp" + +namespace icinga +{ + +class I2_BASE_API PrimitiveType : public Type +{ +public: + PrimitiveType(const String& name); + + virtual String GetName(void) const; + virtual const Type *GetBaseType(void) const; + virtual int GetAttributes(void) const; + virtual int GetFieldId(const String& name) const; + virtual Field GetFieldInfo(int id) const; + virtual int GetFieldCount(void) const; + +private: + String m_Name; +}; + +#define REGISTER_PRIMITIVE_TYPE(type) \ + namespace { namespace UNIQUE_NAME(prt) { \ + void RegisterPrimitiveType ## type(void) \ + { \ + icinga::Type *t = new PrimitiveType(#type); \ + icinga::Type::Register(t); \ + } \ + \ + INITIALIZE_ONCE(RegisterPrimitiveType ## type); \ + } } + +} + +#endif /* PRIMITIVETYPE_H */ diff --git a/lib/base/string.cpp b/lib/base/string.cpp index 279cd44cf..a11a901aa 100644 --- a/lib/base/string.cpp +++ b/lib/base/string.cpp @@ -19,11 +19,14 @@ #include "base/string.hpp" #include "base/value.hpp" +#include "base/primitivetype.hpp" #include #include using namespace icinga; +REGISTER_PRIMITIVE_TYPE(String); + const String::SizeType String::NPos = std::string::npos; String::String(void) diff --git a/lib/base/type.hpp b/lib/base/type.hpp index f26b3fcee..d8ebf8046 100644 --- a/lib/base/type.hpp +++ b/lib/base/type.hpp @@ -35,15 +35,18 @@ enum FieldAttribute FAConfig = 1, FAState = 2 }; - + +class Type; + struct Field { int ID; + const Type *FType; const char *Name; int Attributes; - Field(int id, const char *name, int attributes) - : ID(id), Name(name), Attributes(attributes) + Field(int id, const Type *type, const char *name, int attributes) + : ID(id), FType(type), Name(name), Attributes(attributes) { } }; @@ -105,14 +108,14 @@ struct FactoryHelper #define REGISTER_TYPE(type) \ namespace { namespace UNIQUE_NAME(rt) { \ - void RegisterType(void) \ + void RegisterType ## type(void) \ { \ icinga::Type *t = new TypeImpl(); \ t->SetFactory(FactoryHelper().GetFactory()); \ icinga::Type::Register(t); \ } \ \ - INITIALIZE_ONCE(RegisterType); \ + INITIALIZE_ONCE(RegisterType ## type); \ } } } diff --git a/lib/cli/clicommand.cpp b/lib/cli/clicommand.cpp index 441dd2e92..36bd388b7 100644 --- a/lib/cli/clicommand.cpp +++ b/lib/cli/clicommand.cpp @@ -78,6 +78,10 @@ std::vector icinga::GetFieldCompletionSuggestions(const Type *type, cons if (!(field.Attributes & FAConfig)) continue; + if (field.FType != Type::GetByName("int") && field.FType != Type::GetByName("double") + && field.FType != Type::GetByName("bool") && field.FType != Type::GetByName("String")) + continue; + String fname = field.Name; if (fname == "__name" || fname == "templates" || fname == "type") diff --git a/tools/mkclass/classcompiler.cpp b/tools/mkclass/classcompiler.cpp index 2b87cf4e6..4795fecff 100644 --- a/tools/mkclass/classcompiler.cpp +++ b/tools/mkclass/classcompiler.cpp @@ -253,8 +253,16 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&) size_t num = 0; for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) { + std::string ftype = it->Type; + + if (ftype.find("::Ptr") == ftype.size() - strlen("::Ptr")) + ftype = ftype.substr(0, ftype.size() - strlen("::Ptr")); + + if (it->Attributes & FAEnum) + ftype = "int"; + std::cout << "\t\t\t" << "case " << num << ":" << std::endl - << "\t\t\t\t" << "return Field(" << num << ", \"" << it->Name << "\", " << it->Attributes << ");" << std::endl; + << "\t\t\t\t" << "return Field(" << num << ", Type::GetByName(\"" << ftype << "\"), \"" << it->Name << "\", " << it->Attributes << ");" << std::endl; num++; }