Skip to content

Commit

Permalink
style: use twosided<T> instead of std::array<T,2>
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwa committed Dec 26, 2024
1 parent bbd5bc7 commit ca26b0e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions common/bspinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs::path &
json &node = nodes.insert(nodes.end(), json::object()).value();

node.push_back({"planenum", src_node.planenum});
node.push_back({"children", src_node.children});
node.push_back({"children", json::array({src_node.children[0], src_node.children[1]})});
node.push_back({"mins", src_node.mins});
node.push_back({"maxs", src_node.maxs});
node.push_back({"firstface", src_node.firstface});
Expand Down Expand Up @@ -769,7 +769,7 @@ void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs::path &
json &clipnode = clipnodes.insert(clipnodes.end(), json::object()).value();

clipnode.push_back({"planenum", src_clipnodes.planenum});
clipnode.push_back({"children", src_clipnodes.children});
clipnode.push_back({"children", json::array({src_clipnodes.children[0], src_clipnodes.children[1]})});
}
}

Expand Down
4 changes: 2 additions & 2 deletions include/common/bspfile_generic.hh
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ struct dplane_t : qplane3f
struct bsp2_dnode_t
{
int32_t planenum;
std::array<int32_t, 2> children; /* negative numbers are -(leafs+1), not nodes */
twosided<int32_t> children; /* negative numbers are -(leafs+1), not nodes */
qvec3f mins; /* for sphere culling */
qvec3f maxs;
uint32_t firstface;
Expand Down Expand Up @@ -270,7 +270,7 @@ struct mface_t
struct bsp2_dclipnode_t
{
int32_t planenum;
std::array<int32_t, 2> children; /* negative numbers are contents */
twosided<int32_t> children; /* negative numbers are contents */

// serialize for streams
void stream_write(std::ostream &s) const;
Expand Down
6 changes: 3 additions & 3 deletions include/common/bspfile_q1.hh
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ constexpr static int BSPXBRUSHES_CONTENTS_CLIP = -8;
struct bsp29_dnode_t
{
int32_t planenum;
std::array<int16_t, 2>
twosided<int16_t>
children; /* negative numbers are -(leafs+1), not nodes. children[0] is front, children[1] is back */
qvec3s mins; /* for sphere culling */
qvec3s maxs;
Expand All @@ -133,7 +133,7 @@ struct bsp29_dnode_t
struct bsp2rmq_dnode_t
{
int32_t planenum;
std::array<int32_t, 2> children; /* negative numbers are -(leafs+1), not nodes */
twosided<int32_t> children; /* negative numbers are -(leafs+1), not nodes */
qvec3s mins; /* for sphere culling */
qvec3s maxs;
uint32_t firstface;
Expand Down Expand Up @@ -162,7 +162,7 @@ struct bsp2rmq_dnode_t
struct bsp29_dclipnode_t
{
int32_t planenum;
std::array<int16_t, 2> children; /* negative numbers are contents */
twosided<int16_t> children; /* negative numbers are contents */

bsp29_dclipnode_t() = default;

Expand Down
2 changes: 1 addition & 1 deletion include/common/bspfile_q2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ enum q2_contents_t : int32_t
struct q2_dnode_t
{
int32_t planenum;
std::array<int32_t, 2> children; // negative numbers are -(leafs+1), not nodes
twosided<int32_t> children; // negative numbers are -(leafs+1), not nodes
qvec3s mins; // for frustom culling
qvec3s maxs;
uint16_t firstface;
Expand Down
20 changes: 20 additions & 0 deletions include/common/cmdlib.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#pragma once

#include "qvec.hh"

#include <array>
#include <cstdint>
#include <cstring> // for memcpy()
Expand Down Expand Up @@ -305,6 +307,15 @@ inline std::ostream &operator<=(std::ostream &s, const std::array<T, N> &c)
return s;
}

template<typename T>
inline std::ostream &operator<=(std::ostream &s, const twosided<T> &c)
{
s <= c[0];
s <= c[1];

return s;
}

template<typename... T>
inline std::ostream &operator<=(std::ostream &s, std::tuple<T &...> tuple)
{
Expand Down Expand Up @@ -462,6 +473,15 @@ inline std::istream &operator>=(std::istream &s, std::array<T, N> &c)
return s;
}

template<typename T>
inline std::istream &operator>=(std::istream &s, twosided<T> &c)
{
s >= c[0];
s >= c[1];

return s;
}

template<typename... T>
inline std::istream &operator>=(std::istream &s, std::tuple<T &...> tuple)
{
Expand Down
11 changes: 9 additions & 2 deletions include/common/qvec.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1173,13 +1173,16 @@ std::vector<V> PointsAlongLine(const V &start, const V &end, const float step)
bool LinesOverlap(
const qvec3f &p0, const qvec3f &p1, const qvec3f &q0, const qvec3f &q1, double on_epsilon = DEFAULT_ON_EPSILON);

/**
* Same as a std::array<T, 2> with the added semantics that arr[0] is "the front" and arr[1] is "the back".
*/
template<typename T>
struct twosided
{
T front, back;

// 0 is front, 1 is back
constexpr T &operator[](const int32_t &i)
constexpr T &operator[](int32_t i)
{
switch (i) {
case 0: return front;
Expand All @@ -1189,7 +1192,7 @@ struct twosided
throw std::exception();
}
// 0 is front, 1 is back
constexpr const T &operator[](const int32_t &i) const
constexpr const T &operator[](int32_t i) const
{
switch (i) {
case 0: return front;
Expand All @@ -1208,6 +1211,10 @@ struct twosided

// swap the front and back values
constexpr void swap() { std::swap(front, back); }

// std::array compat
using value_type = T;
constexpr size_t size() const { return 2; }
};

namespace qv
Expand Down

0 comments on commit ca26b0e

Please sign in to comment.