Skip to content

Commit

Permalink
[12262] Replace existing G3D files (.cpp/.h), with 8.01 distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
blueboy authored and Schmoozerd committed Nov 17, 2012
1 parent 3b4c1f6 commit 17bcfc3
Show file tree
Hide file tree
Showing 80 changed files with 3,842 additions and 1,504 deletions.
3 changes: 3 additions & 0 deletions dep/include/g3dlite/G3D/AABox.h
Expand Up @@ -20,6 +20,7 @@
#include "G3D/debug.h"
#include "G3D/Array.h"
#include "G3D/Plane.h"
#include "G3D/Sphere.h"

namespace G3D {

Expand Down Expand Up @@ -259,6 +260,8 @@ class AABox {
void getBounds(AABox& out) const {
out = *this;
}

void getBounds(Sphere& out) const;
};

}
Expand Down
228 changes: 209 additions & 19 deletions dep/include/g3dlite/G3D/Any.h
Expand Up @@ -5,7 +5,7 @@
@maintainer Morgan McGuire
@created 2006-06-11
@edited 2009-12-16
@edited 2010-03-16
Copyright 2000-2010, Morgan McGuire.
All rights reserved.
Expand All @@ -17,7 +17,9 @@
#include "G3D/platform.h"
#include "G3D/Table.h"
#include "G3D/Array.h"
#include "G3D/Set.h"
#include "G3D/AtomicInt32.h"
#include "G3D/stringutils.h"
#include <string>

// needed for Token
Expand All @@ -34,11 +36,14 @@ class TextOutput;
/**
\brief Easy loading and saving of human-readable configuration files.
Encodes typed, structured data and can serialize it to a human
Any encodes typed, structured data and can serialize it to a human
readable format that is very similar to the Python language's data
syntax. Well-suited for quickly creating human-readable file formats,
especially since deserialization and serialization preserve comments and
an Any can tell you what file and line it came from.
syntax. It is well-suited for quickly creating human-readable file
formats, especially since deserialization and serialization preserve
comments and an Any can tell you what file and line it came from. The
syntax allows most C++ editors to properly highlight Any files, and
makes it easy to design little ad-hoc C-like languages in
configuration files.
The class is designed so that copying Anys generally is fast, even if
it is a large array or table. This is because data is shared between
Expand All @@ -50,15 +55,17 @@ Sample File:
{
shape = "round",
# in meters
// in meters
radius = 3.7,
position = Vector3(1.0, -1.0, 0.0),
texture = { format = "RGB8", size = (320, 200)}
video = { format = "RGB8", size = (320, 200)},
material = #include("rocks.mat")
}
</pre>
Sample code using:
Sample code using Any:
<pre>
Any x;
x.load("ball.txt");
Expand Down Expand Up @@ -110,6 +117,21 @@ Vector3::Vector3(const Any& any) {
}
</pre>
It is often convenient to iterate through the table portion:
<pre>
for (Any::AnyTable::Iterator it = any.table().begin(); it.hasMore(); ++it) {
const std::string& k = toLower(it->key);
if (key == "hello") {
...
} else if (key == "goodbye") {
...
} else {
any.verify(false, "Unsupported key: " + it->key);
}
}
</pre>
\section BNF
Serialized format BNF:
Expand All @@ -119,27 +141,38 @@ identifier-op ::= "::" | "->" | "."
identifier-exp ::= [identifier-op] identifier (identifier-op identifier)*
comment ::= "#" <any characters> "\n"
comment ::= C++ single or multi-line comments
separator ::= "," | ";"
number ::= <legal C printf number format>
string ::= <legal C double-quoted string; backslashes must be escaped>
boolean ::= "True" | "False"
none ::= "None"
array ::= "(" [value ("," value)*] ")"
array ::= ("(" | "[") [ value (separator value)* [separator] ] (")" | "]")
pair ::= (identifier | string) "=" value
table ::= "{" [pair (separator pair)*] "}"
named-array ::= identifier-exp tuple
named-table ::= identifier-exp dict
table ::= "{" [ pair (separator pair)* [separator] ] "}"
named-array ::= identifier-exp array
named-table ::= identifier-exp table
include ::= "#" "include" "(" string ")"
value ::= [comment] (none | number | boolean | string | array | table | named-array | named-table)
value ::= [comment] (none | number | boolean | string | array | table | named-array | named-table | include)
</pre>
Except for single-line comments, whitespace is not significant.
All parsing is case-insensitive.
The include expression pastes the contents of the named file in as if
they appeared in the original source. Note that an include expression
can only appear in the locations where a value is expected. This means
that it cannot yield more than one element of an array and cannot serve
as the pair in a table.
The deserializer allows the substitution of [] for () when writing
tuples and ";" for ",".
tuples and ";" for ",". These are convenient when mimicing a
programming language, e.g., <code>"[ printf("hello world."); clearScreen();]"</code>
parses as an array containing two named arrays within it. The
deserializer also allows a trailing comma inside any array or table,
which also convenient when commenting out the last element.
The serializer indents four spaces for each level of nesting.
Tables are written with the keys in alphabetic order.
Expand Down Expand Up @@ -407,9 +440,31 @@ class Any {
const std::string& string() const;
bool boolean() const;

/** If a valid string, takes the string value and creates a fully qualified filename.
If not found, the returned string is empty.
The file is searched for the following ways:
- In the directory from which the Any was loaded.
- By calling System::findDataFile as you would with other data files.
*/
std::string resolveStringAsFilename() const;

