Skip to content

Commit

Permalink
minor modifications to the headers to make them easier to parse and d…
Browse files Browse the repository at this point in the history
…oxygen crap
  • Loading branch information
slembcke committed Nov 19, 2011
1 parent e932d24 commit f27c5e1
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 204 deletions.
273 changes: 163 additions & 110 deletions doxygen_generator.rb
@@ -1,125 +1,178 @@
DOC_FILE = open("generated_docs", "w")

def output(str='')
DOC_FILE.puts str
end

def find_declarations(name)
IO.readlines("|find include -name \"*.h\" | xargs grep -h \"^#{name}.*;\\s*$\"")
end

def output_getter(struct_base, struct, type, name)
output "/// @fn static inline #{type} #{struct}Get#{name}(const #{struct_base} *#{struct_base[2..-1].downcase});"
output "/// Get the #{name} of a #{struct}."
output
end

def output_setter(struct_base, struct, type, name)
output "/// @fn static inline void #{struct}Set#{name}(#{struct_base} *#{struct_base[2..-1].downcase}, #{type} value);"
output "/// Set the #{name} of a #{struct}."
output
end

def struct_parser(decl)
decl.match(/\((.*?), (.*?), (.*?)(, .*)?\)/).captures.values_at(0, 2)
end

def search_struct(struct)
upname = struct[2..-1].capitalize
find_declarations("CP_Define#{upname}StructProperty").each do|decl|
type, name = *struct_parser(decl)
output_getter(struct, struct, type, name)
output_setter(struct, struct, type, name)
end
FUNCS = {}
IO.readlines("|ruby extract_protos.rb").each{|line|
func = eval(line)
FUNCS[func[:name]] = func
}

SKIP = Object.new

