Commit 9ad63a8e authored by Andrew Twyman's avatar Andrew Twyman

Merge pull request #166 from vi/forward_declare_classes_for_interfaces

Forward declare instead of #include enums/records from interface's hpp
parents 010ef4ca 02cae9d1
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
#pragma once #pragma once
#include "item_list.hpp"
#include "sort_order.hpp"
#include <memory> #include <memory>
namespace textsort { namespace textsort {
class TextboxListener; class TextboxListener;
enum class sort_order;
struct ItemList;
class SortItems { class SortItems {
public: public:
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
#pragma once #pragma once
#include "item_list.hpp"
namespace textsort { namespace textsort {
struct ItemList;
class TextboxListener { class TextboxListener {
public: public:
virtual ~TextboxListener() {} virtual ~TextboxListener() {}
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
- (nonnull instancetype)initWithItems:(nonnull NSArray<NSString *> *)items - (nonnull instancetype)initWithItems:(nonnull NSArray<NSString *> *)items
{ {
if (self = [super init]) { if (self = [super init]) {
_items = items; _items = [items copy];
} }
return self; return self;
} }
......
...@@ -37,12 +37,12 @@ class CppGenerator(spec: Spec) extends Generator(spec) { ...@@ -37,12 +37,12 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
var hppFwds = mutable.TreeSet[String]() var hppFwds = mutable.TreeSet[String]()
var cpp = mutable.TreeSet[String]() var cpp = mutable.TreeSet[String]()
def find(ty: TypeRef) { find(ty.resolved) } def find(ty: TypeRef, forwardDeclareOnly: Boolean) { find(ty.resolved, forwardDeclareOnly) }
def find(tm: MExpr) { def find(tm: MExpr, forwardDeclareOnly: Boolean) {
tm.args.foreach(find) tm.args.foreach((x) => find(x, forwardDeclareOnly))
find(tm.base) find(tm.base, forwardDeclareOnly)
} }
def find(m: Meta) = for(r <- marshal.references(m, name)) r match { def find(m: Meta, forwardDeclareOnly : Boolean) = for(r <- marshal.references(m, name, forwardDeclareOnly)) r match {
case ImportRef(arg) => hpp.add("#include " + arg) case ImportRef(arg) => hpp.add("#include " + arg)
case DeclRef(decl, Some(spec.cppNamespace)) => hppFwds.add(decl) case DeclRef(decl, Some(spec.cppNamespace)) => hppFwds.add(decl)
case DeclRef(_, _) => case DeclRef(_, _) =>
...@@ -130,8 +130,8 @@ class CppGenerator(spec: Spec) extends Generator(spec) { ...@@ -130,8 +130,8 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
override def generateRecord(origin: String, ident: Ident, doc: Doc, params: Seq[TypeParam], r: Record) { override def generateRecord(origin: String, ident: Ident, doc: Doc, params: Seq[TypeParam], r: Record) {
val refs = new CppRefs(ident.name) val refs = new CppRefs(ident.name)
r.fields.foreach(f => refs.find(f.ty)) r.fields.foreach(f => refs.find(f.ty, false))
r.consts.foreach(c => refs.find(c.ty)) r.consts.foreach(c => refs.find(c.ty, false))
refs.hpp.add("#include <utility>") // Add for std::move refs.hpp.add("#include <utility>") // Add for std::move
val self = marshal.typename(ident, r) val self = marshal.typename(ident, r)
...@@ -255,11 +255,11 @@ class CppGenerator(spec: Spec) extends Generator(spec) { ...@@ -255,11 +255,11 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
override def generateInterface(origin: String, ident: Ident, doc: Doc, typeParams: Seq[TypeParam], i: Interface) { override def generateInterface(origin: String, ident: Ident, doc: Doc, typeParams: Seq[TypeParam], i: Interface) {
val refs = new CppRefs(ident.name) val refs = new CppRefs(ident.name)
i.methods.map(m => { i.methods.map(m => {
m.params.map(p => refs.find(p.ty)) m.params.map(p => refs.find(p.ty, true))
m.ret.foreach(refs.find) m.ret.foreach((x)=>refs.find(x, true))
}) })
i.consts.map(c => { i.consts.map(c => {
refs.find(c.ty) refs.find(c.ty, true)
}) })
val self = marshal.typename(ident, i) val self = marshal.typename(ident, i)
......
...@@ -32,7 +32,7 @@ class CppMarshal(spec: Spec) extends Marshal(spec) { ...@@ -32,7 +32,7 @@ class CppMarshal(spec: Spec) extends Marshal(spec) {
override def toCpp(tm: MExpr, expr: String): String = throw new AssertionError("cpp to cpp conversion") override def toCpp(tm: MExpr, expr: String): String = throw new AssertionError("cpp to cpp conversion")
override def fromCpp(tm: MExpr, expr: String): String = throw new AssertionError("cpp to cpp conversion") override def fromCpp(tm: MExpr, expr: String): String = throw new AssertionError("cpp to cpp conversion")
def references(m: Meta, exclude: String): Seq[SymbolReference] = m match { def references(m: Meta, exclude: String, forwardDeclareOnly: Boolean): Seq[SymbolReference] = m match {
case p: MPrimitive => p.idlName match { case p: MPrimitive => p.idlName match {
case "i8" | "i16" | "i32" | "i64" => List(ImportRef("<cstdint>")) case "i8" | "i16" | "i32" | "i64" => List(ImportRef("<cstdint>"))
case _ => List() case _ => List()
...@@ -45,9 +45,23 @@ class CppMarshal(spec: Spec) extends Marshal(spec) { ...@@ -45,9 +45,23 @@ class CppMarshal(spec: Spec) extends Marshal(spec) {
case MSet => List(ImportRef("<unordered_set>")) case MSet => List(ImportRef("<unordered_set>"))
case MMap => List(ImportRef("<unordered_map>")) case MMap => List(ImportRef("<unordered_map>"))
case d: MDef => d.defType match { case d: MDef => d.defType match {
case DEnum | DRecord => case DRecord =>
if (d.name != exclude) { if (d.name != exclude) {
List(ImportRef(include(d.name))) if (forwardDeclareOnly) {
List(DeclRef(s"struct ${typename(d.name, d.body)};", Some(spec.cppNamespace)))
} else {
List(ImportRef(include(d.name)))
}
} else {
List()
}
case DEnum =>
if (d.name != exclude) {
if (forwardDeclareOnly) {
List(DeclRef(s"enum class ${typename(d.name, d.body)};", Some(spec.cppNamespace)))
} else {
List(ImportRef(include(d.name)))
}
} else { } else {
List() List()
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#pragma once #pragma once
#include "client_returned_record.hpp"
#include <cstdint> #include <cstdint>
#include <experimental/optional> #include <experimental/optional>
#include <string> #include <string>
...@@ -11,6 +10,8 @@ ...@@ -11,6 +10,8 @@
namespace testsuite { namespace testsuite {
struct ClientReturnedRecord;
/** Client interface */ /** Client interface */
class ClientInterface { class ClientInterface {
public: public:
......
...@@ -3,10 +3,11 @@ ...@@ -3,10 +3,11 @@
#pragma once #pragma once
#include "extern_record_with_derivings.hpp"
#include "test_helpers.hpp" #include "test_helpers.hpp"
#include <memory> #include <memory>
struct ExternRecordWithDerivings;
class ExternInterface2 { class ExternInterface2 {
public: public:
virtual ~ExternInterface2() {} virtual ~ExternInterface2() {}
......
...@@ -3,12 +3,6 @@ ...@@ -3,12 +3,6 @@
#pragma once #pragma once
#include "assorted_primitives.hpp"
#include "color.hpp"
#include "map_list_record.hpp"
#include "nested_collection.hpp"
#include "primitive_list.hpp"
#include "set_record.hpp"
#include <cstdint> #include <cstdint>
#include <experimental/optional> #include <experimental/optional>
#include <memory> #include <memory>
...@@ -20,6 +14,12 @@ namespace testsuite { ...@@ -20,6 +14,12 @@ namespace testsuite {
class ClientInterface; class ClientInterface;
class UserToken; class UserToken;
enum class color;
struct AssortedPrimitives;
struct MapListRecord;
struct NestedCollection;
struct PrimitiveList;
struct SetRecord;
/** /**
* Helper methods used by various different tests. * Helper methods used by various different tests.
......
...@@ -2,6 +2,12 @@ ...@@ -2,6 +2,12 @@
#include "client_returned_record.hpp" #include "client_returned_record.hpp"
#include "client_interface.hpp" #include "client_interface.hpp"
#include "user_token.hpp" #include "user_token.hpp"
#include "assorted_primitives.hpp"
#include "color.hpp"
#include "map_list_record.hpp"
#include "nested_collection.hpp"
#include "primitive_list.hpp"
#include "set_record.hpp"
#include <exception> #include <exception>
namespace testsuite { namespace testsuite {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment