]> granicus.if.org Git - icinga2/commitdiff
Hide attributes in command auto-completion which cannot be set
authorGunnar Beutner <gunnar@beutner.name>
Fri, 31 Oct 2014 07:49:14 +0000 (08:49 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Fri, 31 Oct 2014 07:49:14 +0000 (08:49 +0100)
refs #7403

lib/base/CMakeLists.txt
lib/base/array.cpp
lib/base/dictionary.cpp
lib/base/primitivetype.cpp [new file with mode: 0644]
lib/base/primitivetype.hpp [new file with mode: 0644]
lib/base/string.cpp
lib/base/type.hpp
lib/cli/clicommand.cpp
tools/mkclass/classcompiler.cpp

index 5947e73a2cd487655512c08f4f70794303041f17..815af1162a4da0291d732c194b0af4eb376e1d42 100644 (file)
@@ -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 
index b352e6ec44b66215cf1b0e42b1462867c15ea21f..3a6f4ccde1c3360f8c3eebad2c561390e85cec56 100644 (file)
 #include "base/array.hpp"
 #include "base/objectlock.hpp"
 #include "base/debug.hpp"
+#include "base/primitivetype.hpp"
 #include <boost/foreach.hpp>
 
 using namespace icinga;
 
+REGISTER_PRIMITIVE_TYPE(Array);
+
 /**
  * Restrieves a value from an array.
  *
index cec0961a1c20bd1ea9860a853757f3e0cea3b6f0..b87f1e64f097809be4a7adc48ca94fd05f8692cd 100644 (file)
 #include "base/dictionary.hpp"
 #include "base/objectlock.hpp"
 #include "base/debug.hpp"
+#include "base/primitivetype.hpp"
 #include <boost/foreach.hpp>
 
 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 (file)
index 0000000..306070c
--- /dev/null
@@ -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 (file)
index 0000000..8e82eda
--- /dev/null
@@ -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 */
index 279cd44cfc14effaabd0140031a2a6ff9602c176..a11a901aaf479a049edf64f5ba8293f3c7b134cc 100644 (file)
 
 #include "base/string.hpp"
 #include "base/value.hpp"
+#include "base/primitivetype.hpp"
 #include <boost/algorithm/string/trim.hpp>
 #include <ostream>
 
 using namespace icinga;
 
+REGISTER_PRIMITIVE_TYPE(String);
+
 const String::SizeType String::NPos = std::string::npos;
 
 String::String(void)
index f26b3fcee60d1bb9023fefaf9fe45b64baac2ed9..d8ebf804694191aa6a4ac91b58d3af7f5873578a 100644 (file)
@@ -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<type>(); \
                        t->SetFactory(FactoryHelper<type>().GetFactory()); \
                        icinga::Type::Register(t); \
                } \
                \
-               INITIALIZE_ONCE(RegisterType); \
+               INITIALIZE_ONCE(RegisterType ## type); \
        } }
 
 }
index 441dd2e925a671bb990fcbb1bc198e9ed3fe5c19..36bd388b720c8b5400d1ddb79293ad8d1165338d 100644 (file)
@@ -78,6 +78,10 @@ std::vector<String> 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")
index 2b87cf4e66b2010f11c5d8b75c58ddaed26b0b6b..4795fecff5615eb3eec0ce2e503334e644197441 100644 (file)
@@ -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++;
                }