1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

pub mod header;
pub mod source;

use crate::generator::cpp::{fragment::CppFragmentPair, GeneratedCppBlocks};
use clang_format::clang_format;
use header::write_cpp_header;
use source::write_cpp_source;

/// For a given GeneratedCppBlocks write the namespace pair
pub fn namespace_pair(generated: &GeneratedCppBlocks) -> (String, String) {
    let namespace_start = if generated.namespace.is_empty() {
        "".to_owned()
    } else {
        format!("namespace {namespace} {{", namespace = generated.namespace)
    };
    let namespace_end = if generated.namespace.is_empty() {
        "".to_owned()
    } else {
        format!(
            "}} // namespace {namespace}",
            namespace = generated.namespace
        )
    };
    (namespace_start, namespace_end)
}

/// For a given GeneratedCppBlocks write this into a C++ header and source pair
pub fn write_cpp(generated: &GeneratedCppBlocks) -> CppFragmentPair {
    let header = write_cpp_header(generated);
    let source = write_cpp_source(generated);

    CppFragmentPair {
        header: clang_format(&header).unwrap_or(header),
        source: clang_format(&source).unwrap_or(source),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::generator::cpp::qobject::{GeneratedCppQObject, GeneratedCppQObjectBlocks};
    use indoc::indoc;
    use pretty_assertions::assert_str_eq;

    /// Helper to create a GeneratedCppBlocks for testing
    pub fn create_generated_cpp() -> GeneratedCppBlocks {
        GeneratedCppBlocks {
            cxx_file_stem: "cxx_file_stem".to_owned(),
            namespace: "cxx_qt::my_object".to_owned(),
            qobjects: vec![
                GeneratedCppQObject {
                    ident: "MyObject".to_owned(),
                    rust_ident: "MyObjectRust".to_owned(),
                    cxx_qt_thread_ident: "MyObjectCxxQtThread".to_owned(),
                    namespace_internals: "cxx_qt::my_object::cxx_qt_my_object".to_owned(),
                    base_class: "QStringListModel".to_owned(),
                    blocks: GeneratedCppQObjectBlocks {
                        metaobjects: vec![
                            "Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)".to_owned(),
                            "Q_PROPERTY(bool longPropertyNameThatWrapsInClangFormat READ getToggle WRITE setToggle NOTIFY toggleChanged)"
                                .to_owned(),
                        ],
                        methods: vec![
                            CppFragmentPair {
                                header: "int count() const;".to_owned(),
                                source: indoc! {r#"
                                    int
                                    MyObject::count() const
                                    {
                                      // getter
                                    }
                                "#}
                                .to_owned(),
                            },
                            CppFragmentPair {
                                header: "bool toggle() const;".to_owned(),
                                source: indoc! {r#"
                                    bool
                                    MyObject::toggle() const
                                    {
                                      // getter
                                    }
                                "#}
                                .to_owned(),
                            },
                            CppFragmentPair {
                                header: "Q_INVOKABLE void invokable();".to_owned(),
                                source: indoc! {r#"
                                    void
                                    MyObject::invokable()
                                    {
                                      // invokable method
                                    }
                                "#}
                                .to_owned(),
                            },
                            CppFragmentPair {
                                header: "void cppMethod();".to_owned(),
                                source: indoc! {r#"
                                    void
                                    MyObject::cppMethod()
                                    {
                                      // cpp method
                                    }
                                "#}
                                .to_owned(),
                            },
                        ],
                        slots: vec![
                            CppFragmentPair {
                                header: "void setCount(int count);".to_owned(),
                                source: indoc! {r#"
                                    void
                                    MyObject::setCount(int count) const
                                    {
                                      // setter
                                    }
                                "#}
                                .to_owned(),
                            },
                            CppFragmentPair {
                                header: "void setToggle(bool toggle);".to_owned(),
                                source: indoc! {r#"
                                    void
                                    MyObject::setToggle(bool toggle) const
                                    {
                                      // setter
                                    }
                                "#}
                                .to_owned(),
                            },
                        ],
                        signals: vec![
                            "void countChanged();".to_owned(),
                            "void toggleChanged();".to_owned(),
                        ],
                    }
                }
            ],
        }
    }

    /// Helper to create a GeneratedCppBlocks for testing with multiple qobjects
    pub fn create_generated_cpp_multi_qobjects() -> GeneratedCppBlocks {
        GeneratedCppBlocks {
            cxx_file_stem: "cxx_file_stem".to_owned(),
            namespace: "cxx_qt".to_owned(),
            qobjects: vec![
                GeneratedCppQObject {
                    ident: "FirstObject".to_owned(),
                    rust_ident: "FirstObjectRust".to_owned(),
                    cxx_qt_thread_ident: "FirstObjectCxxQtThread".to_owned(),
                    namespace_internals: "cxx_qt::cxx_qt_first_object".to_owned(),
                    base_class: "QStringListModel".to_owned(),
                    blocks: GeneratedCppQObjectBlocks {
                        metaobjects: vec![
                            "Q_PROPERTY(int longPropertyNameThatWrapsInClangFormat READ count WRITE setCount NOTIFY countChanged)"
                                .to_owned(),
                        ],
                        methods: vec![CppFragmentPair {
                            header: "int count() const;".to_owned(),
                            source: indoc! {r#"
                                    int
                                    FirstObject::count() const
                                    {
                                      // getter
                                    }
                                "#}
                            .to_owned(),
                        }],
                        slots: vec![CppFragmentPair {
                            header: "void setCount(int count);".to_owned(),
                            source: indoc! {r#"
                                    void
                                    FirstObject::setCount(int count) const
                                    {
                                      // setter
                                    }
                                "#}
                            .to_owned(),
                        }],
                        signals: vec!["void countChanged();".to_owned()],
                    }
                },
                GeneratedCppQObject {
                    ident: "SecondObject".to_owned(),
                    rust_ident: "SecondObjectRust".to_owned(),
                    cxx_qt_thread_ident: "SecondObjectCxxQtThread".to_owned(),
                    namespace_internals: "cxx_qt::cxx_qt_second_object".to_owned(),
                    base_class: "QStringListModel".to_owned(),
                    blocks: GeneratedCppQObjectBlocks {
                        metaobjects: vec![
                            "Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)"
                                .to_owned(),
                        ],
                        methods: vec![CppFragmentPair {
                            header: "int count() const;".to_owned(),
                            source: indoc! {r#"
                                    int
                                    SecondObject::count() const
                                    {
                                      // getter
                                    }
                                "#}
                            .to_owned(),
                        }],
                        slots: vec![CppFragmentPair {
                            header: "void setCount(int count);".to_owned(),
                            source: indoc! {r#"
                                    void
                                    SecondObject::setCount(int count) const
                                    {
                                      // setter
                                    }
                                "#}
                            .to_owned(),
                        }],
                        signals: vec!["void countChanged();".to_owned()],
                    }
                },
            ],
        }
    }

    /// Helper to create a GeneratedCppBlocks with no namespace for testing
    pub fn create_generated_cpp_no_namespace() -> GeneratedCppBlocks {
        let mut generated = create_generated_cpp();
        generated.namespace = "".to_owned();
        generated.qobjects[0].namespace_internals = "cxx_qt_my_object".to_owned();
        generated
    }

    /// Helper for the expected header
    pub fn expected_header() -> &'static str {
        indoc! {r#"
        #pragma once

        #include <memory>
        #include <mutex>

        namespace rust::cxxqtlib1 {
        template<typename T>
        class CxxQtThread;
        }

        namespace cxx_qt::my_object {
        class MyObject;
        using MyObjectCxxQtThread = rust::cxxqtlib1::CxxQtThread<MyObject>;
        } // namespace cxx_qt::my_object

        #include "cxx-qt-gen/cxx_file_stem.cxx.h"

        namespace cxx_qt::my_object {
        class MyObject : public QStringListModel
        {
          Q_OBJECT
          Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)
          Q_PROPERTY(bool longPropertyNameThatWrapsInClangFormat READ getToggle WRITE setToggle NOTIFY toggleChanged)

        public:
          explicit MyObject(QObject* parent = nullptr);
          ~MyObject();
          const MyObjectRust& unsafeRust() const;
          MyObjectRust& unsafeRustMut();
          std::unique_ptr<MyObjectCxxQtThread> qtThread() const;

        public:
          int count() const;
          bool toggle() const;
          Q_INVOKABLE void invokable();
          void cppMethod();

        public Q_SLOTS:
          void setCount(int count);
          void setToggle(bool toggle);

        Q_SIGNALS:
          void countChanged();
          void toggleChanged();

        private:
          rust::Box<MyObjectRust> m_rustObj;
          std::shared_ptr<std::recursive_mutex> m_rustObjMutex;
          std::shared_ptr<rust::cxxqtlib1::CxxQtGuardedPointer<MyObject>> m_cxxQtThreadObj;
        };

        static_assert(std::is_base_of<QObject, MyObject>::value, "MyObject must inherit from QObject");
        } // namespace cxx_qt::my_object

        namespace cxx_qt::my_object::cxx_qt_my_object {
        std::unique_ptr<MyObject>
        newCppObject();
        } // namespace cxx_qt::my_object::cxx_qt_my_object

        Q_DECLARE_METATYPE(cxx_qt::my_object::MyObject*)

        "#}
    }

    /// Helper for the expected header with multiple QObjects
    pub fn expected_header_multi_qobjects() -> &'static str {
        indoc! {r#"
        #pragma once

        #include <memory>
        #include <mutex>

        namespace rust::cxxqtlib1 {
        template<typename T>
        class CxxQtThread;
        }

        namespace cxx_qt {
        class FirstObject;
        using FirstObjectCxxQtThread = rust::cxxqtlib1::CxxQtThread<FirstObject>;
        } // namespace cxx_qt

        namespace cxx_qt {
        class SecondObject;
        using SecondObjectCxxQtThread = rust::cxxqtlib1::CxxQtThread<SecondObject>;
        } // namespace cxx_qt

        #include "cxx-qt-gen/cxx_file_stem.cxx.h"

        namespace cxx_qt {
        class FirstObject : public QStringListModel
        {
          Q_OBJECT
          Q_PROPERTY(int longPropertyNameThatWrapsInClangFormat READ count WRITE setCount NOTIFY countChanged)

        public:
          explicit FirstObject(QObject* parent = nullptr);
          ~FirstObject();
          const FirstObjectRust& unsafeRust() const;
          FirstObjectRust& unsafeRustMut();
          std::unique_ptr<FirstObjectCxxQtThread> qtThread() const;

        public:
          int count() const;

        public Q_SLOTS:
          void setCount(int count);

        Q_SIGNALS:
          void countChanged();

        private:
          rust::Box<FirstObjectRust> m_rustObj;
          std::shared_ptr<std::recursive_mutex> m_rustObjMutex;
          std::shared_ptr<rust::cxxqtlib1::CxxQtGuardedPointer<FirstObject>> m_cxxQtThreadObj;
        };

        static_assert(std::is_base_of<QObject, FirstObject>::value, "FirstObject must inherit from QObject");
        } // namespace cxx_qt

        namespace cxx_qt::cxx_qt_first_object {
        std::unique_ptr<FirstObject>
        newCppObject();
        } // namespace cxx_qt::cxx_qt_first_object

        Q_DECLARE_METATYPE(cxx_qt::FirstObject*)

        namespace cxx_qt {
        class SecondObject : public QStringListModel
        {
          Q_OBJECT
          Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)

        public:
          explicit SecondObject(QObject* parent = nullptr);
          ~SecondObject();
          const SecondObjectRust& unsafeRust() const;
          SecondObjectRust& unsafeRustMut();
          std::unique_ptr<SecondObjectCxxQtThread> qtThread() const;

        public:
          int count() const;

        public Q_SLOTS:
          void setCount(int count);

        Q_SIGNALS:
          void countChanged();

        private:
          rust::Box<SecondObjectRust> m_rustObj;
          std::shared_ptr<std::recursive_mutex> m_rustObjMutex;
          std::shared_ptr<rust::cxxqtlib1::CxxQtGuardedPointer<SecondObject>> m_cxxQtThreadObj;
        };

        static_assert(std::is_base_of<QObject, SecondObject>::value, "SecondObject must inherit from QObject");
        } // namespace cxx_qt

        namespace cxx_qt::cxx_qt_second_object {
        std::unique_ptr<SecondObject>
        newCppObject();
        } // namespace cxx_qt::cxx_qt_second_object

        Q_DECLARE_METATYPE(cxx_qt::SecondObject*)

        "#}
    }

    /// Helper for the expected header with no namespace
    pub fn expected_header_no_namespace() -> &'static str {
        indoc! {r#"
        #pragma once

        #include <memory>
        #include <mutex>

        namespace rust::cxxqtlib1 {
        template<typename T>
        class CxxQtThread;
        }


        class MyObject;
        using MyObjectCxxQtThread = rust::cxxqtlib1::CxxQtThread<MyObject>;


        #include "cxx-qt-gen/cxx_file_stem.cxx.h"


        class MyObject : public QStringListModel
        {
          Q_OBJECT
          Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged)
          Q_PROPERTY(bool longPropertyNameThatWrapsInClangFormat READ getToggle WRITE setToggle NOTIFY toggleChanged)

        public:
          explicit MyObject(QObject* parent = nullptr);
          ~MyObject();
          const MyObjectRust& unsafeRust() const;
          MyObjectRust& unsafeRustMut();
          std::unique_ptr<MyObjectCxxQtThread> qtThread() const;

        public:
          int count() const;
          bool toggle() const;
          Q_INVOKABLE void invokable();
          void cppMethod();

        public Q_SLOTS:
          void setCount(int count);
          void setToggle(bool toggle);

        Q_SIGNALS:
          void countChanged();
          void toggleChanged();

        private:
          rust::Box<MyObjectRust> m_rustObj;
          std::shared_ptr<std::recursive_mutex> m_rustObjMutex;
          std::shared_ptr<rust::cxxqtlib1::CxxQtGuardedPointer<MyObject>> m_cxxQtThreadObj;
        };

        static_assert(std::is_base_of<QObject, MyObject>::value, "MyObject must inherit from QObject");


        namespace cxx_qt_my_object {
        std::unique_ptr<MyObject>
        newCppObject();
        } // namespace cxx_qt_my_object

        Q_DECLARE_METATYPE(MyObject*)

        "#}
    }

    /// Helper for the expected source
    pub fn expected_source() -> &'static str {
        indoc! {r#"
        #include "cxx-qt-gen/cxx_file_stem.cxxqt.h"

        namespace cxx_qt::my_object {

        MyObject::MyObject(QObject* parent)
          : QStringListModel(parent)
          , m_rustObj(cxx_qt::my_object::cxx_qt_my_object::createRs())
          , m_rustObjMutex(std::make_shared<std::recursive_mutex>())
          , m_cxxQtThreadObj(std::make_shared<rust::cxxqtlib1::CxxQtGuardedPointer<MyObject>>(this))
        {
        }

        MyObject::~MyObject()
        {
          const auto guard = std::unique_lock(m_cxxQtThreadObj->mutex);
          m_cxxQtThreadObj->ptr = nullptr;
        }

        const MyObjectRust&
        MyObject::unsafeRust() const
        {
          return *m_rustObj;
        }

        MyObjectRust&
        MyObject::unsafeRustMut()
        {
          return *m_rustObj;
        }

        std::unique_ptr<MyObjectCxxQtThread>
        MyObject::qtThread() const
        {
          return std::make_unique<MyObjectCxxQtThread>(m_cxxQtThreadObj, m_rustObjMutex);
        }

        int
        MyObject::count() const
        {
          // getter
        }

        bool
        MyObject::toggle() const
        {
          // getter
        }

        void
        MyObject::invokable()
        {
          // invokable method
        }

        void
        MyObject::cppMethod()
        {
          // cpp method
        }

        void
        MyObject::setCount(int count) const
        {
          // setter
        }

        void
        MyObject::setToggle(bool toggle) const
        {
          // setter
        }

        } // namespace cxx_qt::my_object

        namespace cxx_qt::my_object::cxx_qt_my_object {
        std::unique_ptr<MyObject>
        newCppObject()
        {
          return std::make_unique<MyObject>();
        }
        } // namespace cxx_qt::my_object::cxx_qt_my_object

        "#}
    }

    /// Helper for the expected source with multiple QObjects
    pub fn expected_source_multi_qobjects() -> &'static str {
        indoc! {r#"
        #include "cxx-qt-gen/cxx_file_stem.cxxqt.h"

        namespace cxx_qt {

        FirstObject::FirstObject(QObject* parent)
          : QStringListModel(parent)
          , m_rustObj(cxx_qt::cxx_qt_first_object::createRs())
          , m_rustObjMutex(std::make_shared<std::recursive_mutex>())
          , m_cxxQtThreadObj(std::make_shared<rust::cxxqtlib1::CxxQtGuardedPointer<FirstObject>>(this))
        {
        }

        FirstObject::~FirstObject()
        {
          const auto guard = std::unique_lock(m_cxxQtThreadObj->mutex);
          m_cxxQtThreadObj->ptr = nullptr;
        }

        const FirstObjectRust&
        FirstObject::unsafeRust() const
        {
          return *m_rustObj;
        }

        FirstObjectRust&
        FirstObject::unsafeRustMut()
        {
          return *m_rustObj;
        }

        std::unique_ptr<FirstObjectCxxQtThread>
        FirstObject::qtThread() const
        {
          return std::make_unique<FirstObjectCxxQtThread>(m_cxxQtThreadObj, m_rustObjMutex);
        }

        int
        FirstObject::count() const
        {
          // getter
        }

        void
        FirstObject::setCount(int count) const
        {
          // setter
        }

        } // namespace cxx_qt

        namespace cxx_qt::cxx_qt_first_object {
        std::unique_ptr<FirstObject>
        newCppObject()
        {
          return std::make_unique<FirstObject>();
        }
        } // namespace cxx_qt::cxx_qt_first_object

        namespace cxx_qt {

        SecondObject::SecondObject(QObject* parent)
          : QStringListModel(parent)
          , m_rustObj(cxx_qt::cxx_qt_second_object::createRs())
          , m_rustObjMutex(std::make_shared<std::recursive_mutex>())
          , m_cxxQtThreadObj(std::make_shared<rust::cxxqtlib1::CxxQtGuardedPointer<SecondObject>>(this))
        {
        }

        SecondObject::~SecondObject()
        {
          const auto guard = std::unique_lock(m_cxxQtThreadObj->mutex);
          m_cxxQtThreadObj->ptr = nullptr;
        }

        const SecondObjectRust&
        SecondObject::unsafeRust() const
        {
          return *m_rustObj;
        }

        SecondObjectRust&
        SecondObject::unsafeRustMut()
        {
          return *m_rustObj;
        }

        std::unique_ptr<SecondObjectCxxQtThread>
        SecondObject::qtThread() const
        {
          return std::make_unique<SecondObjectCxxQtThread>(m_cxxQtThreadObj, m_rustObjMutex);
        }

        int
        SecondObject::count() const
        {
          // getter
        }

        void
        SecondObject::setCount(int count) const
        {
          // setter
        }

        } // namespace cxx_qt

        namespace cxx_qt::cxx_qt_second_object {
        std::unique_ptr<SecondObject>
        newCppObject()
        {
          return std::make_unique<SecondObject>();
        }
        } // namespace cxx_qt::cxx_qt_second_object

        "#}
    }

    /// Helper for the expected header with no namespace
    pub fn expected_source_no_namespace() -> &'static str {
        indoc! {r#"
        #include "cxx-qt-gen/cxx_file_stem.cxxqt.h"



        MyObject::MyObject(QObject* parent)
          : QStringListModel(parent)
          , m_rustObj(cxx_qt_my_object::createRs())
          , m_rustObjMutex(std::make_shared<std::recursive_mutex>())
          , m_cxxQtThreadObj(std::make_shared<rust::cxxqtlib1::CxxQtGuardedPointer<MyObject>>(this))
        {
        }

        MyObject::~MyObject()
        {
          const auto guard = std::unique_lock(m_cxxQtThreadObj->mutex);
          m_cxxQtThreadObj->ptr = nullptr;
        }

        const MyObjectRust&
        MyObject::unsafeRust() const
        {
          return *m_rustObj;
        }

        MyObjectRust&
        MyObject::unsafeRustMut()
        {
          return *m_rustObj;
        }

        std::unique_ptr<MyObjectCxxQtThread>
        MyObject::qtThread() const
        {
          return std::make_unique<MyObjectCxxQtThread>(m_cxxQtThreadObj, m_rustObjMutex);
        }

        int
        MyObject::count() const
        {
          // getter
        }

        bool
        MyObject::toggle() const
        {
          // getter
        }

        void
        MyObject::invokable()
        {
          // invokable method
        }

        void
        MyObject::cppMethod()
        {
          // cpp method
        }

        void
        MyObject::setCount(int count) const
        {
          // setter
        }

        void
        MyObject::setToggle(bool toggle) const
        {
          // setter
        }



        namespace cxx_qt_my_object {
        std::unique_ptr<MyObject>
        newCppObject()
        {
          return std::make_unique<MyObject>();
        }
        } // namespace cxx_qt_my_object

        "#}
    }

    #[test]
    fn test_write_cpp() {
        let generated = create_generated_cpp();
        let result = write_cpp(&generated);
        assert_str_eq!(result.header, clang_format(expected_header()).unwrap());
        assert_str_eq!(result.source, clang_format(expected_source()).unwrap());
    }

    #[test]
    fn test_write_cpp_multi_qobjects() {
        let generated = create_generated_cpp_multi_qobjects();
        let result = write_cpp(&generated);
        assert_str_eq!(
            result.header,
            clang_format(expected_header_multi_qobjects()).unwrap()
        );
        assert_str_eq!(
            result.source,
            clang_format(expected_source_multi_qobjects()).unwrap()
        );
    }

    #[test]
    fn test_write_cpp_no_namespace() {
        let generated = create_generated_cpp_no_namespace();
        let result = write_cpp(&generated);
        assert_str_eq!(
            result.header,
            clang_format(expected_header_no_namespace()).unwrap()
        );
        assert_str_eq!(
            result.source,
            clang_format(expected_source_no_namespace()).unwrap()
        );
    }
}