/** If this is named ARRAY or TABLE, returns the name. */
const std::string& name() const;

/** If this is named ARRAY or TABLE, returns true if the name begins with \a s. The comparision is case insensitive. */
bool nameBeginsWith(const std::string& s) const;

/** If this is named ARRAY or TABLE, returns true if the name begins with \a s. The comparision is case insensitive. */
bool nameBeginsWith(const char* s) const;

/** If this is named ARRAY or TABLE, returns true if the name is \a s. The comparision is case insensitive. */
bool nameEquals(const std::string& s) const;

/** If this is named ARRAY or TABLE, returns true if the name is\a s. The comparision is case insensitive. */
bool nameEquals(const char* s) const;

/** \brief Set the name used when serializing an ARRAY or TABLE.
Only legal for ARRAY or TABLE. The \a name must begin with a letter
Expand Down Expand Up @@ -439,6 +494,14 @@ class Any {
const Any& operator[](int i) const;
Any& operator[](int i);

const Any& last() const {
return (*this)[size() - 1];
}

Any& last() {
return (*this)[size() - 1];
}

/** Directly exposes the underlying data structure for an ARRAY. */
const Array<Any>& array() const;
void append(const Any& v0);
Expand All @@ -456,7 +519,7 @@ class Any {

// Needed to prevent the operator[](int) overload from catching
// string literals
inline const Any& operator[](const char* key) const {
const Any& operator[](const char* key) const {
return operator[](std::string(key));
}

Expand Down Expand Up @@ -486,7 +549,7 @@ class Any {
Any& operator[](const std::string& key);

/** \copydoc Any::operator[](const std::string&) */
inline Any& operator[](const char* key) {
Any& operator[](const char* key) {
return operator[](std::string(key));
}

Expand All @@ -503,6 +566,10 @@ class Any {
/** for an ARRAY, resizes and returns the last element */
Any& next();

/** The parent directory of the location from which this Any was loaded. This is useful for
interpreting filenames relative to the Any's source location,
which may not match the current directory if the Any was from an included file. */
std::string sourceDirectory() const;

/** True if the Anys are exactly equal, ignoring comments. Applies deeply on arrays and tables. */
bool operator==(const Any& x) const;
Expand Down Expand Up @@ -542,10 +609,15 @@ class Any {
*/
void verify(bool value, const std::string& message = "") const;

/** Verifies that the name begins with identifier \a n. It may contain
identifier operators after this */

/** Verifies that the name <i>begins with</i> identifier \a n (case insensitive).
It may contain identifier operators after this */
void verifyName(const std::string& n) const;

/** Verifies that the name <i>begins with</i> identifier \a n or \a m (case insensitive).
It may contain identifier operators after this */
void verifyName(const std::string& n, const std::string& m) const;

/** Verifies that the type is \a t. */
void verifyType(Type t) const;

Expand All @@ -565,6 +637,124 @@ class Any {

}; // class Any


/**
Convenient iteration over the keys of a Any::TABLE, usually
for implementing construction of an object from an Any.
Getting an element using either iteration or explicit requests
consumes that element from the iterator (but not from the Any!)
It is an error to consume the same element more than once from
the same iterator.
<pre>
AnyKeyIterator r(a);
r.getIfPresent("enabled", enabled);
r.getIfPresent("showSamples", showSamples);
r.getIfPresent("showTiles", showTiles);
r.verifyDone();
</pre>
\beta
*/
class AnyTableReader {
private:
Any m_any;
Set<std::string> m_alreadyRead;
public:

/** Verifies that \a is a TABLE with the given \a name. */
AnyTableReader(const std::string& name, const Any& a) : m_any(a) {
try {
m_any.verifyType(Any::TABLE);
m_any.verifyName(name);
} catch (const ParseError& e) {
// If an exception is thrown, the destructors will not be
// invoked automatically.
m_any.~Any();
m_alreadyRead.~Set();
throw e;
}
}

/** Verifies that \a is a TABLE. */
AnyTableReader(const Any& a) : m_any(a) {
try {
m_any.verifyType(Any::TABLE);
} catch (const ParseError& e) {
// If an exception is thrown, the destructors will not be
// invoked automatically.
m_any.~Any();
m_alreadyRead.~Set();
throw e;
}
}

bool hasMore() const {
return m_any.size() > m_alreadyRead.size();
}

/** Verifies that all keys have been read. */
void verifyDone() const {
if (hasMore()) {
// Generate all keys
// Remove the ones we've read
// Assert the rest
// any.verify("");
}
}

#if 0
/** Returns the current key */
const std::string& key() const;

/** Returns the current value */
const Any& value() const;

AnyKeyIterator& operator++();
#endif

/** If key \s appears in the any, reads its value into \a v and
removes that key from the ones available to iterate over.
If key \s does not appear in the any, throws a G3D::ParseError.
Assumes that if key \s appears in the any it has not already been extracted
by this iterator. If it has been read before, an assertion will fail in debug mode.
*/
template<class ValueType>
void get(const std::string& s, ValueType& v) {
v = m_any[s];
m_alreadyRead.insert(toLower(s));
}

/** Get the value associated with a key only if the key is actually present.
If key \s appears in the any, reads its value into \a v and
removes that key from the ones available to iterate over.
If key \s does not appear in the any, does nothing.
Assumes that if key \s appears in the any it has not already been extracted
by this iterator. If it has been read before, an assertion will fail in debug mode.
\return True if the value was read.
*/
template<class ValueType>
bool getIfPresent(const std::string& s, ValueType& v) {
if (m_any.containsKey(s)) {
debugAssertM(! m_alreadyRead.contains(toLower(s)), "read twice");

get(s, v);
return true;
} else {
return false;
}
}
};

} // namespace G3D

#endif
9 changes: 6 additions & 3 deletions dep/include/g3dlite/G3D/BinaryInput.h
@@ -1,12 +1,12 @@
/**
@file BinaryInput.h
@maintainer Morgan McGuire, graphics3d.com
@maintainer Morgan McGuire, http://graphics.cs.williams.edu
@created 2001-08-09
@edited 2006-07-19
@edited 2010-03-19
Copyright 2000-2009, Morgan McGuire.
Copyright 2000-2010, Morgan McGuire.
All rights reserved.
*/

Expand Down Expand Up @@ -370,6 +370,9 @@ class BinaryInput {
*/
std::string readString();

/** Reads until \r, \r\n, \n\r, \n or the end of the file is encountered. Consumes the newline.*/
std::string readStringNewline();

/**
Reads until NULL or the end of the file is encountered.
If the string has odd length (including NULL), reads
Expand Down

0 comments on commit 17bcfc3

Please sign in to comment.