Skip to content

Commit

Permalink
specs for aliased containers
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Nov 14, 2020
1 parent f367109 commit dd1f7f6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
33 changes: 33 additions & 0 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,39 @@ class Point
end
```

### §2.6 Generic types

Only sequential container types are instantiated on the Crystal side, as if each
container implements the following interface:

```crystal
module Container(T)
include Indexable(T)
# All containers must be default-constructible
# abstract def initialize
abstract def unsafe_fetch(index : Int) : T
abstract def push(value : T) : Void
abstract def size : Int32
def <<(x : T)
push(x)
self
end
def concat(values : Enumerable(T))
values.each { |v| self << v }
self
end
end
```

Bindgen automatically collects all instantiations of each container type that
appear in method argument types or return types; explicit instantiations may be
configured with the `containers` section. Aliases to complete container types
and container type arguments are both supported.

## §3. Crystal bindings

### §3.1 Naming scheme
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/containers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <vector>
#include <string>

typedef std::vector<unsigned char> bytearray;
typedef unsigned int rgb;

class Containers {
public:
std::vector<int> integers() {
Expand All @@ -15,6 +18,14 @@ class Containers {
return std::vector<std::string>{ "One", "Two", "Three" };
}

bytearray chars() {
return { 0x01, 0x04, 0x09 };
}

std::vector<rgb> palette() {
return { 0xFF0000, 0x00FF00, 0x0000FF };
}

double sum(std::vector<double> list) {
double d = 0;

Expand Down
7 changes: 5 additions & 2 deletions spec/integration/containers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

processors:
- filter_methods
- auto_container_instantiation
- instantiate_containers
- default_constructor
- cpp_wrapper
Expand All @@ -17,6 +18,8 @@ containers:
type: Sequential
instantiations:
- [ "int" ]
- [ "double" ]
- [ "std::string" ]
- [ "std::vector<int>" ]

types:
rgb: { alias_for: "unsigned int" }
bytearray: { alias_for: std::vector<unsigned char> }
8 changes: 8 additions & 0 deletions spec/integration/containers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe "container instantiation feature" do
Test::Containers.new.sum(list).should eq(4.0)
end

it "works with auto instantiated container (aliased container)" do
Test::Containers.new.chars.to_a.should eq([1u8, 4u8, 9u8])
end

it "works with auto instantiated container (aliased element)" do
Test::Containers.new.palette.to_a.should eq([0xFF0000u32, 0x00FF00u32, 0x0000FFu32])
end

it "works with nested containers" do
Test::Containers.new.grid.to_a.map(&.to_a).should eq([[1, 4], [9, 16]])
end
Expand Down

0 comments on commit dd1f7f6

Please sign in to comment.