Skip to content

Commit

Permalink
add turret example
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Oct 2, 2024
1 parent a715933 commit 25460e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
8 changes: 4 additions & 4 deletions docs/examples/exceptions/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Overloaded Struct Example
# Exceptions Example

Error handling in C is typically handled by simple techniques like checking a
return code or checking some other global variable. However, most higher level
Expand All @@ -7,9 +7,9 @@ languages have better way of handling errors, typically by throwing exceptions.
C structs do not support native inheritance, so it is a common pattern for them
to be differentiated from one another using something like a type code or
enumeration. However, in an object oriented language the more common pattern is
to create a parent class and inherit from it. This is most obvious in the
exception classes of most languages, where different kinds of errors are
different classes derived from the main exception or error classes.
to create a parent class and inherit from it. This is common in the exception
classes of most languages, where different kinds of errors are different
classes derived from the main exception or error classes.

Wrapture provides a way to distinguish between different types of a struct so
that it will be translated to the correct class. This example demonstrates the
Expand Down
8 changes: 8 additions & 0 deletions docs/examples/exceptions/turret.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

#include <turret_error.h>

#ifdef __cplusplus
extern "C" {
#endif

struct turret {
int ammo_count;
int x;
Expand All @@ -46,4 +50,8 @@ new_turret( void );
struct turret_error *
reload( struct turret *t );

#ifdef __cplusplus
}
#endif

#endif
8 changes: 8 additions & 0 deletions docs/examples/exceptions/turret_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#define OUT_OF_AMMO 2
#define TARGETING_ERROR 3

#ifdef __cplusplus
extern "C" {
#endif

struct turret_error {
int code;
const char *message;
Expand Down Expand Up @@ -53,4 +57,8 @@ success( void );
struct turret_error *
targeting_error( void );

#ifdef __cplusplus
}
#endif

#endif
36 changes: 20 additions & 16 deletions rakelib/examples.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

def run_cpp_example(name, lib, source, build_dir)
def run_cpp_example(name, lib, sources, build_dir)
example_dir = File.absolute_path("docs/examples/#{name}")

scope = Wrapture::Scope.load_files("#{example_dir}/#{lib}.yml")
Expand All @@ -27,9 +27,10 @@ def run_cpp_example(name, lib, source, build_dir)
Dir.chdir(build_dir) do
usage_opts = "-I. -I#{example_dir} -o #{lib}_usage_cpp"

if source
if sources
source_opts = "-shared -o lib#{lib}.so -fPIC -I#{example_dir}"
sh "gcc #{example_dir}/#{source} #{source_opts}"
source_files = sources.map { |s| "#{example_dir}/#{s}" }.join(' ')
sh "gcc #{source_files} #{source_opts}"
usage_opts += " -L. -l#{scope.name} -l#{lib}"

include_cmd = "include_directories(\".\" \"#{example_dir}\")"
Expand All @@ -43,7 +44,7 @@ def run_cpp_example(name, lib, source, build_dir)
end
end

def run_python_example(name, lib, source, build_dir)
def run_python_example(name, lib, sources, build_dir)
example_dir = File.absolute_path("docs/examples/#{name}")

scope = Wrapture::Scope.load_files("#{example_dir}/#{lib}.yml")
Expand All @@ -52,9 +53,10 @@ def run_python_example(name, lib, source, build_dir)
wrapper.write_setuptools_files(dir: build_dir)

Dir.chdir(build_dir) do
if source
if sources
source_opts = "-shared -o lib#{lib}.so -fPIC -I#{example_dir}"
sh "gcc #{example_dir}/#{source} #{source_opts}"
source_files = sources.map { |s| "#{example_dir}/#{s}" }.join(' ')
sh "gcc #{source_files} #{source_opts}"
end
setup_command = 'python3 setup.py build_ext'
sh "#{setup_command} --include-dirs #{example_dir} --build-lib ."
Expand All @@ -63,16 +65,18 @@ def run_python_example(name, lib, source, build_dir)
end
end

examples = [{ name: 'basic', lib: 'stove', source: 'stove.c' },
{ name: 'constants', lib: 'vcr', source: 'vcr.c' },
{ name: 'enumerations', lib: 'fruit', source: nil },
{ name: 'inheritance', lib: 'mylib', source: 'mylib.c' },
{ name: 'nested_structs', lib: 'fridge', source: 'fridge.c' },
examples = [{ name: 'basic', lib: 'stove', sources: ['stove.c'] },
{ name: 'constants', lib: 'vcr', sources: ['vcr.c'] },
{ name: 'enumerations', lib: 'fruit', sources: nil },
{ name: 'exceptions', lib: 'turret',
sources: ['turret.c', 'turret_error.c'] },
{ name: 'inheritance', lib: 'mylib', sources: ['mylib.c'] },
{ name: 'nested_structs', lib: 'fridge', sources: ['fridge.c'] },
{ name: 'overloaded_struct',
lib: 'security_system',
source: 'security_system.c' },
{ name: 'struct_wrapper', lib: 'stats', source: 'stats.c' },
{ name: 'templates', lib: 'magic_math', source: 'magic_math.c' }]
sources: ['security_system.c'] },
{ name: 'struct_wrapper', lib: 'stats', sources: ['stats.c'] },
{ name: 'templates', lib: 'magic_math', sources: ['magic_math.c'] }]

namespace 'examples' do
examples.each do |ex|
Expand All @@ -85,15 +89,15 @@ namespace 'examples' do

desc 'build and run basic example for C++'
task cpp: [cpp_build_dir] do
run_cpp_example(ex[:name], ex[:lib], ex[:source], cpp_build_dir)
run_cpp_example(ex[:name], ex[:lib], ex[:sources], cpp_build_dir)
end

python_build_dir = "#{build_root}/python"
directory python_build_dir

desc 'build and run basic example for python'
task python: [python_build_dir] do
run_python_example(ex[:name], ex[:lib], ex[:source], python_build_dir)
run_python_example(ex[:name], ex[:lib], ex[:sources], python_build_dir)
end
end
end
Expand Down

0 comments on commit 25460e5

Please sign in to comment.