GETTER_DOCS = {
"cpArbiterGetBodies" => SKIP,
"cpArbiterGetContactPointSet" => SKIP,
"cpArbiterGetCount" => SKIP,
"cpArbiterGetDepth" => SKIP,
"cpArbiterGetElasticity" => "Get the elasticity for this cpArbiter",
"cpArbiterGetFriction" => "Get the friction for this cpArbiter",
"cpArbiterGetNormal" => SKIP,
"cpArbiterGetPoint" => SKIP,
"cpArbiterGetShapes" => SKIP,
"cpArbiterGetSurfaceVelocity" => "Get the surface velocity used by the contact calculation for this cpArbiter",

find_declarations("CP_Define#{upname}StructGetter").each do|decl|
output_getter(struct, struct, *struct_parser(decl))
end
"cpBodyGetAngVel" => "Get the angular velocity of this cpBody",
"cpBodyGetAngVelLimit" => "Get the angular velocity limit of this cpBody",
"cpBodyGetAngle" => "Get the angle of this cpBody",
"cpBodyGetForce" => "Get the force applied to this cpBody",
"cpBodyGetMass" => "Get the mass of this cpBody",
"cpBodyGetMoment" => "Get the moment of inertia of this cpBody",
"cpBodyGetPos" => "Get the position of this cpBody",
"cpBodyGetRot" => "Get the rotation vector of this cpBody",
"cpBodyGetTorque" => "Get the torque applied to this cpBody",
"cpBodyGetUserData" => "Get the userdata pointer for this cpBody",
"cpBodyGetVel" => "Get the velocity of this cpBody",
"cpBodyGetVelLimit" => "Get the velocity limit of this cpBody",

find_declarations("CP_Define#{upname}StructSetter").each do|decl|
output_setter(struct, struct, *struct_parser(decl))
end
end

search_struct("cpBody")
search_struct("cpShape")
search_struct("cpConstraint")
search_struct("cpSpace")

def output_getter2(struct_base, struct, type, name)
output "/// @addtogroup #{struct}"
output "/// @{"
output "/// @fn static inline #{type} #{struct}Get#{name}(const #{struct_base} *#{struct_base[2..-1].downcase});"
output "/// @brief Get the #{name} of a #{struct}."
output "static inline #{type} #{struct}Get#{name}(const #{struct_base} *#{struct_base[2..-1].downcase});"
output "/// @}"
output
end

def output_setter2(struct_base, struct, type, name)
output "/// @addtogroup #{struct}"
output "/// @{"
output "/// @fn static inline void #{struct}Set#{name}(#{struct_base} *#{struct_base[2..-1].downcase}, #{type} value);"
output "/// @brief Set the #{name} of a #{struct}."
output "static inline void #{struct}Set#{name}(#{struct_base} *#{struct_base[2..-1].downcase}, #{type} value);"
output "/// @}"
output
end

def struct_parser2(decl)
decl.match(/\((.*?), (.*?), (.*?), (.*?)\)/).captures.values_at(0, 1, 3)
end

def search_struct2(struct_base)
upname = struct_base[2..-1].capitalize
find_declarations("CP_\\(Define\\|Declare\\)#{upname}Property").each do|decl|
struct, type, name = *struct_parser2(decl)
output_getter2(struct_base, struct, type, name)
output_setter2(struct_base, struct, type, name)
end
"cpCircleShapeGetOffset" => "Get the offset of this cpCircleShape",
"cpCircleShapeGetRadius" => "Get the radius of this cpCircleShape",

find_declarations("CP_Define#{upname}Getter").each do|decl|
output_getter2(struct_base, *struct_parser2(decl))
end
"cpConstraintGetA" => "Get the first of the two bodies this cpConstraint is connected to.",
"cpConstraintGetB" => "Get the second of the two bodies this cpConstraint is connected to.",
"cpConstraintGetErrorBias" => "Get the percentage of constraint error that remains unfixed after each second.",
"cpConstraintGetImpulse" => SKIP,
"cpConstraintGetMaxBias" => "Get the maximum rate this cpConstraint can apply to correct itself at.",
"cpConstraintGetMaxForce" => "Get the maximum force this cpConstraint can apply to correct itself.",
"cpConstraintGetPostSolveFunc" => "Get the function callback that is called each step after the solver runs.",
"cpConstraintGetPreSolveFunc" => "Get the function callback that is called each step before the solver runs.",
"cpConstraintGetUserData" => "Get the user data pointer for this cpConstraint.",

"cpDampedRotarySpringGetDamping" => "Get the damping of this cpDampedRotarySpring.",
"cpDampedRotarySpringGetRestAngle" => "Get the restangle of this cpDampedRotarySpring.",
"cpDampedRotarySpringGetSpringTorqueFunc" => "Get the springtorquefunc of this cpDampedRotarySpring.",
"cpDampedRotarySpringGetStiffness" => "Get the stiffness of this cpDampedRotarySpring.",

"cpDampedSpringGetAnchr1" => "Get the anchr1 of this cpDampedSpring.",
"cpDampedSpringGetAnchr2" => "Get the anchr2 of this cpDampedSpring.",
"cpDampedSpringGetDamping" => "Get the damping of this cpDampedSpring.",
"cpDampedSpringGetRestLength" => "Get the rest length of this cpDampedSpring.",
"cpDampedSpringGetSpringForceFunc" => "Get the spring force callback function of this cpDampedSpring.",
"cpDampedSpringGetStiffness" => "Get the stiffness of this cpDampedSpring.",

find_declarations("CP_Define#{upname}Setter").each do|decl|
output_setter2(struct_base, *struct_parser2(decl))
"cpGearJointGetPhase" => "Get the phase of this cpGearJoint.",
"cpGearJointGetRatio" => "Get the ratio of this cpGearJoint.",

"cpGrooveJointGetAnchr2" => "Get the anchr2 of this cpGrooveJoint.",
"cpGrooveJointGetGrooveA" => "Get the groovea of this cpGrooveJoint.",
"cpGrooveJointGetGrooveB" => "Get the grooveb of this cpGrooveJoint.",

"cpPinJointGetAnchr1" => "Get the anchr1 of this cpPinJoint.",
"cpPinJointGetAnchr2" => "Get the anchr2 of this cpPinJoint.",
"cpPinJointGetDist" => "Get the dist between the anchor points of this cpPinJoint.",

"cpPivotJointGetAnchr1" => "Get the anchr1 of this cpPivotJoint.",
"cpPivotJointGetAnchr2" => "Get the anchr2 of this cpPivotJoint.",

"cpPolyShapeGetNumVerts" => SKIP,
"cpPolyShapeGetVert" => SKIP,

"cpRatchetJointGetAngle" => "Get the angle of this cpRatchetJoint.",
"cpRatchetJointGetPhase" => "Get the phase of this cpRatchetJoint.",
"cpRatchetJointGetRatchet" => "Get the ratchet angular distance of this cpRatchetJoint.",

"cpRotaryLimitJointGetMax" => "Get the max delta angle of this cpRotaryLimitJoint.",
"cpRotaryLimitJointGetMin" => "Get the min delta angle of this cpRotaryLimitJoint.",

"cpSegmentShapeGetA" => "Get the first endpoint of this cpSegmentShape.",
"cpSegmentShapeGetB" => "Get the second endpoint of this cpSegmentShape.",
"cpSegmentShapeGetNormal" => "Get the normal of this cpSegmentShape.",
"cpSegmentShapeGetRadius" => "Get the radius of this cpSegmentShape.",

"cpShapeGetBB" => "Get the bounding box of this cpShape.",
"cpShapeGetBody" => "Get the body this cpShape is attached to.",
"cpShapeGetCollisionType" => "Get the collision type of this cpShape.",
"cpShapeGetElasticity" => "Get the elasticity of this cpShape.",
"cpShapeGetFriction" => "Get the friction of this cpShape.",
"cpShapeGetGroup" => "Get the group of this cpShape.",
"cpShapeGetLayers" => "Get the layer bitmask of this cpShape.",
"cpShapeGetSensor" => "Get the sensor flag of this cpShape.",
"cpShapeGetSurfaceVelocity" => "Get the surface velocity of this cpShape.",
"cpShapeGetUserData" => "Get the user data pointer of this cpShape.",

"cpSimpleMotorGetRate" => "Get the rate of this cpSimpleMotor.",

"cpSlideJointGetAnchr1" => "Get the anchr1 of this cpSlideJoint.",
"cpSlideJointGetAnchr2" => "Get the anchr2 of this cpSlideJoint.",
"cpSlideJointGetMax" => "Get the max distance between the anchors of this cpSlideJoint.",
"cpSlideJointGetMin" => "Get the min distance between the anchors of this cpSlideJoint.",

"cpSpaceGetCollisionBias" => "Get the collision bias of this cpSpace.",
"cpSpaceGetCollisionPersistence" => "Get the collision persistence of this cpSpace.",
"cpSpaceGetCollisionSlop" => "Get the collision slop of this cpSpace.",
"cpSpaceGetCurrentTimeStep" => "Get the most recent timestep used with this cpSpace.",
"cpSpaceGetDamping" => "Get the damping of this cpSpace.",
"cpSpaceGetEnableContactGraph" => "Get the enable contact graph flag of this cpSpace.",
"cpSpaceGetGravity" => "Get the gravity of this cpSpace.",
"cpSpaceGetIdleSpeedThreshold" => "Get the idle speed threshold of this cpSpace.",
"cpSpaceGetIterations" => "Get the number of solver iterations of this cpSpace.",
"cpSpaceGetSleepTimeThreshold" => "Get the sleep time threshold of this cpSpace.",
"cpSpaceGetStaticBody" => "Get the static body of this cpSpace.",
"cpSpaceGetUserData" => "Get the user data pointer of this cpSpace.",
}

