Skip to content

Commit

Permalink
implement python nested structures (#105)
Browse files Browse the repository at this point in the history
Implements the nested structure example for the Python wrapper. This includes
factoring some language-specific functionality out of the ClassSpec class.

This update also includes some dependency updates.
  • Loading branch information
goatshriek authored Jul 25, 2024
1 parent 26c907a commit 25867f1
Show file tree
Hide file tree
Showing 24 changed files with 320 additions and 177 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
build/*
coverage/*
docs/html/*
Gemfile.lock
*.gem

# ignore ctags files
Expand Down
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ source 'https://rubygems.org'
gemspec

group :development do
gem 'bundler', '>= 1.6.4', '< 2.5'
gem 'bundler', '>= 1.6.4'
gem 'rake', '>= 0.9.2'
gem 'rdoc', '>= 6.6.3.1' # minimum to address CVE-2024-27281
end

group :test do
# minitest at or above 5.12 cause problems with rbx 4
gem 'minitest', '>= 5.9', '< 5.12'
gem 'minitest', '>= 5.9'
gem 'rexml', '>= 3.3.2' # included explicitly to address CVE-2024-39908
gem 'rubocop', '>= 0.69', require: false
gem 'rubocop-minitest', require: false
gem 'rubocop-rake', require: false
Expand Down
82 changes: 0 additions & 82 deletions Gemfile.lock

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Wrapture
[![Github Actions Build Status](https://github.com/goatshriek/wrapture/workflows/build/badge.svg)](https://github.com/goatshriek/wrapture/actions?query=workflow%3Abuild)
[![Github Actions Build Status](https://github.com/goatshriek/wrapture/actions/workflows/build.yml/badge.svg)](https://github.com/goatshriek/wrapture/actions?query=workflow%3Abuild)
[![Coverage Report](https://codecov.io/gh/goatshriek/wrapture/branch/latest/graph/badge.svg)](https://codecov.io/gh/goatshriek/wrapture)
[![SonarCloud Status](https://sonarcloud.io/api/project_badges/measure?project=goatshriek_wrapture&metric=alert_status)](https://sonarcloud.io/dashboard?id=goatshriek_wrapture)
[![Apache 2.0 License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/inheritance/mylib_usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ using namespace std;
using namespace library;

int main( int argc, char **argv ) {
Item movie("Space Mutiny");
Book harry_potter("Harry Potter and the Chamber of Commerce", false, 500);
Item tablet("Tax Educator 2002");
Book lord_of_the_rings("The Two Showers", false, 1000);
Item movie( "Space Mutiny" );
Book harry_potter( "Harry Potter and the Chamber of Commerce", false, 500 );
Item tablet( "Tax Educator 2002" );
Book lord_of_the_rings( "The Two Showers", false, 1000 );

movie.CheckOut();
int pc = harry_potter.GetPageCount();
Expand Down
61 changes: 58 additions & 3 deletions docs/examples/nested_structs/fridge.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <fridge.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

struct fridge *
new_fridge( int temperature ){
Expand All @@ -11,7 +12,7 @@ new_fridge( int temperature ){
return NULL;
}

fridge->temp = 42;
fridge->temp = temperature;
fridge->ice = NULL;
fridge->filter = NULL;
fridge->freezer = NULL;
Expand Down Expand Up @@ -64,7 +65,7 @@ new_freezer( int minimum_temp, int set_temp ){
}

void
add_ice_maker_to_fridge( struct fridge *fridge, struct ice_maker *maker ){
add_ice_maker_to_fridge( struct fridge *fridge, struct ice_maker *ice ){
if( !fridge ){
return;
}
Expand All @@ -73,7 +74,7 @@ add_ice_maker_to_fridge( struct fridge *fridge, struct ice_maker *maker ){
free( fridge->ice );
}

fridge->ice = maker;
fridge->ice = ice;
}

void
Expand Down Expand Up @@ -102,3 +103,57 @@ add_freezer_to_fridge( struct fridge *fridge, struct freezer *freezer ){

fridge->freezer = freezer;
}

void
print_freezer( struct freezer *freezer ){
if( !freezer ){
puts( "no freezer" );
return;
}

printf( "minimum temperature: %d\n", freezer->minimum_temp );
printf( "set temperature: %d\n", freezer->set_temp );
}

void
print_fridge( struct fridge *fridge ){
if( !fridge ){
return;
}
printf( "temperature: %d\n", fridge->temp );

puts( "ice maker:" );
print_ice_maker( fridge->ice );

puts( "filter:" );
print_water_filter( fridge->filter );

puts( "freezer:" );
print_freezer( fridge->freezer );
}

void
print_ice_maker( struct ice_maker *ice ){
if( !ice ){
puts( "no ice maker" );
return;
}

printf( "size: %d\n", ice->size );

if( ice->can_crush_ice ){
puts( "can crush ice" );
} else {
puts( "cannot crush ice" );
}
}

void
print_water_filter( struct water_filter *filter ){
if( !filter ){
puts( "no water filter" );
return;
}

printf( "purity level: %d\n", filter->purity_level );
}
22 changes: 21 additions & 1 deletion docs/examples/nested_structs/fridge.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef __FRIDGE_H
#define __FRIDGE_H

#ifdef __cplusplus
extern "C" {
#endif

struct ice_maker {
int size;
int can_crush_ice; // boolean value
Expand Down Expand Up @@ -35,7 +39,7 @@ struct freezer *
new_freezer( int minimum_temp, int set_temp );

void
add_ice_maker_to_fridge( struct fridge *fridge, struct ice_maker *maker );
add_ice_maker_to_fridge( struct fridge *fridge, struct ice_maker *ice );

void
add_water_filter_to_fridge( struct fridge *fridge,
Expand All @@ -44,4 +48,20 @@ add_water_filter_to_fridge( struct fridge *fridge,
void
add_freezer_to_fridge( struct fridge *fridge, struct freezer *freezer );

void
print_freezer( struct freezer *freezer );

void
print_fridge( struct fridge *fridge );

void
print_ice_maker( struct ice_maker *ice );

void
print_water_filter( struct water_filter *filter );

#ifdef __cplusplus
}
#endif

#endif
12 changes: 9 additions & 3 deletions docs/examples/nested_structs/fridge.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
classes:
- name: "IceMaker"
namespace: "kitchen"
libraries: "fridge"
includes: "fridge.h"
equivalent-struct:
name: "ice_maker"
Expand All @@ -16,6 +17,7 @@ classes:
type: "equivalent-struct-pointer"
- name: "WaterFilter"
namespace: "kitchen"
libraries: "fridge"
includes: "fridge.h"
equivalent-struct:
name: "water_filter"
Expand All @@ -29,6 +31,7 @@ classes:
type: "equivalent-struct-pointer"
- name: "Freezer"
namespace: "kitchen"
libraries: "fridge"
includes: "fridge.h"
equivalent-struct:
name: "freezer"
Expand All @@ -44,11 +47,9 @@ classes:
type: "equivalent-struct-pointer"
- name: "Fridge"
namespace: "kitchen"
libraries: "fridge"
includes:
- "fridge.h"
- "Freezer.hpp"
- "IceMaker.hpp"
- "WaterFilter.hpp"
equivalent-struct:
name: "fridge"
constructors:
Expand Down Expand Up @@ -90,3 +91,8 @@ classes:
- name: "equivalent-struct-pointer"
- name: "new_freezer"
type: "struct freezer *"
- name: "Print"
wrapped-function:
name: "print_fridge"
params:
- name: "equivalent-struct-pointer"
37 changes: 37 additions & 0 deletions docs/examples/nested_structs/fridge_usage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: Apache-2.0

/*
* Copyright 2024 Joel E. Anderson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <Freezer.hpp>
#include <Fridge.hpp>
#include <IceMaker.hpp>
#include <WaterFilter.hpp>

using namespace kitchen;

int main( int argc, char **argv ){
Fridge kitchen_fridge( 34 );
WaterFilter filter( 10 );
IceMaker ice_maker( 10, 1 );
Freezer freezer( -10, 4 );

kitchen_fridge.AddFreezer( freezer );
kitchen_fridge.AddIceMaker( ice_maker );
kitchen_fridge.AddWaterFilter( filter );

kitchen_fridge.Print();
}
30 changes: 30 additions & 0 deletions docs/examples/nested_structs/fridge_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python

# SPDX-License-Identifier: Apache-2.0

# Copyright 2024 Joel E. Anderson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from kitchen import Freezer, Fridge, IceMaker, WaterFilter

kitchen_fridge = Fridge( 34 )
filter = WaterFilter( 10 )
ice_maker = IceMaker( 10, 1 )
freezer = Freezer( -10, 4 )

kitchen_fridge.AddFreezer( freezer )
kitchen_fridge.AddIceMaker( ice_maker )
kitchen_fridge.AddWaterFilter( filter )

kitchen_fridge.Print()
Loading

0 comments on commit 25867f1

Please sign in to comment.