diff --git a/file_templates/CMakeLists.txt b/file_templates/CMakeLists.txt index e64333f3b6..56028aebd5 100644 --- a/file_templates/CMakeLists.txt +++ b/file_templates/CMakeLists.txt @@ -1,41 +1,46 @@ set(filetemplate_DIRS classes/cpp_basic classes/private_pointer classes/qt_widget classes/qobject + classes/qobject_pimpl classes/python_basic classes/c_gobject classes/c_gobject_private classes/c_gobject_properties classes/qt_shared classes/qt_interface testing/cpp_cpputest testing/cpp_gtest testing/cpp_qtestlib testing/cpp_qtestlib_kdevelop testing/python_pyunit testing/php_phpunit other/cmake_module ) kdevplatform_add_file_templates(${filetemplate_DIRS}) install ( FILES common/method_declaration_cpp.txt common/method_definition_cpp.txt common/license_header_cpp.txt common/class_declaration_cpp.txt common/class_declaration_apidox_cpp.txt common/class_method_declaration_cpp.txt common/class_method_declaration_apidox_cpp.txt common/class_property_setter_declaration_cpp.txt common/class_property_setter_declaration_apidox_cpp.txt + common/class_property_setter_definition_cpp.txt common/class_property_getter_declaration_cpp.txt common/class_property_getter_declaration_apidox_cpp.txt + common/class_property_getter_definition_cpp.txt + common/class_property_signal_declaration_cpp.txt + common/class_qproperty_declaration_cpp.txt common/cpp_header.h common/cpp_header_onlyfunctions.h common/cpp_implementation.cpp DESTINATION ${DATA_INSTALL_DIR}/kdevcodegen/templates ) diff --git a/file_templates/classes/qobject/class.cpp b/file_templates/classes/qobject/class.cpp index 04be01230a..07a5f8c041 100644 --- a/file_templates/classes/qobject/class.cpp +++ b/file_templates/classes/qobject/class.cpp @@ -1,67 +1,78 @@ {% extends "cpp_implementation.cpp" %} {% load kdev_filters %} {% block extra_definitions %} {% for method in private_functions %} {% with method.arguments as arguments %} {% include "method_definition_cpp.txt" %} { + {% if method.type %} return {{ method.default_return_value }}; {% endif %} - } {% endwith %} {% endfor %} {% endblock extra_definitions %} {% block function_definitions %} {% for method in public_functions %} {% with method.arguments as arguments %} {% include "method_definition_cpp.txt" %} { + {% if method.type %}return {{ method.default_return_value }}; {% endif %} + } {% endwith %} {% endfor %} {% for method in protected_functions %} {% with method.arguments as arguments %} {% include "method_definition_cpp.txt" %} { + {% if method.type %}return {{ method.default_return_value }}; {% endif %} + } {% endwith %} {% endfor %} {% for property in members %} -{{ property.type }} {{ name }}::{{ property.name }}() const + +{% include "class_property_getter_definition_cpp.txt" %} { return m_{{ property.name }}; } -void {{ name }}::set{{ property.name|upper_first }}({{ property.type|arg_type }} {{ property.name }}) +{% include "class_property_setter_definition_cpp.txt" %} { + if (m_{{ property.name }} == {{ property.name }}) { + return; + } + + m_{{ property.name }} = {{ property.name }}; + emit {{ property.name }}Changed(m_{{ property.name }}); } {% endfor %} {% endblock function_definitions %} {% block bottom %} {% endblock bottom %} diff --git a/file_templates/classes/qobject/class.h b/file_templates/classes/qobject/class.h index 2bc37111a2..6af1128829 100644 --- a/file_templates/classes/qobject/class.h +++ b/file_templates/classes/qobject/class.h @@ -1,82 +1,89 @@ {% extends "cpp_header.h" %} {% load kdev_filters %} {% block includes %} {{ block.super }} {% if not base_classes %} #include {% endif %} {% endblock includes %} {% block class_declaration_open %} {% if base_classes %} {{ block.super }} {% else %} {% include "class_declaration_apidox_cpp.txt" %} class {{ name }} : public QObject { {% endif %} {% endblock class_declaration_open %} {% block class_body %} Q_OBJECT {% for property in members %} - Q_PROPERTY({{ property.type }} {{ property.name }} READ {{ property.name }} WRITE set{{ property.name|upper_first }}) + {% include "class_qproperty_declaration_cpp.txt" %} {% endfor %} {% if public_functions or members %} public: {% endif %} {% for method in public_functions %} {% include "class_method_declaration_apidox_cpp.txt" %} {% include "class_method_declaration_cpp.txt" %} {% endfor %} {% for property in members %} {% include "class_property_getter_declaration_apidox_cpp.txt" %} {% include "class_property_getter_declaration_cpp.txt" %} {% endfor %} {% if members %} public Q_SLOTS: {% for property in members %} {% include "class_property_setter_declaration_apidox_cpp.txt" %} {% include "class_property_setter_declaration_cpp.txt" %} + {% endfor %} + +Q_SIGNALS: + {% for property in members %} + + {% include "class_property_signal_declaration_cpp.txt" %} + {% endfor %} {% endif %} {% if protected_functions %} protected: {% for method in protected_functions %} {% include "class_method_declaration_apidox_cpp.txt" %} {% include "class_method_declaration_cpp.txt" %} {% endfor %} {% endif %} {% if private_functions or members %} private: {% for method in private_functions %} {% include "class_method_declaration_apidox_cpp.txt" %} {% include "class_method_declaration_cpp.txt" %} {% endfor %} {% for property in members %} {{property.type}} m_{{property.name}}; {% endfor %} {% endif %} {% endblock class_body %} diff --git a/file_templates/classes/qobject_pimpl/class.cpp b/file_templates/classes/qobject_pimpl/class.cpp new file mode 100644 index 0000000000..7ea3bcb6cb --- /dev/null +++ b/file_templates/classes/qobject_pimpl/class.cpp @@ -0,0 +1,112 @@ +{% extends "cpp_implementation.cpp" %} +{% load kdev_filters %} + +{% block includes %} +#include "{{ output_file_header }}" +#include "{{ output_file_privateheader }}" +{% endblock includes %} + + +{% block extra_definitions %} + +{% for method in private_functions %} +{% with name|add:"Private" as name %} + +{% include "method_definition_cpp.txt" %} +{ + + {% if method.type %} + return {{ method.default_return_value }}; + {% endif %} + +} + +{% endwith %} +{% endfor %} + +{% endblock extra_definitions %} + +{% block function_definitions %} + +{% for method in public_functions %} +{% with method.arguments as arguments %} + +{% include "method_definition_cpp.txt" %} +{% if method.isConstructor %} + {% with arguments|first as argFirst %} + {# copy constructor? #} + {% if arguments|length == 1 and argFirst.type == method.name|arg_type %} + : d_ptr(new {{ name }}Private(*{{ argFirst.name }}.d_ptr)) + {% else %} + : d_ptr(new {{ name }}Private()) + {% endif %} + {% endwith %} +{% endif %} +{ + + {% if method.isDestructor %} + delete d_ptr; + {% endif %} + {% if method.type %}return {{ method.default_return_value }}; + {% endif %} + +} + +{% endwith %} +{% endfor %} + +{% for method in protected_functions %} +{% with method.arguments as arguments %} + +{% include "method_definition_cpp.txt" %} +{% if method.isConstructor %} + {% with arguments|first as argFirst %} + {# copy constructor? #} + {% if arguments|length == 1 and argFirst.type == method.name|arg_type %} + : d_ptr(new {{ name }}Private(*{{ argFirst.name }}.d_ptr)) + {% else %} + : d_ptr(new {{ name }}Private()) + {% endif %} + {% endwith %} +{% endif %} +{ + + {% if method.isDestructor %} + delete d_ptr; + {% endif %} + {% if method.type %}return {{ method.default_return_value }}; + {% endif %} + +} + +{% endwith %} +{% endfor %} + +{% for property in members %} + + +{% include "class_property_getter_definition_cpp.txt" %} +{ + Q_D(const {{ name }}); + return d->{{ property.name }}; +} + + +{% include "class_property_setter_definition_cpp.txt" %} +{ + Q_D({{ name }}); + if (d->{{ property.name }} == {{ property.name }}) { + return; + } + + + d->{{ property.name }} = {{ property.name }}; + emit {{ property.name }}Changed(d->{{ property.name }}); +} + +{% endfor %} + +{% endblock function_definitions %} + +{% block bottom %} +{% endblock bottom %} diff --git a/file_templates/classes/qobject/class.h b/file_templates/classes/qobject_pimpl/class.h similarity index 78% copy from file_templates/classes/qobject/class.h copy to file_templates/classes/qobject_pimpl/class.h index 2bc37111a2..30cfa8d7cd 100644 --- a/file_templates/classes/qobject/class.h +++ b/file_templates/classes/qobject_pimpl/class.h @@ -1,82 +1,79 @@ {% extends "cpp_header.h" %} {% load kdev_filters %} {% block includes %} {{ block.super }} {% if not base_classes %} #include {% endif %} {% endblock includes %} {% block class_declaration_open %} {% if base_classes %} {{ block.super }} {% else %} {% include "class_declaration_apidox_cpp.txt" %} class {{ name }} : public QObject { {% endif %} {% endblock class_declaration_open %} {% block class_body %} Q_OBJECT {% for property in members %} - Q_PROPERTY({{ property.type }} {{ property.name }} READ {{ property.name }} WRITE set{{ property.name|upper_first }}) + {% include "class_qproperty_declaration_cpp.txt" %} {% endfor %} {% if public_functions or members %} public: {% endif %} {% for method in public_functions %} {% include "class_method_declaration_apidox_cpp.txt" %} {% include "class_method_declaration_cpp.txt" %} {% endfor %} {% for property in members %} {% include "class_property_getter_declaration_apidox_cpp.txt" %} {% include "class_property_getter_declaration_cpp.txt" %} {% endfor %} {% if members %} public Q_SLOTS: {% for property in members %} {% include "class_property_setter_declaration_apidox_cpp.txt" %} {% include "class_property_setter_declaration_cpp.txt" %} + {% endfor %} + +Q_SIGNALS: + {% for property in members %} + + {% include "class_property_signal_declaration_cpp.txt" %} + {% endfor %} {% endif %} {% if protected_functions %} protected: {% for method in protected_functions %} {% include "class_method_declaration_apidox_cpp.txt" %} {% include "class_method_declaration_cpp.txt" %} {% endfor %} {% endif %} -{% if private_functions or members %} private: - {% for method in private_functions %} - - {% include "class_method_declaration_apidox_cpp.txt" %} - {% include "class_method_declaration_cpp.txt" %} - - {% endfor %} - - {% for property in members %} - {{property.type}} m_{{property.name}}; - {% endfor %} -{% endif %} + class {{ name }}Private* const d_ptr; + Q_DECLARE_PRIVATE({{ name }}) {% endblock class_body %} diff --git a/file_templates/classes/qobject_pimpl/class_p.h b/file_templates/classes/qobject_pimpl/class_p.h new file mode 100644 index 0000000000..a9b48ac310 --- /dev/null +++ b/file_templates/classes/qobject_pimpl/class_p.h @@ -0,0 +1,43 @@ +{% extends "cpp_header.h" %} +{% load kdev_filters %} + + +{% block include_guard_open %} +{% with "_P_H" as include_guard_suffix %} +#ifndef {% include "include_guard_cpp.txt" %} +#define {% include "include_guard_cpp.txt" %} +{% endwith %} +{% endblock include_guard_open %} + + +{% block includes %} +{% endblock includes %} + + +{% block class_declaration_open %} +{% include "class_declaration_apidox_cpp.txt" %} +class {{ name }}Private +{ +{% endblock class_declaration_open %} + +{% block class_body %} +{% if members or private_functions %} +public: + {% for method in private_functions %} + + {% include "class_method_declaration_apidox_cpp.txt" %} + {% include "class_method_declaration_cpp.txt" %} + + {% endfor %} + + {% for property in members %} + {{property.type}} {{property.name}}; + {% endfor %} +{% endif %} +{% endblock class_body %} + +{% block include_guard_close %} +{% with "_P_H" as include_guard_suffix %} +#endif // {% include "include_guard_cpp.txt" %} +{% endwith %} +{% endblock include_guard_close %} diff --git a/file_templates/classes/qobject_pimpl/qobject_pimpl.desktop b/file_templates/classes/qobject_pimpl/qobject_pimpl.desktop new file mode 100644 index 0000000000..1d1adede83 --- /dev/null +++ b/file_templates/classes/qobject_pimpl/qobject_pimpl.desktop @@ -0,0 +1,23 @@ +[General] +Name=QObject pimpl subclass +Comment=QObject subclass with private implementation +Category=C++/Qt +Language=C++ +Type=Class +BaseClasses=public QObject +Files=Header,PrivateHeader,Implementation + +[Header] +Name=Header +File=class.h +OutputFile={{ name }}.h + +[PrivateHeader] +Name=Private Header +File=class_p.h +OutputFile={{ name }}_p.h + +[Implementation] +Name=Implementation +File=class.cpp +OutputFile={{ name }}.cpp diff --git a/file_templates/common/class_method_declaration_apidox_cpp.txt b/file_templates/common/class_method_declaration_apidox_cpp.txt index bd68958626..c60d8156d7 100644 --- a/file_templates/common/class_method_declaration_apidox_cpp.txt +++ b/file_templates/common/class_method_declaration_apidox_cpp.txt @@ -1,23 +1,34 @@ {# Template for api dox in front of class method declaration #} +{% load kdev_filters %} {% with method.arguments as arguments %} {# standard four spaces indentation to match context #} /** {% if method.isConstructor %} - * {% if not arguments %}Default constructor{% else %}Constructor{% endif %} + {% if not arguments %} + * Default constructor + {% else %} + {% with arguments|first as argFirst %} + {% if arguments|length == 1 and argFirst.type == method.name|arg_type %} + * Copy Constructor + {% else %} + * Constructor + {% endif %} + {% endwith %} + {% endif %} {% elif method.isDestructor %} * Destructor {% else %} * @todo write docs {% endif %} {# and > or, so we go here if arguments or (returntype and not constructor/destructor) #} {% if arguments or method.returnType and not method.isConstructor and not method.isDestructor %} * {% endif %} {% for argument in arguments %} * @param {{ argument.name }} TODO {% endfor %} {% if method.returnType and not method.isConstructor and not method.isDestructor %} * @return TODO {% endif %} */ {% endwith %} diff --git a/file_templates/common/class_property_getter_definition_cpp.txt b/file_templates/common/class_property_getter_definition_cpp.txt new file mode 100644 index 0000000000..8135ad1c57 --- /dev/null +++ b/file_templates/common/class_property_getter_definition_cpp.txt @@ -0,0 +1 @@ +{{ property.type }} {{ name }}::{{ property.name }}() const diff --git a/file_templates/common/class_property_setter_definition_cpp.txt b/file_templates/common/class_property_setter_definition_cpp.txt new file mode 100644 index 0000000000..455e5a02e5 --- /dev/null +++ b/file_templates/common/class_property_setter_definition_cpp.txt @@ -0,0 +1,2 @@ +{% load kdev_filters %} +void {{ name }}::set{{ property.name|upper_first }}({{ property.type|arg_type }} {{ property.name }}) diff --git a/file_templates/common/class_property_signal_declaration_cpp.txt b/file_templates/common/class_property_signal_declaration_cpp.txt new file mode 100644 index 0000000000..3f2ea89ec5 --- /dev/null +++ b/file_templates/common/class_property_signal_declaration_cpp.txt @@ -0,0 +1,4 @@ +{# Template for class property signal method declaration #} +{% load kdev_filters %} + {# standard four spaces indentation to match context #} + void {{ property.name }}Changed({{ property.type|arg_type }} {{ property.name }}); diff --git a/file_templates/common/class_qproperty_declaration_cpp.txt b/file_templates/common/class_qproperty_declaration_cpp.txt new file mode 100644 index 0000000000..82128d56a9 --- /dev/null +++ b/file_templates/common/class_qproperty_declaration_cpp.txt @@ -0,0 +1,4 @@ +{# Template for class qproperty declaration #} +{% load kdev_filters %} + {# standard four spaces indentation to match context #} + Q_PROPERTY({{ property.type }} {{ property.name }} READ {{ property.name }} WRITE set{{ property.name|upper_first }} NOTIFY {{ property.name }}Changed)