def output_getter(func)
name = func[:name]

doc = GETTER_DOCS[name]
return if doc == SKIP

struct, property = */(cp\w*)Get(.+)/.match(name).captures

if doc
prototype = "#{func[:inline] ? "static inline " : ""}#{func[:return]} #{name}(#{func[:args]})"

DOC_FILE.puts <<-EOF
/// @addtogroup #{struct}
/// @{
/// @fn #{prototype};
/// @brief #{doc}
#{prototype};
/// @}
EOF
else
puts %{\t"#{name}" => "Get the #{property.downcase} of this #{struct}.",}
end
end

search_struct2("cpConstraint")

def output_getter2(struct_base, struct, type, name)
output "/// @fn #{type} #{struct}Get#{name}(const #{struct_base} *#{struct_base[2..-1].downcase});"
output "/// Get the #{name} of a #{struct}."
output
end

def output_setter2(struct_base, struct, type, name)
output "/// @fn void #{struct}Set#{name}(#{struct_base} *#{struct_base[2..-1].downcase}, #{type} value);"
output "/// Set the #{name} of a #{struct}."
output
end

def struct_parser3(decl)
decl.match(/\((.*?), (.*?), (.*?)\)/).captures
end

def search_struct3(struct_base)
upname = struct_base[2..-1].capitalize
find_declarations("CP_\\(Define\\|Declare\\)#{upname}Property").each do|decl|
struct, type, name = *struct_parser3(decl)
output_getter2(struct_base, struct, type, name)
output_setter2(struct_base, struct, type, name)
end
def output_setter(func)
name = func[:name]

find_declarations("CP_\\(Define\\|Declare\\)#{upname}Getter").each do|decl|
output_getter2(struct_base, *struct_parser3(decl))
end
doc = GETTER_DOCS[name.gsub("Set", "Get")]
return if doc == SKIP

find_declarations("CP_\\(Define\\|Declare\\)#{upname}Setter").each do|decl|
output_setter2(struct_base, *struct_parser3(decl))
struct, property = */(cp\w*)Set(.+)/.match(name).captures

