Skip to content

Commit 828953f

Browse files
authored
Merge branch 'master' into instlib-delete-copy-move
2 parents 1ef6ff8 + 9e53861 commit 828953f

20 files changed

Lines changed: 455 additions & 165 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ is simple to incorporate into existing projects.
44

55
[![Build Status](https://travis-ci.org/devosoft/Empirical.svg?branch=master)](https://travis-ci.org/devosoft/Empirical) [![Documentation Status](https://readthedocs.org/projects/empirical/badge/?version=latest)](https://empirical.readthedocs.io/en/latest/?badge=latest)
66

7-
See the doc/QuickStartGuides/ folder to start using the library.
7+
See our [Quick Start Guides](https://empirical.readthedocs.io/en/latest/QuickStartGuides) to start using the library.
88

99
Tools in Empirical include:
1010
* Web-enabled elements to facilitate compiling to JavaScript and with a full web interfaces,
1111
using Mozilla's Emscripten compiler (from C++ to high-efficiency JavaScript).
1212
* Debug tools to facilitate audited memory management; these tools are turned off outside of
1313
debug mode allowing the full speed of raw pointers and normal standard library components.
1414
* A wide range of Helper tools to streamline common scientific computing tasks such as
15-
configuration, data mangement, mathematical manipulations, etc.
15+
configuration, data management, mathematical manipulations, etc.
1616
All of these are designed to be easy-to-use and efficient.
1717
* A powerful set of evolution tools for building Artificial Life or Evolutionary Computation
1818
software.

doc/QuickStartGuides/0-Overview

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Overview
2+
========
3+
4+
The goal of Empirical is to simplify the development of efficient, reliable, and accessible
5+
scientific software.
6+
7+
We have several quick-start guides for different portions of the code.
8+
9+
Getting started:
10+
- Using specific header files is simple, just :code:`#include` them into your own project.
11+
We recommend adding your :code:`Empirical/source/` folder to the include directories for your C++ compiler (usually with the :code:`-I` option).
12+
- To use the web features in Empirical, you must first install the emscripten compiler, which can be slightly more complicated.
13+
* Our `Hello World Guide`_ will help you get compiling with Empirical, natively and for web.
14+
15+
.. _`Hello World Guide`: 1-HelloWorld.html

doc/QuickStartGuides/1-BaseTools

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
Hello World
2+
===========
3+
4+
Empirical allows you to compile your C++ to target
5+
6+
- running at the command line (e.g., native machine code) and
7+
- running in the web browser (e.g., javascript assembly code).
8+
9+
This how-to aims to walk you through all the nitty gritty required to successfully compile a simple "Hello World" for both targets.
10+
11+
Install: Native C++ Compiler
12+
----------------------------
13+
14+
In the Unix-verse (e.g., Linux / MacOS) commonly used compilers include :code:`gcc` and :code:`clang`.
15+
From this point onwards, we assume that you're working with :code:`gcc`.
16+
Unless you really know what you're doing, you'll want to have :code:`gcc` installed.
17+
The good news is: you might already!
18+
19+
Bring up a terminal and try entering:
20+
21+
.. code-block:: bash
22+
23+
which gcc
24+
25+
If :code:`which` spits out a path, then you have :code:`gcc` installed!
26+
If :code:`which` says "not found," you'll need to go ahead and install :code:`gcc`.
27+
For Linux users, your package manager (e.g., :code:`yum`, :code:`apt`, etc.) is probably the way to go about this.
28+
For MacOS users, you'll need to get Apple's "Command Line Tools for Xcode."
29+
Either way, give it a quick web search (e.g., "install gcc on [my operating system]") and there should be plenty of how-to guides that walk you through step-by-step.
30+
31+
TODO
32+
Windows...
33+
Maybe you should try git for Windows (e.g., "GitBash")?
34+
35+
Compile & Run: Command Line
36+
---------------------------
37+
38+
Assuming you haven't already pulled down a clone of Empirical, let's get your working environment all set.
39+
40+
.. code-block:: bash
41+
42+
git clone https://github.com/devosoft/Empirical
43+
cd Empirical/examples/ProjectTemplate
44+
45+
Let's take a look at what we want to compile.
46+
47+
:code:`source/native/project_name.cc`:
48+
49+
.. code-block:: c++
50+
51+
// This is the main function for the NATIVE version of this project.
52+
53+
#include <iostream>
54+
55+
#include "base/vector.h"
56+
#include "config/command_line.h"
57+
58+
int main(int argc, char* argv[])
59+
{
60+
emp::vector<std::string> args = emp::cl::args_to_strings(argc, argv);
61+
62+
std::cout << "Hello World!" << std::endl;
63+
}
64+
65+
This part is where Empirical source is brought in.
66+
67+
.. code-block:: c++
68+
69+
#include "base/vector.h"
70+
#include "config/command_line.h"
71+
72+
The main function uses Empirical's vector and argument parsing tools to process command line options, but doesn't do anything with them.
73+
Then, we print "Hello World!".
74+
75+
Let's compile!
76+
77+
.. code-block:: bash
78+
79+
make
80+
81+
If you :code:`ls`, you should now see the executable :code:`project_name` has been created.
82+
Let's run!
83+
84+
.. code-block:: bash
85+
86+
./project_name
87+
88+
Install: Web C++ Compiler
89+
-------------------------
90+
91+
In order to compile for web, you'll need the `emscripten LLVM-to-Web Compiler`_.
92+
If you're a new user, you (probably) don't have this set up so we'll walk you through step-by-step.
93+
94+
.. _emscripten LLVM-to-Web Compiler: https://emscripten.org
95+
96+
We aim for Empirical to track the current release of emscripten.
97+
However, as of February 2019, we've fallen a bit behind due to some breaking changes in recent emscripten releases.
98+
(See, e.g., `Empirical Issue #197`_).
99+
While we're working on bringing Empirical up-to-date with the latest emscripten, you'll need to work with emscripten version :code:`v1.37.1` in order to compile Empirical's web tools.
100+
101+
.. _Empirical Issue #197: https://github.com/devosoft/Empirical/issues/197
102+
103+
Here's how to get that taken care of.
104+
105+
.. code-block:: bash
106+
107+
git clone https://github.com/emscripten-core/emsdk.git
108+
cd emsdk
109+
./emsdk install emscpripten v1.37.1
110+
./emsdk activate emscpripten v1.37.1
111+
./emsdk install clang-e1.37.1-64bit
112+
./emsdk activate clang-e1.37.1-64bit
113+
114+
When you want to use the emscripten compiler, you'll want to hop over to the :code:`emsdk` directory and run
115+
116+
.. code-block:: bash
117+
118+
source ./emsdk_env.sh
119+
120+
in order to load emscripten's odds and ends into your :code:`PATH`.
121+
You only need to do this once per terminal session (e.g., the first time you want to use emscripten in a terminal session).
122+
123+
Compile & Run: Web Browser
124+
--------------------------
125+
126+
Assuming your working directory is still :code:`Empirical/examples/ProjectTemplate` and you have loaded up emscripten (e.g., :code:`source source ./emsdk_env.sh`), compiling for web is a snap!
127+
128+
Let's take a look at what we want to compile first, though.
129+
130+
:code:`source/web/project_name-web.cc`:
131+
132+
.. code-block:: c++
133+
134+
// This file is part of Project Name
135+
// Copyright (C) Michigan State University, 2017.
136+
// Released under the MIT Software license; see doc/LICENSE
137+
138+
#include "web/web.h"
139+
140+
namespace UI = emp::web;
141+
142+
UI::Document doc("emp_base");
143+
144+
int main()
145+
{
146+
doc << "<h1>Hello, world!</h1>";
147+
}
148+
149+
The line
150+
151+
.. code-block:: c++
152+
153+
#include "web/web.h"
154+
155+
brings in Empirical's web tools, which provide a convenient interface for C++ code to interact with browser-y bits like html and Javascript.
156+
157+
The line
158+
159+
.. code-block:: c++
160+
161+
UI::Document doc("emp_base");
162+
163+
creates a persistent :code:`UI::Document` object (e.g., outside the scope of the :code:`main` function) that hooks into the :code:`"emp_base"` div in :code:`web/project_name.html`.
164+
165+
Then, in :code:`main`, we write our message to the :code:`"emp_base"` div (wrapped in some html markup formatting... e.g., :code:`<h1>` and :code:`</h1>`).
166+
167+
You can find a more comprehensive explanation of the contents of this `.cpp` file in our `Quick Start Guide for Web Tools`_.
168+
169+
.. _`Quick Start Guide for Web Tools`: 3-WebTools.html
170+
171+
Let's compile
172+
173+
.. code-block:: bash
174+
175+
make web
176+
177+
We should now have :code:`web/project_name.js` and :code:`web/project_name.js.mem` ready to go.
178+
You can verify this by entering :code:`ls web` at your command line.
179+
180+
We'll need to locally serve our working directory in order to view our compiled product in a web browser.
181+
Python provides a handy, no-hassle tool for this.
182+
183+
Try running
184+
185+
.. code-block:: bash
186+
187+
python3 -m http.server
188+
189+
at your command line.
190+
If it starts up, then great!
191+
Just leave it running for now.
192+
193+
If you don't have :code:`python3` installed, a step-by-step guide for your operating system is probably only a quick web search away.
194+
Alternatively, go ahead and use your web serving tool of choice.
195+
196+
Pop open your favorite browser and point the address bar to `http://localhost:8000/web/project_name.html`_.
197+
198+
.. _`http://localhost:8000/web/project_name.html`: http://localhost:8000/web/project_name.html
199+
200+
Voila!
201+
202+
You can end your web serving process by closing the terminal window you're working in or entering :code:`<ctrl>-c` a the command line.
203+
204+
Extending the Project Template
205+
------------------------------
206+
207+
We've used the project template to run some simple "hello world" code natively and in the browser.
208+
209+
If you're wondering how to extend the project template, we have a quick start guide on exactly that here_).
210+
211+
.. _here: 4-UsingProjectTemplate.html
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Base Tools
2+
==========
3+
4+
A handful of tools are available in the source/base/ folder in Empirical. These mimic basic
5+
functionality available in C++ or the standard library, but provide extra protection against
6+
common memory use errors and additional information to the developer.
7+
8+
base/assert.h
9+
---------------------
10+
11+
This file adds an :code:`emp_assert` macro that can handle all of the same functionality as the
12+
standard library assert, but with additional features. Specifically, additional arguments
13+
may be added that are printed when the assert is triggered. For example, the line
14+
15+
.. code-block:: C++
16+
17+
emp_assert(i < 10, i);
18+
19+
if triggered, would print something to the effect of
20+
21+
.. code-block:: bash
22+
23+
Assert Error (In assert.cc line 6): i < 10
24+
i: [1844674]
25+
Abort
26+
27+
Indicating that not only was :code:`i >= 10`, but its current value is 1844674.
28+
29+
If compiled as part of a web app (with emscripten) :code:`emp_assert`: will automatically use the
30+
JavaScript Alert function to indicate any assert failures. Asserts are all disabled (fully
31+
removed from compiled code) if compiled using the :code:`NDEBUG` option (for most compilers, this
32+
deactivation is accomplished by using the :code:`-DNDEBUG` flag at compile time.)
33+
34+
35+
base/array.h and base/vector.h
36+
----------------------------------
37+
38+
These files setup the :code:`emp::array<...>` and :code:`emp::vector<...>` template objects, which behave
39+
almost identically to :code:`std::array<...>` and :code:`std::vector<...>`, respectively. The one difference
40+
is that they do bounds checking when they are indexed into or specific size matters. As with
41+
asserts, these additional bounds checks are removed when compiled with the :code:`NDEBUG` option.
42+
43+
44+
base/Ptr.h
45+
-------------
46+
47+
The :code:`emp::Ptr<...>` template provides an alternate method of building pointers, but with the
48+
ability to turn on additional debugging facilities; unlike assert, array, and vector, no
49+
debugging is performed by default due to substantial run-time costs. For example declaring a
50+
variable as :code:`emp::Ptr<int>` is the same as declaring it :code:`int *`.
51+
52+
If the :code:`EMP_TRACK_MEM` option is set (:code:`-DEMP_TRACK_MEM` compiler flag) then all pointer usage is
53+
tracked and many simple memory errors will be identified during execution, such as using or
54+
deleting an unallocated pointer.
55+
56+
Most usage of the :code:`Ptr` class is identical to raw pointers, including all operator.
57+
Differences include:
58+
59+
* Rather than using :code:`new TYPE` to allocate a new pointer, use :code:`emp::NewPtr<TYPE>`
60+
* If allocating an array, use :code`:emp::NewArrayPtr<TYPE>(SIZE)`
61+
* To delete use the :code:`.Delete()` or :code:`.DeleteArray()` member function (not :code:`delete` or :code:`delete[]`).
62+
* To cast one :code:`Ptr` type to another, use the :code:`.Cast<TYPE>` member function.
63+
* To dynamic cast (double-checking types and returning :code:`nullptr` on failure), use :code:`.DynamicCast<TYPE>`
64+
65+
66+
To convert a :code:`Ptr`-allocated pointer to the raw form, use the :code:`Raw()` member function. Make sure
67+
that any pointer allocated as a :code:`Ptr` type is also freed as a :code:`Ptr` type.

0 commit comments

Comments
 (0)