if doc
prototype = "static inline void #{name}(#{func[:args]})"

DOC_FILE.puts <<-EOF
/// @addtogroup #{struct}
/// @{
/// @fn #{prototype};
/// @brief #{doc.gsub("Get", "Set")}
#{prototype};
/// @}"
EOF
else
puts %{\t"#{name}" => "Set the #{property.downcase} of this #{struct}.",}
end
end

search_struct3("cpShape")

getters = FUNCS.keys.find_all{|name| /(cp\w*)Get(.+)/.match(name)}.sort
FUNCS.values_at(*getters).each{|func| output_getter(func)}

setters = FUNCS.keys.find_all{|name| /(cp\w*)Set(.+)/.match(name)}.sort
FUNCS.values_at(*setters).each{|func| output_setter(func)}
37 changes: 17 additions & 20 deletions extract_protos.rb
@@ -1,25 +1,22 @@
# extract function prototypes from the headers for making FFIs, etc.
# use like this: find include/ -name "*.h" | xargs cat | ruby extract_protos.rb

FORMAT = ARGV[0]

# match 0 is the whole function proto
# match 1 is the return value
# match 2 returns pointer?
# match 3 is the function name
# match 4 is the remainder
REGEX = /.*?((\w+(\s+\*)?)\s*(cp\w*)\(.*?\))(.*)/
# match 1 is either "static inline " or nil
# match 2 is the return type
# match 3 is the function symbol name
# match 4 is the arguments
PATTERN = /.*?((static inline )?(\w*\*?)\s(cp\w*)\((.*?)\))/

STDIN.readlines.each do|str|
while match = REGEX.match(str)
captures = match.captures
proto, ret, sym, str = captures.values_at(0, 1, 3, 4)
break if ret == "return"
IO.readlines("|gcc -DNDEBUG -E include/chipmunk/chipmunk.h").each do|line|
str = line

while match = PATTERN.match(str)
str = match.post_match

proto, inline, ret, name, args = match.captures.values_at(0, 1, 2, 3, 4)
next if ret == "return" || ret == ""

inline = !!inline

if FORMAT
puts eval('"' + FORMAT + '"')
else
puts "* #{proto} - #{sym}"
end
p({:inline => inline, :return => ret, :name => name, :args => args})
# puts "#{sym} - #{inline ? "static inline " : ""}#{ret} #{sym}(#{args})"
end
end
15 changes: 9 additions & 6 deletions include/chipmunk/chipmunk.h
Expand Up @@ -19,9 +19,6 @@
* SOFTWARE.
*/

/// @defgroup misc Misc
/// @{

#ifndef CHIPMUNK_HEADER
#define CHIPMUNK_HEADER

Expand Down Expand Up @@ -58,21 +55,26 @@ void cpMessage(const char *condition, const char *file, int line, int isError, i

#include "chipmunk_types.h"

// Allocated size for various Chipmunk buffers
/// @defgroup misc Misc
/// @{

/// Allocated size for various Chipmunk buffers
#ifndef CP_BUFFER_BYTES
#define CP_BUFFER_BYTES (32*1024)
#endif

// Chipmunk memory function aliases.
#ifndef cpcalloc
/// Chipmunk calloc() alias.
#define cpcalloc calloc
#endif

#ifndef cprealloc
/// Chipmunk realloc() alias.
#define cprealloc realloc
#endif

#ifndef cpfree
/// Chipmunk free() alias.
#define cpfree free
#endif

Expand Down Expand Up @@ -146,6 +148,8 @@ cpFloat cpMomentForBox(cpFloat m, cpFloat width, cpFloat height);
/// Calculate the moment of inertia for a solid box.
cpFloat cpMomentForBox2(cpFloat m, cpBB box);

//@}

#ifdef __cplusplus
}

Expand All @@ -157,4 +161,3 @@ static inline cpVect operator -(const cpVect v){return cpvneg(v);}

#endif
#endif
//@}
2 changes: 1 addition & 1 deletion include/chipmunk/chipmunk_types.h
Expand Up @@ -137,8 +137,8 @@ static inline cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
/// Hash value type.
typedef uintptr_t cpHashValue;

// Oh C, how we love to define our own boolean types to get compiler compatibility
/// Chipmunk's boolean type.
/// Oh C, how we love to define our own boolean types to get compiler compatibility
#ifdef CP_BOOL_TYPE
typedef CP_BOOL_TYPE cpBool;
#else
Expand Down

0 comments on commit f27c5e1

Please sign in to comment.